-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
715 lines (619 loc) · 23.9 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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
var request = require('request');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var date_time = require('date-and-time');
var Client = require('node-rest-client').Client;
var client = new Client();
var moment = require('moment');
const roundTo = require('round-to');
var base64 = require('base-64');
var apiai = require('apiai');
var randomInt = require('random-int');
var congifURL = "<HEROKU_APP_URL>";
//var dotenv = require('dotenv').config()
var dialogFlow = apiai("<DIALOGFLOW_TOKEN>");
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(express.static('public'));
app.listen((process.env.PORT || 3000));
var combos;
var status;
var PAGE_ACCESS_TOKEN = '<PAGE_ACCESS_TOKEN>'
const sessions = {};
const findOrCreateSession = (fbid) => {
let sessionId;
Object.keys(sessions).forEach(k => {
if (sessions[k].fbid === fbid) {
sessionId = k;
}
});
if (!sessionId) {
sessionId = new Date().toISOString();
sessions[sessionId] = {
fbid: fbid,
Name: {},
context: {},
Email: {},
academicArea: 0,
};
}
// console.log("SESSION ID : " + sessionId)
return sessionId;
};
app.get('/', function (req, res) {
res.send('Hello world, Bot is Active.')
});
app.get('/webhook', function (req, res) {
if (req.query['hub.verify_token'] === '<YOUT_RANDOM_TOKEN>') {
res.send(req.query['hub.challenge']);
} else {
res.send('Invalid verify token');
}
});
app.post('/webhook', function (req, res) {
var data = req.body;
if (data.object == 'page') {
data.entry.forEach(function (pageEntry) {
var pageID = pageEntry.id;
var timeOfEvent = pageEntry.time;
if (pageEntry.messaging != null) {
pageEntry.messaging.forEach(function (messagingEvent) {
var senderID = messagingEvent.sender.id;
if (messagingEvent.message)
receivedMessage(messagingEvent);
else if (messagingEvent.postback)
receivedPostback(messagingEvent);
else
console.log("Webhook received unknown messagingEvent: ");
});
}
});
res.sendStatus(200);
}
});
function receivedPostback(event) {
var senderID = event.sender.id;
//var recipientID = event.recipient.id;
const sessionId = findOrCreateSession(senderID);
var timeOfPostback = event.timestamp;
if (event.postback.payload)
payload_value = event.postback.payload
switch (payload_value.split('|')[0]) {
case 'hobbies':
sendTextMessage(senderID, "Code Code Code!! \n\nHaha I am just Kidding. I looooovve Swimming 🏊🏻 and Skating ⛸️ \n\nWhat are your hobbies?");
break;
case 'fun':
sendGifMessage(senderID, "winking_dean");
setTimeout(function () {
sendFun(senderID);
}, 3500)
break;
case 'GET_STARTED_PAYLOAD':
user_name(senderID);
setTimeout(function () {
sendStartingMessage(senderID)
}, 1000)
break;
case 'restartBot':
restart_Bot(senderID)
break;
case 'aboutme':
sendTextMessage(senderID, "Hello world! I am 23 year old Software Developer. There is nothing much to tell about myself :P \n\nMy Instagram Profile👇 \nhttps://www.instagram.com/mayankkgandhi/ \n\nThis is a simple Chatbot which I have built and it will be updated time to time.. \n\n Just interact and have fun! :D ");
setTimeout(function () {
sendStartingMessage(senderID)
}, 1500)
break;
case 'contactus':
sendTextMessage(senderID, "For any queries you can mail me at [email protected] . \nThanks!");
break;
default:
console.log("===============================================Received Unknow ===========================================");
}
};
function receivedMessage(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var message = event.message;
var messageText = message.text;
var knownMessage = true;
var quickReplyMessage = false;
var messageAttachments = message.attachments;
var sticker = message.sticker_id;
if (message.quick_reply) {
if (message.quick_reply.payload) {
quickReplyMessage = true;
switch (message.quick_reply.payload.split('|')[0]) {
case 'aboutme':
sendTextMessage(senderID, "Hello world! I am 23 year old Software Developer. There is nothing much to tell about myself :P \n\nMy Instagram Profile👇 \nhttps://www.instagram.com/mayankkgandhi/ \n\nThis is a simple Chatbot which I have built and it will be updated time to time.. \n\n Just interact and have fun! :D ");
setTimeout(function () {
sendStartingMessage(senderID)
}, 1500)
break;
case 'fun':
if (message.quick_reply.payload.split('|')[1] == "meme") {
var favorites = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12];
var favorite = favorites[Math.floor(Math.random() * favorites.length)];
Image(senderID, favorite + ".jpg")
setTimeout(function () {
continueAfterfun(senderID, "meme");
}, 3000)
}
else if (message.quick_reply.payload.split('|')[1] == "riddle") {
var favorites = ["At night they come without being fetched. By day they are lost without being stolen. What are they? \n\n\n\nStars",
"What occurs once in every minute, twice in every moment, but never in a thousand years? \n\n\n\nThe Letter M ",
"What 8 letter word can have a letter taken away and it still makes a word. Take another letter away and it still makes a word. Keep on doing that until you have one letter left. What is the word? \n\n\n\nThe word is starting! starting, staring, string, sting, sing, sin, in, I. So cool!!!",
"Re-arrange the letters,\n\nO O U S W T D N E J R \n\nto spell just one word. \n\n\n\n'just one word'",
"A woman shoots her husband. Then she holds him under water for over 5 minutes. Finally, she hangs him. But 5 minutes later they both go out together and enjoy a wonderful dinner together. How can this be? \n\n\n\nThe woman was a photographer. She shot a picture of her husband, developed it, and hung it up to dry."];
var favorite = favorites[Math.floor(Math.random() * favorites.length)];
// var postmessage = "hi my favorite site is " + favorite;
sendTextMessage(senderID, favorite);
console.log(favorite)
setTimeout(function () {
continueAfterfun(senderID, "riddle");
}, 3000)
}
break;
case 'menu':
sendStartingMessage(senderID);
break;
case 'contactus':
sendTextMessage(senderID, "For any queries you can mail me at [email protected] . \nThanks!");
break;
case 'restartBot':
restart_Bot(senderID)
break;
default:
{
const sessionId = findOrCreateSession(senderID);
if (sessions[sessionId].academicArea === 0) {
knownMessage = false;
sessions[sessionId].Email = messageText;
setTimeout(function () {
continueAfterfun(senderID);
}, 1000)
} else {
user_name(senderID);
setTimeout(function () {
sendStartingMessage(senderID)
}, 1000)
}
}
}
}
}
else if (messageText) {
var lowAll = messageText.toLowerCase();
var greets = ['hi', 'hii', 'hiii', 'hiiii', 'hiiiii', 'hiiiiii', 'hey', 'heyy', 'hello', 'reply', 'test', 'testing', 'dai',
'buddy', 'bud', 'ji', 'hungry', 'answer pls', 'pls answer', 'answer please', 'please answer',
'hellooo', 'helloo', 'heloo', 'hell', 'ans pls', 'pls ans', 'ans please', 'please ans',
'bruh', 'hey', 'hie', 'wassup', 'oye',
'hui', 'sup', 'bro', 'sup', 'bro', 'heylo',
'namaste', 'namasthe', 'namasthey', 'aloha', 'ahoy',
'ahoyy', 'ahoy mate', 'hi mate', 'ciao',
'guten tag', 'bonjour', 'bonjur', 'hola',
'olaa', 'ola', 'hallo', 'halo', 'salaam',
'salam', 'ohayo', 'konichiwa', 'konnichiwa',
'marhaba', 'merhaba', 'ni hau', 'mate'
];
if (greets.indexOf(lowAll) >= 0) {
{
const sessionId = findOrCreateSession(senderID);
user_name(senderID);
setTimeout(function () {
sendStartingMessage(senderID)
}, 1000)
}
}
else if (messageText === 'yoaddmenu') {
addpersistantMenu();
} else if (messageText === 'yoremovemenu') {
removePersistentMenu();
} else {
{
const sessionId = findOrCreateSession(senderID);
apiAI(senderID, messageText);
}
}
}
else if (messageAttachments) {
console.log("inside attachements===========")
console.log(event)
console.log(messageAttachments)
if (sticker && messageAttachments[0].type == "image") {
sendTextMessage(senderID, "Yo! 👍🏼 to you too.")
} else if (!sticker && messageAttachments[0].type == "image") {
sendGifMessage(senderID, "not_even_mad");
}
}
};
function apiAI(sender, text) {
const sessionId = findOrCreateSession(sender);
var request = dialogFlow.textRequest(text, {
sessionId: sender
});
request.on('response', function (response) {
console.log("api AI response >>>>>");
let responseText = response.result.fulfillment.speech;
let responseData = response.result.fulfillment.data;
let messages = response.result.fulfillment.messages;
let action = response.result.action;
let contexts = response.result.contexts;
let parameters = response.result.parameters;
let intentName = response.result.metadata.intentName;
if (action == "") {
switch (intentName) {
case 'foul-words':
console.log('Unknown query IF ACTION');
var favorites = ["You have your entire life to be a jerk. Why not take today off?!",
"Please, keep talking. I only yawn when I’m super fascinated.",
"Do you talk to your Daddy like this?!",
"You don't know how to talk to your Daddy.. Do you?!",
"Sorry, I didn’t get that. I don’t speak bullshit.",
"Are you always such an idiot, or do you just show off when I’m around?",
"I’d tell you how I really feel, but I wasn’t born with enough middle fingers to express myself in this case.",
"I'm sorry I didn't get that - I don't speak idiot.",
"I'm sorry, was I meant to be offended? The only thing offending me is your face."];
var favorite = favorites[Math.floor(Math.random() * favorites.length)];
// var postmessage = "hi my favorite site is " + favorite;
sendTextMessage(sender, favorite);
console.log(favorite)
break;
case 'hobbies':
sendTextMessage(sender, "That is really nice. I hope you are having fun! :D")
break;
}
} else if (response.result.score === 0) {
console.log('Unknown query ELSE IF CASE');
//api ai could not evaluate input.
console.log('Unknown query' + response.result.resolvedQuery);
sendTextMessage(sender, "I don't understand what you're trying to say! Can you be more specific?");
} else {
console.log('Unknown query ELSE CASE');
sendTextMessage(sender, responseText);
}
});
request.on('error', function (error) {
console.log("error");
console.log(error);
});
request.end();
}
function sendGifMessage(sender, name) {
var messageData = {
recipient: {
id: sender
},
message: {
attachment: {
type: "image",
payload: {
url: congifURL + "/assets/" + name + ".gif"
}
}
}
};
callSendAPI(messageData);
}
function user_name(sender) {
console.log(sender)
const sessionId = findOrCreateSession(sender);
request.get({
url: 'https://graph.facebook.com/v2.6/' + sender + '?fields=first_name,gender&access_token=' + PAGE_ACCESS_TOKEN,
method: 'GET'
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
// Print out the response body
var user_fields = JSON.parse(body);
console.dir(user_fields);
var kyz = Object.keys(user_fields);
console.dir(user_fields[kyz[0]]);
sessions[sessionId].Name = user_fields[kyz[0]];
} else {
console.log('error', body);
sessions[sessionId].Name = "Facebook User"
}
if (sessions[sessionId].Name == "Facebook User") {
sendTextMessage(sender, "Hey! This is Mayank personal AI Bot. Let's interact and have some fun! 😎 In case you need me, I'm here 24*7 for you 😉 ")
} else {
sendTextMessage(sender, "Hey " + sessions[sessionId].Name + "! Let's interact and have some fun! 😎 In case you need me, I'm here 24*7 for you 😉 ")
}
});
}
function sendTextMessage(sender, messageText) {
var messageData = {
recipient: {
id: sender
},
message: {
text: messageText
}
};
callSendAPI(messageData);
}
function callSendAPI(messageData) {
request({
url: '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." + error);
}
});
}
function removePersistentMenu() {
request({
url: 'https://graph.facebook.com/v2.6/me/thread_settings',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: {
setting_type: "call_to_actions",
thread_state: "existing_thread",
call_to_actions: []
}
}, function (error, response, body) {
console.log(response)
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error removePersistentMenu: ', response.body.error)
}
})
}
function sendStartingMessage(sender) {
var messageData = {
recipient: {
id: sender
},
message: {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [
{
"title": "Click on the buttons to interact!",
"image_url": congifURL + "/assets/the-best.gif",
buttons: [
{
"type": "web_url",
"title": "Career",
"url": "https://www.linkedin.com/in/mayankketangandhi",
"webview_height_ratio": "full"
},
{
"type": "postback",
"title": "Hobbies",
"payload": "hobbies",
},
{
type: "postback",
title: "Fun",
payload: 'fun'
},
]
}
]
}
}
}
};
callSendAPI(messageData)
};
function sendFun(sender) {
var params = [
{
"content_type": "text",
"title": "Memes for Life!",
"payload": 'fun|' + "meme"
},
// {
// "content_type": "text",
// "title": "Jokes",
// "payload": 'fun|' + "joke"
// },
{
"content_type": "text",
"title": "Riddles",
"payload": 'fun|' + "riddle"
},
]
var messageData = {
"text": "Cool! What would you like?",
"quick_replies": params
}
request({
url: 'https://graph.facebook.com/v2.10/me/messages',
qs: {
access_token: process.env.PAGE_ACCESS_TOKEN
},
method: 'POST',
json: {
recipient: {
id: sender
},
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error Test about Site: ', response.body.error)
}
})
}
function continueAfterfun(sender, fun) {
if (fun == "MEME") {
var params = [
{
"content_type": "text",
"title": "Want more?",
"payload": 'fun|' + "meme"
},
{
"content_type": "text",
"title": "Riddles?",
"payload": 'fun|' + "riddle"
},
{
"content_type": "text",
"title": "About Me",
"payload": 'aboutme'
},
]
} else {
var params = [
{
"content_type": "text",
"title": "Want more?",
"payload": 'fun|' + "riddle"
},
{
"content_type": "text",
"title": "Meme?",
"payload": 'fun|' + "meme"
},
{
"content_type": "text",
"title": "About Me",
"payload": 'aboutme'
},
]
}
var messageData = {
"text": "So what next?",
"quick_replies": params
}
request({
url: 'https://graph.facebook.com/v2.10/me/messages',
qs: {
access_token: process.env.PAGE_ACCESS_TOKEN
},
method: 'POST',
json: {
recipient: {
id: sender
},
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error Test about Site: ', response.body.error)
}
})
}
function Image(sender, pic) {
messageData = {
"attachment": {
"type": "image",
"payload": {
"url": congifURL + "/assets/" + pic,
}
}
}
request({
url: 'https://graph.facebook.com/v2.10/me/messages',
qs: {
access_token: process.env.PAGE_ACCESS_TOKEN
},
method: 'POST',
json: {
recipient: {
id: sender
},
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error Test floor Plan Image ?>>: ', response.body.error)
}
})
}
function sendMail(sender) {
var params =
[
{
"content_type": "user_email",
}
]
var messageData = {
"text": "Click on your Email and we will get back to you with all the necessary details.",
"quick_replies": params
}
request({
url: 'https://graph.facebook.com/v2.10/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: {
recipient: { id: sender },
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
function addpersistantMenu() {
request({
url: 'https://graph.facebook.com/v2.6/me/thread_settings',
qs: {
access_token: process.env.PAGE_ACCESS_TOKEN
},
method: 'POST',
json: {
"setting_type": "call_to_actions",
"thread_state": "existing_thread",
"call_to_actions": [
{
"type": "postback",
"title": "Restart Bot 🚀",
"payload": "restartBot"
},
{
"type": "postback",
"title": "About Me",
"payload": 'aboutme'
},
{
"type": "postback",
"title": "Contact Me",
"payload": 'contactus'
},
]
}
}, function (error, response, body) {
console.log(response)
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error addpersistantMenu: ', response.body.error)
}
})
};
function restart_Bot(sender) {
const sessionId = findOrCreateSession(sender)
delete sessions[sessionId];
setTimeout(function () {
user_name(sender);
sendStartingMessage(sender)
}, 1000)
}