-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
696 lines (657 loc) · 25.2 KB
/
index.js
File metadata and controls
696 lines (657 loc) · 25.2 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
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
/**
* Created by Atanas on 06-Mar-17.
*/
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const fetch = require('node-fetch');
const app = express();
app.set('port', (process.env.PORT || 5000));
//Allow us to process data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//ROUTES
app.get('/', function(req,res) {
res.send("Hi, I'm a chatbot");
});
let token = "EAAa4Wnh0ewUBAFupVsHWsX9tdkUVMBHVZAtJQEF1cLF4qZCguDShji7WvEh7XQZCO0L7itkGjOxpsiWFaD23k8NRDwHEHFjKYvHqJDi0zZAKjV98H3F6lz0MvbuovJn6d92FlfepoYksrRiwikQLRZCKZBiitOqHjtn7FUcZBF2xwZDZD";
//FACEBOOK
app.get('/webhook', function(req, res) {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === "token987") {
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);
}
});
let frame = "";
let city="";
let prevFrame="";
let botID=638196106390731;
app.post('/webhook/', function(req, res) {
let messaging_events = req.body.entry[0].messaging;
for (let i = 0; i < messaging_events.length; i++) {
let event = messaging_events[i];
let sender = event.sender.id;
let payload="";
if (event.message && event.message.text && sender!=botID) {
let text = event.message.text;
decision(sender,text);
}
if (event.postback){
let payload = event.postback.payload;
decision(sender,payload);
}
}
res.sendStatus(200)
});
function decision(sender,text){
let greeting = "Hi, this is cost-comparison bot. The bot can compare the basic cost needs for students in your city and in Blagoevgrad, Bulgaria.\u000A\u000AIf you need help to navigate type 'help'\u000AIf you want to take a step back type 'back'\u000AIf you want to end the conversation type 'quit'\u000A\u000ASo let's go. Do you want to see the price comparisson between your city and Blagoevgrad?";
if(text=="back")
{
switch (frame){
case "greeting":
sendText(sender,"Can't go back.");
break;
case "answer":
frame="";
break;
case "askAdministration":
frame="";
break;
case "city":
frame="greeting";
text="yes";
break;
case "choice1":
frame="answer";
text="chat";
break;
case "choice2":
frame="city";
break;
}
}
if(text=="quit")
{
sendText(sender, "Thank you messaging us. Goodbye");
frame="";
}
if(text.toLowerCase()=="help")
{
sendText(sender,"To go back - type 'back'. " +
"To end conversation - type 'quit'");
}
else {
switch (frame) {
case "":
if(text.toLowerCase()!="quit"){
sendText(sender, greeting);
frame = "greeting";
}
break;
case "greeting":
if (text.toLowerCase() == "yes") {
sendText(sender, "How would you prefer to check the prices?");
sendGenericMessage(sender);
frame = "answer";
}
else if (text.toLowerCase() != "help" && text.toLowerCase()!="back") {
sendText(sender, "Do you have a question to the administration?");
prevFrame = frame;
frame = "askAdministration";
}
break;
case "askAdministration":
if (text.toLowerCase() == "yes") {
sendText(sender, "An admission officer will contact you as soon as possible.");
sendText(sender, "Thank you messaging us. Goodbye");
frame = "";
}
else {
sendText(sender, "Thank you for messaging us. Goodbye.");
frame = "";
}
break;
case "city":
city = text.toLowerCase();
sendText(sender, "What comparison category do you want to see?");
sendGenericMessagePriceType(sender);
frame = "choice1";
break;
case "answer":
if (text == "chat") {
sendText(sender, "Where are you from?");
frame = "city";
}
else if (text.toLowerCase() != "help" && text.toLowerCase() != "back") {
sendText(sender, "Opening website. Thank you messaging us. Goodbye.");
frame = "";
}
break;
case "choice1":
if (text == "restaurants") {
sendGenericMessageRestaurants(sender);
frame = "choice2";
}
else if (text == "markets") {
sendGenericMessageMarkets(sender);
frame = "choice2";
}
else if (text == "transportation") {
sendGenericMessageTransportation(sender);
frame = "choice2";
}
else if (text == "utilities") {
sendGenericMessageUtilities(sender);
frame = "choice2";
}
else if (text == "sports") {
sendGenericMessageSports(sender);
frame = "choice2";
}
else if (text == "clothing") {
sendGenericMessageClothing(sender);
frame = "choice2";
}
else if (text == "quit") {
sendText(sender, "Thank you messaging us. Goodbye.");
frame = "";
}
else {
sendText(sender, "Cannot recognise answer. Select one of the options or write quit to end the conversation.");
sendGenericMessagePriceType(sender);
}
break;
case "choice2":
let id = "";
if (text == "meal") {
id = 1;
}
else if (text == "McMeal") {
id = 3
}
else if (text == "beer") {
id = 4;
}
else if (text == "cappuccino") {
id = 114;
}
else if (text == "coke") {
id = 6;
}
else if (text == "water") {
id = 7;
}
else if (text == "milk") {
id = 8;
}
else if (text == "cheese") {
id = 12;
}
else if (text == "apples") {
id = 110;
}
else if (text == "potato") {
id = 112;
}
else if (text == "wine") {
id = 14;
}
else if (text == "cigarettes") {
id = 17;
}
else if (text == "ticket") {
id = 18;
}
else if (text == "taxi") {
id = 107;
}
else if (text == "gasoline") {
id = 24;
}
else if (text == "mobile") {
id = 32;
}
else if (text == "internet") {
id = 33;
}
else if (text == "fitness") {
id=40;
}
else if (text == "cinema") {
id = 44;
}
else if (text == "jeans") {
id=60;
}
else if (text == "shoes") {
id=64;
}
Promise.all([fetch("http://cost-comparison.azurewebsites.net/getItem/"+city+"/"+id).then((res) => res.json()),
fetch("http://cost-comparison.azurewebsites.net/getItem/Blagoevgrad/"+id).then((res) => res.json())])
.then(function(json) {
console.log(json);
let cost="";
let price;
if(json[0].average_price>json[1].average_price){
cost="cheaper";
price=(json[0].average_price-json[1].average_price)/json[0].average_price;
}
else if(json[0].average_price<json[1].average_price){
cost="more expensive";
price=(json[1].average_price-json[0].average_price)/json[0].average_price;
}
else{
cost="same price";
price=0;
}
price=price*100;
sendText(sender,"The average price in "+city+" is $"+json[0].average_price.toFixed(2)+"\u000AThe average price is Blagoevgrad is $"+ json[1].average_price.toFixed(2)+"\u000AIn Blagoevgrad is "+cost+" with "+price.toFixed(2)+"%\u000ADo you want to see something else?");
frame = "checkAgain";
});
break;
case "checkAgain":
if (text.toLowerCase() == "yes") {
sendText(sender, "What comparison category do you want to see?");
sendGenericMessagePriceType(sender);
prevFrame = frame;
frame = "choice1";
}
else if (text.toLowerCase() == "no") {
sendText(sender, "Do you have a question to the administration?");
prevFrame = frame;
frame = "askAdministration";
}
else {
sendText(sender, "Cannot recognize answer. Please write yes or no.");
}
break;
}
}
}
function sendText(sender, text) {
let messageData = {text: text};
request({
url: "https://graph.facebook.com/v2.6/me/messages",
qs: {access_token: token},
method: "POST",
json: {
recipient: {id: sender},
message: messageData
}
}), function(error, response, body){
if (error){
console.log("sending error");
}
else if(response.body.error){
console.log("response body error");
}
}
}
function sendGenericMessagePriceType(recipientId){
let messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "Restaurants",
payload: "restaurants"
}, {
type: "postback",
title: "Markets",
payload: "markets",
}, {
type: "postback",
title: "Transportation",
payload: "transportation",
}]
},
{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "Utilities(Monthly)",
payload: "utilities",
}, {
type: "postback",
title: "Sports and Leisure",
payload: "sports",
}, {
type: "postback",
title: "Clothing and Shoes",
payload: "clothing",
}]
}]
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessage(recipientId) {
let messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Cost-comparison",
subtitle: "Check the prices in Blagoevgrad",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "web_url",
url: "http://cost-comparison.azurewebsites.net/home",
title: "Check Prices online"
}, {
type: "postback",
title: "Chat with bot",
payload: "chat"
}],
}]
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessageRestaurants(recipientId){
let messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "Meal Restaurant",
payload: "meal"
}, {
type: "postback",
title: "McMeal at McDonalds",
payload: "McMeal",
}, {
type: "postback",
title: "Domestic Beer",
payload: "beer",
}]
},
{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "Cappuccino",
payload: "cappuccino",
}, {
type: "postback",
title: "Coke/Pepsi",
payload: "coke",
}, {
type: "postback",
title: "Water",
payload: "water",
}]
}]
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessageMarkets(recipientId){
let messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "Milk(1 liter)",
payload: "milk"
}, {
type: "postback",
title: "Local Cheese (1kg)",
payload: "cheese",
}, {
type: "postback",
title: "Apples (1kg)",
payload: "apples",
}]
},
{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "Potato (1kg)",
payload: "potato",
}, {
type: "postback",
title: "Bottle of Wine",
payload: "wine",
}, {
type: "postback",
title: "Pack of Cigarettes",
payload: "cigarettes",
}]
}]
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessageTransportation(recipientId){
let messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "Ticket (Local Transport)",
payload: "ticket"
}, {
type: "postback",
title: "Taxi(Normal Tariff)",
payload: "taxi",
}, {
type: "postback",
title: "Gasoline (1 liter)",
payload: "gasoline",
}]
}]
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessageUtilities(recipientId){
let messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "1 min. Mobile Tariff",
payload: "mobile"
}, {
type: "postback",
title: "Internet(10 Mbps)",
payload: "internet",
}]
}]
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessageSports(recipientId){
let messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "Fitness Club",
payload: "fitness"
}, {
type: "postback",
title: "Cinema",
payload: "cinema",
}]
}]
}
}
}
};
callSendAPI(messageData);
}
function sendGenericMessageClothing(recipientId){
let messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Categories",
subtitle: "Choose a category",
item_url: "http://nodeci.azurewebsites.net/",
image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/American_University_in_Bulgaria.jpg/1200px-American_University_in_Bulgaria.jpg",
buttons: [{
type: "postback",
title: "Pair of Jeans",
payload: "jeans"
}, {
type: "postback",
title: "Nike Shoes",
payload: "shoes",
}]
}]
}
}
}
};
callSendAPI(messageData);
}
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: token },
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
let recipientId = body.recipient_id;
let 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);
}
});
}
const httpJSONRequest = function (url) {
return new Promise(function (resolve, reject) {
console.log(url);
let xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open('GET', url, true);
xhr.send();
xhr.addEventListener("readystatechange", processRequest, false);
function processRequest(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
let response = JSON.parse(xhr.responseText);
resolve(response);
} else if (xhr.readyState == 4) {
console.log(xhr.readyState);
console.log(xhr.status);
reject(Error("Something went wrong with the request \n\t\t\t\t\t XHR Status: " + xhr.status));
}
}
});
};
app.listen(app.get('port'),function(res,req) {
console.log("running: port");
});