-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
404 lines (316 loc) · 10.5 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
* Created by kinnimew on 26/4/16.
*/
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var request = require("request");
const FB_MESSENGER_ENDPOINT = "https://graph.facebook.com/v2.6/me/messages";
const FB_PROFILE_ENDPOINT = "https://graph.facebook.com/v2.6/";
const FB_SETTINGS_ENDPOINT = "https://graph.facebook.com/v2.6/me/thread_settings";
const NOTIFICATION_TYPE = {
REGULAR: "REGULAR",
SILENT_PUSH: "SILENT_PUSH",
NO_PUSH: "NO_PUSH"
};
function FBBotFramework(options, cb) {
if (!options || !options.page_token) {
var error = new Error("Page Access Token missing. See FB documentation for details: https://developers.facebook.com/docs/messenger-platform/quickstart");
if (typeof cb === "function") {
return cb(error)
}
throw error;
}
this.page_token = options.page_token;
this.verify_token = options.verify_token;
this.commands = [];
if (cb) cb(null);
}
// Setup
util.inherits(FBBotFramework, EventEmitter);
FBBotFramework.NOTIFICATION_TYPE = NOTIFICATION_TYPE;
FBBotFramework.prototype.verify = function (req, res) {
if (req.query['hub.verify_token'] === this.verify_token) {
res.send(req.query['hub.challenge']);
} else {
res.status(500).send('Error, wrong validation token');
}
};
// Send API, Details please visit https://developers.facebook.com/docs/messenger-platform/send-api-reference#request
FBBotFramework.prototype.send = function (recipient, messageData, notificationType, cb) {
notificationType = notificationType || NOTIFICATION_TYPE.REGULAR;
if (typeof notificationType === 'function') {
cb = notificationType;
notificationType = NOTIFICATION_TYPE.REGULAR
}
var req = {
url: FB_MESSENGER_ENDPOINT,
qs: {access_token: this.page_token},
method: "POST",
json: {
recipient: {id: recipient},
message: messageData,
notification_type: notificationType
}
};
request(req, function (err, res, body) {
if (cb) {
if (err) return cb(err);
if (body.error) return cb(body.error);
cb(null, body);
}
});
};
FBBotFramework.prototype.sendTextMessage = function (recipient, text, notificationType, cb) {
var messageData = {text: text};
this.send(recipient, messageData, notificationType, cb);
};
FBBotFramework.prototype.sendAudioAttachment = function (recipient, audioUrl, notificationType, cb) {
var messageData = {
attachment: {
type: "audio",
payload: {url: audioUrl}
}
};
this.send(recipient, messageData, notificationType, cb);
};
FBBotFramework.prototype.sendVideoAttachment = function (recipient, videoUrl, notificationType, cb) {
var messageData = {
attachment: {
type: "file",
payload: {url: videoUrl}
}
};
this.send(recipient, messageData, notificationType, cb);
};
FBBotFramework.prototype.sendFileAttachment = function (recipient, fileUrl, notificationType, cb) {
var messageData = {
attachment: {
type: "video",
payload: {url: fileUrl}
}
};
this.send(recipient, messageData, notificationType, cb);
};
// TODO: Audio, Video and File Upload
FBBotFramework.prototype.sendImageMessage = function (recipient, imageUrl, notificationType, cb) {
var messageData = {
attachment: {
type: "image",
payload: {url: imageUrl}
}
};
this.send(recipient, messageData, notificationType, cb);
};
FBBotFramework.prototype.sendButtonMessage = function (recipient, text, buttons, notificationType, cb) {
var messageData = {
attachment: {
type: "template",
payload: {
template_type: "button",
text: text,
buttons: buttons
}
}
};
this.send(recipient, messageData, notificationType, cb);
};
// Limitation
// Title: 45 characters
// Subtitle: 80 characters
// Call-to-action title: 20 characters
// Call-to-action items: 3 buttons
// Bubbles per message (horizontal scroll): 10 elements
FBBotFramework.prototype.sendBubbleMessage = FBBotFramework.prototype.sendGenericMessage = function (recipient, elements, notificationType, cb) {
var messageData = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: elements
}
}
};
this.send(recipient, messageData, notificationType, cb);
};
FBBotFramework.prototype.sendReceiptMessage = function (recipient, receipt, notificationType, cb) {
if (!receipt.template_type) {
receipt.template_type = "receipt";
}
var messageData = {
"attachment": {
"type": "template",
"payload": receipt
}
};
this.send(recipient, messageData, notificationType, cb);
};
FBBotFramework.prototype.getUserProfile = function (userId, cb) {
var req = {
method: "GET",
uri: FB_PROFILE_ENDPOINT + userId,
qs: {
fields: 'first_name,last_name,profile_pic,locale,timezone,gender',
access_token: this.page_token
},
json: true
};
request(req, function (err, res, body) {
if (err) return cb(err);
if (body.error) return cb(body.error);
cb(null, body);
});
};
// Bot Profile
FBBotFramework.prototype.getBotProfile = function (fields, cb) {
var req = {
method: 'GET',
uri: FB_PROFILE_ENDPOINT + 'messenger_profile',
qs: {
fields: fields.join(),
access_token: this.page_token
},
json: true
};
request(req, function (err, res, body) {
if (err) return cb(err);
if (body.error) return cb(body.err);
cb(null, body);
})
};
// Middleware
FBBotFramework.prototype.middleware = function () {
var bot = this;
return function (req, res) {
if (req.method === 'GET') {
return bot.verify(req, res);
}
if (req.method === 'POST') {
// Read data from the request
var data = '';
req.setEncoding('utf8');
req.on('data', function (chunk) {
data += chunk;
});
req.on('end', function () {
// Always return HTTP200 to Facebook's POST Request
res.send({});
var messageData = JSON.parse(data);
var messagingEvent = messageData.entry[0].messaging;
messagingEvent.forEach(function (event) {
// Extract senderID, i.e. recipient
var sender = event.sender.id;
// Trigger onEcho Listener
if (event.message && event.message.is_echo) {
return bot.emit('echo', event.recipient.id, event.message.text);
}
// Trigger quickyReply Listener
if (event.message && event.message.quick_reply) {
return bot.emit('quickreply', sender, event.message.quick_reply.payload);
}
// Trigger onMessage Listener
if (event.message && event.message.text) {
bot.emit('message', sender, event.message.text);
}
// Trigger onPostback Listener
if (event.postback && event.postback.payload) {
bot.emit('postback', sender, event.postback.payload);
}
// Trigger onAttachment Listener
if (event.message && event.message.attachments) {
bot.emit('attachment', sender, event.message.attachments);
}
});
});
}
};
};
FBBotFramework.prototype.setGreetingText = function (text, cb) {
var req = {
url: FB_SETTINGS_ENDPOINT,
qs: {access_token: this.page_token},
method: "POST",
json: {
"setting_type": "greeting",
"greeting": {
"text": text
}
}
};
request(req, function (err, res, body) {
if (cb) {
if (err) return cb(err);
if (body.error) return cb(body.error);
cb(null, body);
}
});
};
FBBotFramework.prototype.setGetStartedButton = function (payload, cb) {
var req = {
url: FB_SETTINGS_ENDPOINT,
qs: {access_token: this.page_token},
method: "POST",
json: {
"setting_type": "call_to_actions",
"thread_state": "new_thread",
"call_to_actions": [
{
"payload": payload
}
]
}
};
request(req, function (err, res, body) {
if (cb) {
if (err) return cb(err);
if (body.error) return cb(body.error);
cb(null, body);
}
});
};
FBBotFramework.prototype.setPersistentMenu = function (menuButtons, cb) {
var req = {
url: FB_SETTINGS_ENDPOINT,
qs: {access_token: this.page_token},
method: "POST",
json: {
"setting_type": "call_to_actions",
"thread_state": "existing_thread",
"call_to_actions": menuButtons
}
};
request(req, function (err, res, body) {
if (cb) {
if (err) return cb(err);
if (body.error) return cb(body.error);
cb(null, body);
}
});
};
FBBotFramework.prototype.sendQuickReplies = function (recipient, text, replies, notificationType, cb) {
var messageData = {
text: text,
quick_replies: replies
};
this.send(recipient, messageData, notificationType, cb);
};
FBBotFramework.prototype.sendLocationRequest = function (recipient, text, notificationType, cb) {
var messageData = {
text: text,
quick_replies: [{content_type: "location"}]
};
this.send(recipient, messageData, notificationType, cb);
};
FBBotFramework.prototype.sendListMessage = function (recipient, elements, notificationType, cb) {
var messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "list",
"top_element_style": "compact",
"elements": elements
}
}
};
this.send(recipient, messageData, notificationType, cb);
};
module.exports = FBBotFramework;