-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (48 loc) · 1.64 KB
/
app.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
const FacebookMessengerBot = require('calamars').FacebookMessengerBot;
const Forks = require('./src/forks/forks');
const payloads = require('./src/forks/payloads');
const menu = require('./src/menu');
const Helpers = require('./src/helpers');
const messages = require('./src/messages');
let storage = [];
const onMessage = function(updateEvent){
Helpers.setUserStorage(updateEvent, storage, mybot)
.then(() => {
let { step } = storage[updateEvent.update.sender.id]
step = step === 0? 'initial': step
if(Forks[step] && Forks[step].resolve)
Forks[step].resolve(updateEvent, storage, mybot)
})
};
const onPostback = function(updateEvent){
Helpers.setUserStorage(updateEvent, storage, mybot)
.then(()=>{
let payload = updateEvent.update.postback.payload;
let p = Helpers.isJSON(payload)? Helpers.getJSON(payload).type : payload
if(p.indexOf('/') !== -1){
let p1 = p.split('/')[0]
let p2 = p.split('/')[1]
if(payloads[p1]) payloads[p1](updateEvent, storage, p2)
} else {
if(payloads[p]) payloads[p](updateEvent, storage)
}
})
}
const mybot = new FacebookMessengerBot({
port: process.env.PORT || 3000,
callbackPath: process.env.FB_CALLBACK_PATH,
verifyToken: process.env.FB_VERIFY_TOKEN,
pageTokens: [process.env.FB_PAGE_ACCESS_TOKEN],
listeners: { onMessage, onPostback }
});
mybot.start().then(function(){
console.log(`server is running on port ${process.env.PORT || 3000}`);
mybot.setWelcomeMessage({
text: messages('greeting')
})
mybot.setThreadSettings({
type: 'call_to_actions',
state: 'existing_thread',
cta: menu
})
})