-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
85 lines (75 loc) · 2.48 KB
/
handler.js
File metadata and controls
85 lines (75 loc) · 2.48 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
const bot = require('./services/bot.js');
module.exports.chatbot = (event, context, callback) => {
if (event.method === 'GET') {
// facebook app verification
if (event.query['hub.verify_token'] === process.env.FB_APP_TOKEN && event.query['hub.challenge']) {
return callback(null, parseInt(event.query['hub.challenge']));
} else {
return callback(new Error('[403] Invalid token'));
}
}
if (event.method === 'POST') {
event.body.entry.map((entry) => {
entry.messaging.map((messagingItem) => {
const senderId = messagingItem.sender.id;
// handle postback messages
if (messagingItem.postback && messagingItem.postback.payload) {
// Do something with messagingItem.postback.payload
return callback(null, {
statusCode: 200,
body: JSON.stringify({
message: 'Handled postback',
input: event,
}),
});
// handle 'message type' message
} else if (messagingItem.message) {
const msg = messagingItem.message;
// Handle quick message (help only)
if (msg.quick_reply && msg.quick_reply.payload === 'INIT_HELP') {
bot.notifyProcessing(senderId);
bot.sendHelpMessage(senderId).then(function () {
return callback(null, {
statusCode: 200,
body: JSON.stringify({
message: 'Handled help',
input: event,
}),
}
);
}).catch(function (err) {
return callback(err);
});
// Handle text message
} else if (msg.text) {
const text = msg.text;
bot.notifyProcessing(senderId);
// Do something here, or else send a default message like this one:
bot.sendPuzzledApology(senderId).then(function () {
return callback(null, {
statusCode: 404,
body: JSON.stringify({
message: 'Unable to handle request',
input: event,
})
}
);
}).catch(function (err) {
return callback(err);
});
}
}
});
});
} else {
const response = {
statusCode: 400,
body: JSON.stringify({
message: 'Bad Request',
input: event,
}),
};
return callback(null, response);
}
};