Last active
June 1, 2016 09:02
-
-
Save edlvj/698a35db5f753ef278f41b2922fc5106 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var express = require('express'), | |
http = require('http'), | |
Bot = require('messenger-bot'); | |
const Wit = require('node-wit').Wit; | |
let bot = new Bot({ | |
token: '', | |
verify: '', | |
page_id: '' | |
}) | |
// нужно для Wit | |
const firstEntityValue = (entities, entity) => { | |
const val = entities && entities[entity] && | |
Array.isArray(entities[entity]) && | |
entities[entity].length > 0 && | |
entities[entity][0].value; | |
if (!val) { | |
return null; | |
} | |
return typeof val === 'object' ? val.value : val; | |
}; | |
const sessions = {}; | |
const findOrCreateSession = (fbid) => { | |
let sessionId; | |
Object.keys(sessions).forEach(k => { | |
if (sessions[k].fbid === fbid) { | |
sessionId = k; | |
} | |
}); | |
if (!sessionId) { | |
sessionId = new Date().toISOString(); | |
sessions[sessionId] = {fbid: fbid, context: {}}; | |
} | |
return sessionId; | |
}; | |
bot.on('error', (err) => { | |
console.log(err.message) | |
}) | |
//buttons posback | |
// обработчик на новые сообщения | |
bot.on('message', (payload, reply) => { | |
let text = payload.message.text | |
var sessionId = findOrCreateSession(payload.sender.id); | |
const actions = { | |
say(sessionId, context, message, cb) { | |
const recipientId = sessions[sessionId].fbid; | |
if (recipientId) { | |
reply ({ text: message}); | |
} else { | |
console.log('Oops! Couldn\'t find user for session:', sessionId); | |
cb(); | |
} | |
}, | |
merge(sessionId, context, entities, message, cb) { | |
const tovar = firstEntityValue(entities, 'tovar'); | |
if(context.adress) { | |
delete context.adress; | |
} | |
if(context.operator) { | |
delete context.operator; | |
} | |
if (tovar) { | |
context.tovar = tovar; | |
} | |
const adress = firstEntityValue(entities, 'adress'); | |
if (adress) { | |
if(context.tovar) { | |
delete context.tovar; | |
} | |
if(context.operator) { | |
delete context.operator; | |
} | |
context.adress = adress; | |
} | |
const operator = firstEntityValue(entities, 'operator'); | |
if (operator) { | |
if(context.tovar) { | |
delete context.tovar; | |
} | |
if(context.adress) { | |
delete context.adress; | |
} | |
context.operator = operator; | |
} | |
cb(context); | |
}, | |
error(sessionId, context, error) { | |
console.log(error.message); | |
}, //Приветсвие | |
['auth'](sessionId, context, cb) { | |
reply({ text: 'Привет я Твой Bot.' }) | |
cb(context); | |
}, | |
['search'](sessionId, context, cb) { | |
reply({ text: 'Поиск.' }) | |
}); | |
cb(context); | |
}, | |
['setAdress'](sessionId, context, cb) { | |
//установка адреса | |
context.adress; | |
cb(context); | |
}, | |
['orders'](sessionId, context, cb) { | |
//Корзина | |
cb(context); | |
}, | |
['oformit'](sessionId, context, cb) { | |
//Прием заказов | |
cb(context); | |
}, | |
['sendtochat'](sessionId, context, cb) { | |
//Отправка в чат | |
cb(context); | |
},['gethelp'](sessionId, context, cb) { | |
cb(context); | |
}, | |
}; | |
//Wit Server Access Token | |
witToken = ''; | |
const wit = new Wit(, actions); | |
wit.runActions( | |
sessionId, // the user's current session | |
text, // the user's message | |
sessions[sessionId].context, // the user's current session state | |
(error, context) => { | |
if (error) { | |
console.log('Oops! Got an error from Wit:', error); | |
} else { | |
// Our bot did everything it has to do. | |
// Now it's waiting for further messages to proceed. | |
console.log(context); | |
// Based on the session state, you might want to reset the session. | |
// This depends heavily on the business logic of your bot. | |
// Example: | |
if (context['done']) { | |
delete sessions[sessionId]; | |
} | |
// Updating the user's current session state | |
sessions[sessionId].context = context; | |
} | |
} | |
); | |
}); | |
//Для Webhook | |
var app = express(); | |
http.createServer(bot.middleware()).listen(process.env.PORT); | |
app.get('/', function (req, res) { | |
res.send('Bot working'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment