-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
459 lines (392 loc) · 14.1 KB
/
Copy pathindex.js
File metadata and controls
459 lines (392 loc) · 14.1 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//
// This is main file containing code implementing the Express server and functionality for the Express echo bot.
//
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const path = require('path');
var messengerButton = "<html><head><title>Facebook Messenger Bot </title></head><body><h1>Facebook Messenger Bot by <i><a href=\"https://twitter.com/mynameistsp\">armag</a></i></h1>This is a bot based on Messenger Platform. For more details, see their <a href=\"https://developers.facebook.com/docs/messenger-platform/guides/quick-start\">docs</a>.<br>The complete source code can be found on my <a href=\"https://github.com/armag-pro/messenger-bot\">GitHub repo</a>.<script src=\"https://button.glitch.me/button.js\" data-style=\"glitch\"></script><div class=\"glitchButton\" style=\"position:fixed;top:20px;right:20px;\"></div></body></html>";
var quizQs = ['My fav place is:','I was born in','I am:','I prefer','I have mounted a:','I have read:'];
var options = [ ['Las Vegas','Paris'],['West Bengal','Rajasthan'],['6.1+ ft','6.1- ft'],['Driving','Riding'],
['Helicopter','Elephant'],['One Indian Girl','One night @ call centre']];
var Y = 'PAYLOAD_CORRECT_ANS';
var N = 'PAYLOAD_WRONG_ANS';
var correctAns = [[Y,N],[Y,N],[N,Y],[Y,N],[Y,N],[N,Y]];
// The rest of the code implements the routes for our Express server.
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Webhook validation
app.get('/webhook', function(req, res) {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === process.env.VERIFY_TOKEN) {
console.log("Validating webhook");
res.status(200).send(req.query['hub.challenge']);
} else {
console.error("Failed validation. Make sure the validation tokens match.");
res.sendStatus(403);
}
});
// Display the web page
app.get('/', function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(messengerButton);
res.end();
});
// Message processing
app.post('/webhook', function (req, res) {
console.log(req.body);
var data = req.body;
// Make sure this is a page subscription
if (data.object === 'page') {
// Iterate over each entry - there may be multiple if batched
data.entry.forEach(function(entry) {
var pageID = entry.id;
var timeOfEvent = entry.time;
// Iterate over each messaging event
entry.messaging.forEach(function(event) {
if (event.message) {
receivedMessage(event);
} else if (event.postback) {
receivedPostback(event);
} else {
console.log("Webhook received unknown event: ", event);
}
});
});
// Assume all went well.
//
// You must send back a 200, within 20 seconds, to let us know
// you've successfully received the callback. Otherwise, the request
// will time out and we will keep trying to resend.
res.sendStatus(200);
}
});
// Incoming events handling
function receivedMessage(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfMessage = event.timestamp;
var message = event.message;
console.log("Received message for user %d and page %d at %d with message:",
senderID, recipientID, timeOfMessage);
console.log(JSON.stringify(message));
var messageId = message.mid;
var messageText = message.text;
var messageQR = message.quick_reply;
var messageAttachments = message.attachments;
if (messageQR && messageText) {
for(var i = 0; i<quizQs.length;i++){
if(messageText == options[i][0] || messageText == options[i][1]){
if(messageQR.payload == Y){
sendTextMessage(senderID, "Correct!!! :D");
sendImage(senderID,"https://media3.giphy.com/media/3ornjXIIShZ2MgyyHu/giphy.gif");
}else {
sendTextMessage(senderID, "Nope...That's wrong :/");
sendImage(senderID,"https://cdn.meme.am/cache/instances/folder425/57312425.jpg");
}
sendGenericMessage3(senderID);
break;
}
}
} else if (messageText ) {
if(messageText.match(/^(follow|find|tuhin|twitter)$/i)) {
sendGenericMessage1(senderID);
} else if(messageText.match(/^(restart|start|again|play|Start\/Resume|quiz)$/i)) {
sendGenericMessage2(senderID);
} else if(messageText.match(/^(.*((^|\W)(i\sluv\su)(\W|\b)|(^|\W)(i\sluv\syou)(\W|\b)|(^|\W)(i\slove\su)(\W|\b)|(^|\W)(i\slove\syou)(\W|\b)|(^|\W)(i\slike\su)(\W|\b)).*)$/i)) {
sendTypingIndicatorOn(senderID);
setTimeout(sendTypingIndicatorOff,6000);
setTimeout(function(){sendTextMessage(senderID, "Backspaced everything!!!")},6000);
setTimeout(function(){sendImage(senderID, "https://media2.giphy.com/media/xUA7aWi4gtOdAaX9q8/giphy.gif")},7500);
} else if(messageText.match(/^(.*((^|\W)no(\W|\b)|(^|\W)oops(\W|\b)|(^|\W)hmm(\W|\b)|(^|\W)ahem(\W|\b)|(^|\W)wtf(\W|\b)).*)$/i)) {
sendTextMessage(senderID, "😭");
} else if(messageText.match(/^(.*((^|\W)shit(\W|\b)|(^|\W)what(\W|\b)|(^|\W)idk(\W|\b)|(^|\W)wat(\W|\b)|(^|\W)dont(\W|\b)).*)$/i)) {
sendTextMessage(senderID, ":'(");
} else if(messageText.match(/^(.*((^|\W)bye(\W|\b)|(^|\W)bbye(\W|\b)|(^|\W)bubbye(\W|\b)|(^|\W)quit(\W|\b)|(^|\W)exit(\W|\b)).*)$/i)) {
sendTextMessage(senderID, "So soon...I was liking it so much.");
sendTextMessage(senderID, "You are so much fun to be around");
sendTextMessage(senderID, "do come back later please...");
sendTextMessage(senderID, "heres a quick link btw m.me/torquebull");
sendTextMessage(senderID, "^_^");
} else if(messageText.match(/^(.*((^|\W)chat(\W|\b)|(^|\W)bot(\W|\b)|(^|\W)fb(\W|\b)|(^|\W)lyk(\W|\b)|(^|\W)like(\W|\b)).*)$/i)) {
sendTextMessage(senderID, "(y)");
} else if(messageText.match(/^(.*((^|\W)image(\W|\b)|(^|\W)img(\W|\b)|(^|\W)logo(\W|\b)|(^|\W)jpg(\W|\b)|(^|\W)tsp(\W|\b)).*)$/i)) {
sendImage(senderID,"http://1d-paca.com/sites/default/files/imageupload/image_square/198440-logo-tsp-12042012-1749.jpg");
} else if(messageText.match(/(video|vid)/i)) {
sendVideo(senderID,"https://github.com/mediaelement/mediaelement-files/blob/master/big_buck_bunny.mp4");
} else if(messageText.match(/^(.*((^|\W)sticker(\W|\b)|(^|\W)luv(\W|\b)|(^|\W)love(\W|\b)|(^|\W)wow(\W|\b)|(^|\W)gud(\W|\b)|(^|\W)good(\W|\b)).*)$/i)) {
sendImage(senderID,"http://i.imgur.com/hJK1gUE.png");
} else {
var replies = ['what do you mean by "' + messageText + '" ?', 'You look awesome!',
'Not sure I understood that.','Sry what?','wtf','You know I like you', ':/',
'hmm...','You dont make any sense!','Speak english!','try typing `follow` or `play`',
'say `tuhin`!','Just type in `tsp`','-_-','10010011100 is that what you mean :|]',':v','<(") <(") <(")','what?? 8-)'];
var ch = Math.floor(Math.random()*replies.length);
sendTextMessage(senderID, replies[ch]);
}
} else if (messageAttachments) {
sendTextMessage(senderID, "Message with attachment received and ignored :poop ");
//sendTextMessage(senderID, JSON.stringify(messageAttachments));
}
}
function receivedPostback(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfPostback = event.timestamp;
// The 'payload' param is a developer-defined field which is set in a postback
// button for Structured Messages.
var payload = event.postback.payload;
console.log("Received postback for user %d and page %d with payload '%s' " +
"at %d", senderID, recipientID, payload, timeOfPostback);
// When a postback is called, we'll send a message back to the sender to
// let them know it was successful
if(payload == 'twitter_follow_later'){
sendTextMessage(senderID, "No problem, but sure do it later! o.O");
}
else if(payload == 'quiz_play_later'){
sendTextMessage(senderID, "Oops...type quiz or play to start answering anytime later.");
}
else if(payload == 'quiz_play_begin'){
sendQuickReply(senderID,Math.floor(Math.random()*quizQs.length));
} else if(payload == Y){
sendTextMessage(senderID, "Correct!!! :D");
} else if(payload == N){
sendTextMessage(senderID, "Nope...That's wrong :/");
} else if(payload == 'QUIZ_PAYLOAD'){
sendGenericMessage2(senderID);
} else if(payload == 'SHARE_LOCATION_PAYLOAD'){
sendQuickReplyLocation(senderID, "discarded");
} else if(payload == 'SCORE_PAYLOAD'){
sendImage(senderID,"http://www.wbaf2016.org/wp-content/uploads/2017/03/credit-score.png");
sendTextMessage(senderID, "who cares about the score :P \n\n\nMust say, your net is too slow for an image!");
} else if(payload == 'GET_STARTED_PAYLOAD'){
sendImage(senderID, "https://media0.giphy.com/media/26xBwdIuRJiAIqHwA/giphy.gif");
} else{
sendTextMessage(senderID, "Got it! O:)");
}
}
//////////////////////////
// Sending helpers
//////////////////////////
function sendTextMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText
}
};
callSendAPI(messageData);
}
function sendQuickReply(recipientId, ch) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text:quizQs[ch],
quick_replies:[
{
content_type:"text",
title:options[ch][0],
payload:correctAns[ch][0],
},
{
content_type:"text",
title:options[ch][1],
payload:correctAns[ch][1],
}
]
}
};
callSendAPI(messageData);
}
function sendQuickReplyLocation(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text:"Please share your location: O:)",
quick_replies:[
{
content_type:"location",
}
]
}
};
callSendAPI(messageData);
}
function sendImage(recipientId, imageUrl){
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment:{
type:"image",
payload:{
url: imageUrl
}
}
}
};
callSendAPI(messageData);
}
function sendVideo(recipientId, videoUrl) {
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment:{
type:"video",
payload:{
url: videoUrl
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessage1(recipientId) {
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Find me on twitter",
subtitle: "Follow me and Retweet me.",
item_url: "https://twitter.com/mynameistsp",
image_url: "https://pbs.twimg.com/profile_images/1695114892/216776_147827421966054_100002163903678_298416_4623411_n_400x400.jpg",
buttons: [{
type: "web_url",
url: "https://twitter.com/mynameistsp",
title: "Open Twitter"
}, {
type: "element_share",
}, {
type: "postback",
title: "Not now",
payload: "twitter_follow_later",
}],
}]
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessage2(recipientId) {
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Answer 1 simple question",
subtitle: "Let's see how much you know about me!",
item_url: "https://www.britannica.com/quiz/browse",
image_url: "http://www.mathslogic.in/files/ready-quiz.gif",
buttons: [{
type: "postback",
title: "Start Now!",
payload: "quiz_play_begin",
}, {
type: "postback",
title: "Not now",
payload: "quiz_play_later",
}],
}]
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessage3(recipientId) {
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Answer 1 more?",
subtitle: "Lets see if you were just guessing!",
item_url: "https://www.britannica.com/quiz/browse",
image_url: "https://i.ytimg.com/vi/H2Y1MIdj4sg/hqdefault.jpg",
buttons: [{
type: "postback",
title: "Yass Please!",
payload: "quiz_play_begin",
}, {
type: "postback",
title: "Enough",
payload: "quiz_play_later",
}],
}]
}
}
}
};
callSendAPI(messageData);
}
function sendTypingIndicatorOn(recipientId){
var messageData = {
recipient:{
id: recipientId
},
sender_action: "typing_on"
};
callSendAPI(messageData);
}
function sendTypingIndicatorOff(recipientId){
var messageData = {
recipient:{
id: recipientId
},
sender_action: "typing_off"
};
callSendAPI(messageData);
}
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
console.log("Successfully sent generic message with id %s to recipient %s",
messageId, recipientId);
} else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}
// Set Express to listen out for HTTP requests
var server = app.listen(process.env.PORT || 3000, function () {
console.log("Listening on port %s", server.address().port);
});