-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (56 loc) · 1.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const graphApi = require('./lib/graphApi');
const messengerApi = require('./lib/messengerApi');
const conversationFactory = require('./lib/conversationFactory');
const webhooksHandler = require('./lib/webhooksHandler');
const utils = require('./lib/utils');
const createBot = (app, options) => {
const messageListeners = new Map();
const postbackListeners = new Map();
const conversations = new Map();
const methods = {
send(userId, message, delay) {
if (delay) {
setTimeout(() => messengerApi.sendMessage(options, userId, message), delay);
} else {
messengerApi.sendMessage(options, userId, message);
}
},
on(input, handler) {
messageListeners.set(input, handler);
},
handlePostback(type, handler) {
postbackListeners.set(type, handler);
},
registerPostbackHandlers(handlerObjects) {
Object.keys(handlerObjects).forEach(key => {
this.handlePostback(key, handlerObjects[key]);
});
},
setWelcomeScreenMessage(message) {
messengerApi.setWelcomeScreenMessage(options, message);
},
startConversation(userId) {
const conversation = conversationFactory.create(methods, userId);
conversations.set(userId, conversation);
return conversation;
}
};
app.get(options.webhooksPath, (req, res) => {
if (graphApi.isValidToken(req.query['hub.verify_token'], options.verifyToken)) {
res.send(req.query['hub.challenge']);
} else {
res.send('Error, wrong validation token');
}
});
app.post(`${options.webhooksPath}/`, (req, res) => {
console.log(`Incoming request: ${JSON.stringify(req.body)}`);
webhooksHandler.handle(methods, conversations, messageListeners, postbackListeners, req.body);
res.sendStatus(200);
});
return methods;
};
module.exports = {
createBot,
utils
};