-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
3653 lines (3452 loc) · 97.6 KB
/
server.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const app = express();
const deasync = require("deasync");
const cheerio = require("cheerio");
const sleep = require("sleep");
app.set("port", process.env.PORT || 5000);
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// Process application/json
app.use(bodyParser.json());
// Index route
app.get("/", function(req, res) {
res.send("Hello world, I am a chat bot");
}); //ruk bhai
// for Facebook verification
app.get("/webhook/", function(req, res) {
if (req.query["hub.verify_token"] === "my_voice_is_my_password_verify_me") {
res.send(req.query["hub.challenge"]);
}
res.send("Error, wrong token");
});
//url for all matches
var url = "http://cricapi.com/api/cricket";
let allmatches;
// Spin up the server
app.listen(app.get("port"), function() {
console.log("running on port", app.get("port"));
});
//message identification
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 = req.body.entry[0].messaging[i];
let sender = event.sender.id;
if (event.message && event.message.text) {
let text = event.message.text;
var messageText=text.toLowerCase();
console.log("\n \n \n \n Message recieved " + text);
if (messageText) {
switch(messageText){
case 'saiful':
broadcastMessage () ;
break;
case 'pak':case 'pakistan':case 'pakistan score':case 'pak score':
pakistanScore(sender) ;
break;
case 'ind':case 'india':case 'india score':case 'ind score':
indiaScore(sender) ;
break;
case 'ban':case 'bangladesh':case 'bangladesh score':case 'ban score':
bangladeshScore(sender) ;
break;
case'score': case'live score':
tellScore(sender);
break;
case'bpl':case'bpl score': case'chattogram': case'chittagong':case'dhaka':case'comilla':case'cumilla':case'sylhet':case'khulna':case'rangpur':case'rajshahi':
bplScore(sender);
break;
/*case'india':case'india score':case'ind': */
default:
// tellScore(sender, "null");
// typing(sender);
sendTextMessage(sender,"Please Choose below which team is playing now otherwise you can Also choose Random Score from menu button .Stay with us for all live score. Or you can Also choose user manual for know how to use CLS Bot.");
sendButton(sender, "null");
break;
}
}
} else if (event.postback) {
let text = event.postback.payload;
console.log("\n \n \n \n Postback recieved " + text);
if (text.indexOf("more") > -1) {
var ind = text.indexOf("^");
var tex = text.substr(ind + 1);
text = text.replace("more scores ", "").toLowerCase();
text = text.replace("scores", "").toLowerCase();
var idandsummary = text.toLowerCase().replace("more ", "");
var index = idandsummary.indexOf("^"); // Gets the first index where a space occours
var id = idandsummary.substr(0, index); // Gets the first part
//var tex = idandsummary.substr(index + 1)
// typing(sender);
tellScoredetails(sender, id, tex);
} else if (text.indexOf("commentary") > -1) {
text = text.replace("commentary ", "").toLowerCase();
text = text.replace("commentary ", "").toLowerCase();
var idtext = text.toLowerCase().replace("commentary ", "");
var ind = idtext.indexOf("^");
var id = idtext.substr(0, ind);
// typing(sender);
getCommentry(sender, id);
} else if (text.indexOf("refresh") > -1) {
// typing(sender);
tellScore(sender, "null");
} else if (text.indexOf("bangladesh") > -1) {
// typing(sender);
bangladeshScore(sender);
} else if (text.indexOf("india") > -1) {
indiaScore(sender);
} else if (text.indexOf("pakistan") > -1) {
pakistanScore(sender);
} else if (text.indexOf("srilanka") > -1) {
srilankaScore(sender);
} else if (text.indexOf("africa") > -1) {
africaScore(sender);
} else if (text.indexOf("windies") > -1) {
windiesScore(sender);
} else if (text.indexOf("australia") > -1) {
australiaScore(sender);
} else if (text.indexOf("newzealand") > -1) {
newzealandScore(sender);
} else if (text.indexOf("england") > -1) {
englandScore(sender);
} else if (text.indexOf("afghan") > -1) {
afghanScore(sender);
} else if (text.indexOf("zimbabwe") > -1) {
zimbabweScore(sender);
} else if (text.indexOf("usermanual")> -1){
sendTextMessage(sender,"CLS Bot is another cricket live score teller bot . And there we will discuss How to easily you can use your favourite CLS Bot free .\n 👉👉Step 1: On the team selection Button you can choose any team who are playing this time otherwise its show other matches .👍 \n👉👉 Step 2: if you want to any cricket match scorecard you need to select Random Score from Menu Option .👍 \n 👉👉 Step 3: After that you need to select Show Details Option For those match full summary.👍 \n👉👉Step 4: After select your favorite Match you need to refresh Everytime when you want those match updates 👍 \n👉👉Step 5: yes💪 This is the best feature of your CLS Bot that you can see last 5 balls commentary by click the LAST FIVE BALLS option. \nIF you still Don't Understand please Watch the below Video.That's it✌✌ \n IF YOU Want to SEND ANY FEEDBACK YOU CAN EMAIL US FROM REQUEST TO ADMIN Option.\n🙏 Please Share this video with your friend.");
sendVideo(sender);
} else if (text.indexOf("admin")>-1) {
sendTextMessage(sender,"Thanks for your feedback Please give Your feedback to this email [email protected] ");
} else if (text.indexOf("mornotice")>-1) {
sendTextMessage(sender,"Please Don't forget to Like CLS Bot -Cricket Live Score Page https://www.facebook.com/clsbot");
} else if (text.indexOf("sendphoto") > -1) {
sendphoto(sender);
} else if (text.indexOf("bpl")>-1){
bplScore(sender);
} else if (text.indexOf("name")>-1){
sendTextMessage(sender,"BPL Team Name:\n [Short Name--Full Name] \n (1) CW--Cumilla Warriors \n(2) RPR--Rangpur Riders \n(3) CGC--Chattogram Challengers \n(4) SYT--Sylhet Thunder \n(5) DP--Dhaka Platoon \n (6) KLT--Khulna Tigers \n (7) RSR--Rajshahi Royals ");
} else if (text.indexOf("generic")>-1){
sendGenericMessage(sender);
}
else if (text.indexOf("getstarted") > -1){
sendTextMessage(sender,'Hey, Whats up? Welome to Cricket live score teller bot. please choose which team is playing now otherwise choose Random score .');
sendButton(sender);
}
}
}
res.sendStatus(200);
});
// all matches init
function init() {
allmatches = undefined;
request(
{
url: "http://m.cricbuzz.com/cricket-match/live-scores"
},
function(error, response, html) {
if (!error) {
allmatches = cheerio.load(html);
// console.log("\n \n got html \n\n" + allmatches);
}
}
);
deasync.loopWhile(function() {
return allmatches === undefined;
});
}
//page token
const token =
"EAAgVilpcMScBABkLhurTx5L98zaXNZCuEX850ggGilZA7LqKeZC7T5swJoVfeK6ZB3knt7GaJd42aQocxXvLkekmDgZAetZAO5NDxsouw2FSGPRYTIP6ROXEgGEhFMdn4140jSF9Wzbj4bsVtkBMGKESRD6HfLT5P00GGlOjLPi0Wd8Wn9jU2ZBSwUAYuN2VZAMZD";
var t = [];
//Bangladesh
function getAll() {
console.log(
"getting all matches............................................."
);
allmatches = undefined;
request(
{
url: "http://m.cricbuzz.com/cricket-match/live-scores"
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
allmatches = body;
//console.log("\n \n getting match details json"+JSON.stringify(allmatches));
}
}
);
deasync.loopWhile(function() {
return allmatches === undefined;
});
var headers = [];
var index = 0;
var i;
var temp = {};
var maintemp = null;
var messageData = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: []
}
}
};
if (allmatches !== undefined) {
var c = 0;
var headers = [];
var links = [];
var result = [];
var short_summary = [];
var short = [];
var id = [];
var scount = 0;
allmatches = cheerio.load(allmatches);
allmatches("h4").each(function() {
headers.push(allmatches(this).text());
});
//console.log('\n\n'+headers.length+'\n\n');
allmatches(".cbz-ui-status").each(function() {
result.push(allmatches(this).text());
});
allmatches(".cbz-ui-status").each(function() {
short_summary.push(allmatches(this).text());
scount = scount + 1;
});
allmatches(".btn-group.cbz-btn-group").each(function() {
var a = allmatches(this)
.children()
.first()
.attr("href");
links.push(a);
// console.log(a);
});
//for(i=0;i<scount;i+2){
//short.push(short_summary[i]+' '+short_summary[i+1]);
// }
/* console.log(
"\n c length " +
c +
"\nlinks length " +
links.length +
" " +
short.length +
" " +
result.length +
" " +
headers.length
); */
for (a = 0; a < links.length; a++) {
var idtext = links[a].replace("/cricket-commentary/", "");
var idresult = idtext.split("/", 1);
id.push(idresult[0]);
}
for (i = 0; i < headers.length; i++) {
// console.log("inside header");
var title = headers[i].toLowerCase();
// console.log("\n Teams playing are " + title);
var imagetext = headers[i].replace(/ /g, "+");
var indext = imagetext.indexOf(",");
var imagetxt = imagetext.substr(0, indext);
var imaget=imagetxt.replace(/ /g,"+");
// console.log(imagetxt);
// console.log(indext);
temp = {
title: headers[i],
image_url: "http://placehold.it/1024x512/0084ff/f7f7f7?text="+imaget,
subtitle : result[i],
text: short[i],
buttons: [
{
type: "postback",
payload: "refresh",
title: "Refresh ..."
},
{
type: "postback",
payload: "more scores " + id[i] + "^" + result[i],
title: "Show Score Details..."
}
]
};
// console.log(temp.title);
c = c + 1;
if (temp.title.indexOf("Ban") > -1) maintemp = temp;
else if (
temp.title.indexOf("Aus vs ") > -1 ||
temp.title.indexOf('Aus')>-1 ||
temp.title.indexOf("Eng vs ") > -1 ||
temp.title.indexOf('Eng')>-1 ||
temp.title.indexOf("Pak vs ") > -1 ||
temp.title.indexOf('Ind')>-1 ||
temp.title.indexOf('Pak')>-1 ||
temp.title.indexOf("SL vs ") > -1 ||
temp.title.indexOf('SL')>-1 ||
temp.title.indexOf("RSA vs ") > -1 ||
temp.title.indexOf('RSa')>-1 ||
temp.title.indexOf("WI vs ") > -1 ||
temp.title.indexOf('WI')>-1 ||
temp.title.indexOf("Ind vs ") > -1 ||
temp.title.indexOf("NZ vs ") > -1 ||
temp.title.indexOf('NZ')>-1 ||
temp.title.indexOf("Zim vs ") > -1 ||
temp.title.indexOf('Zim')>-1 ||
temp.title.indexOf("Afg vs ") > -1 ||
temp.title.indexOf('Afg')>-1
) {
t.unshift(temp);
} else t[i] = temp;
}
if (maintemp != null) {
t.unshift(maintemp);
}
// console.log("\n\nmatch index is " + index);
// console.log(a + index);
if (index < c) {
for (var a = 0; a < 10; a++) {
// console.log("\n \n data added in message is " + t[a + index] + "\n");
if (t[index + a] === undefined || a + index == c) break;
else messageData.attachment.payload.elements[a] = t[index + a];
}
} else {
messageData = {
text: "No more matches are played lately"
};
}
console.log(
"\n \n \n \n the data to be sent for all " + JSON.stringify(messageData)
);
return messageData;
} else {
temp = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
{
title: "Couldn't get any Matches, Retry!",
image_url:
"http://placehold.it/1024x512/0084ff/f7f7f7?text=No+Matches",
subtitle: "Scores",
buttons: [
{
type: "postback",
payload: "refresh",
title: "Retry ..."
}
]
}
]
}
}
};
return temp;
}
}
//getting commentry
//var comment;
let allcommentry = [];
function getCommentry(sender, id) {
var commentary = [];
var cLength = 0;
var comment = undefined;
request(
{
url: "http://m.cricbuzz.com/cricket-commentary/" + id
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
comment = body;
//console.log("\n \n getting match details json"+JSON.stringify(allmatches));
}
}
);
deasync.loopWhile(function() {
return comment === undefined;
});
var e = cheerio.load(comment);
e(".commtext").each(function() {
commentary[cLength] = e(this).text();
cLength = cLength + 1;
});
var result;
e(".cbz-ui-status").each(function() {
result = e(this).text();
});
if (
(result.toLowerCase().indexOf("won") > -1) |
(result.toLowerCase().indexOf("lose") > -1)
) {
sendTextMessage(sender, "The match has finished, \n Top is last ball");
} else sendTextMessage(sender, "Top is last ball");
var messagecount = 0;
for (var i = 0; i < cLength; i++) {
var temp = commentary[i];
if (
(temp.toLowerCase().indexOf("1") === 0) |
(temp.toLowerCase().indexOf("2") === 0) |
(temp.toLowerCase().indexOf("3") === 0) |
(temp.toLowerCase().indexOf("4") === 0) |
(temp.toLowerCase().indexOf("5") === 0) |
(temp.toLowerCase().indexOf("6") === 0) |
(temp.toLowerCase().indexOf("7") === 0) |
(temp.toLowerCase().indexOf("8") === 0) |
(temp.toLowerCase().indexOf("9") === 0)
) {
if (temp.length > 320) {
temp = temp.slice(0, 320);
var t = temp.lastIndexOf(" ");
temp = temp.slice(0, t) + "...";
}
sendTextMessage(sender, temp);
messagecount = messagecount + 1;
if (messagecount === 6) break;
}
deasync.sleep(50); //ye karun kya? messages line se jaate hain isse meesage bhi kam karde 4-5
}
}
//getting single details
let match;
let amma;
function getDetails(id, text) {
console.log("\n\n" + text + "\n\n");
if (text.indexOf("Starts") > -1) {
temp = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
{
title: "The Match hasn't started yet",
image_url:
"http://placehold.it/1024x512/0084ff/f7f7f7?text=Not+Started",
subtitle: text,
buttons: [
{
type: "postback",
payload: "refresh",
title: "Other matches"
}
]
}
]
}
}
};
return temp;
} else {
var titlearr = [];
var title = "";
var textarr = [];
var texts = "";
console.log("getting details.............................................");
match = undefined;
request(
{
url: "http://m.cricbuzz.com/cricket-match-summary/" + id
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
match = body;
// console.log("\n \n getting match details json"+JSON.stringify(allmatches));
}
}
);
deasync.loopWhile(function() {
return match === undefined;
});
var d = cheerio.load(match);
d(".team-totals").each(function() {
titlearr.push(d(this).text());
});
titlearr = titlearr.filter(function(n) {
return n != undefined;
});
// console.log(titlearr);
console.log("\n\n" + titlearr.length + "\n\n");
if (titlearr.length == 1) {
title = titlearr[0];
} else {
title =
titlearr[titlearr.length - 1] + " vs " + titlearr[titlearr.length - 2];
}
//------new---
amma= undefined;
request (
{
url: "http://m.cricbuzz.com/cricket-commentary/"+id
}, function (error, response, body)
{
if (!error && response.statusCode === 200)
{
amma=body;
//console.log("\n \n getting match details json"+JSON.stringify(allmatches));
}
});
deasync.loopWhile(function(){return (amma === undefined);
});
var s=cheerio.load(amma);
s(".table.table-condensed b").each(function() {
texts = texts + s(this).text() + " " + "\n";
});
var score= s("span.ui-bat-team-scores").text();
var core=s("span.ui-bowl-team-scores").text();
var lol=s(".commtext").text();
console.log(score);
console.log(core);
console.log(lol);
var titl=titlearr[titlearr.length - 2]
var tit=score.replace (/ /g,'+');
var lo =lol.replace("Download Cricbuzz mobile App",' ');
// var abbo= score
console.log(texts);
var i = 0;
var temp = {};
var found = false;
if (match !== undefined) {
temp = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
{
title: score +" vs "+ titl,
image_url:
"https://fakeimg.pl/1024x512/0084ff,255/f7f7f7,255/?text="+tit+"&font=bebas&font_size=85",
subtitle:texts,
buttons: [
{
type: "postback",
payload: "more scores " + id + "^" + text,
title: "Refresh ..."
}
]
},
{
title: lo,
image_url:
"http://placehold.it/1024x512/0084ff/f7f7f7?text=Commentary",
subtitle: "Press the button to get last 5 balls ",
buttons: [
{
type: "postback",
payload: "more scores " + id + "^" + text,
title: "Refresh ..."
},
{
type: "postback",
payload: "commentary " + id + "^" + text,
title: "Last Five Balls"
}
]
}
]
}
}
};
return temp;
} else {
temp = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
{
title: "Couldn't get the match Details, Retry!",
image_url:
"hhttp://placehold.it/1024x512/0084ff/f7f7f7?text=No+Details",
subtitle: "Scores",
buttons: [
{
type: "postback",
payload: "more scores " + id + "^" + text,
title: "Retry ..."
}
]
}
]
}
}
};
return temp;
}
}
}
function tellScore(sender) {
let messageData = getAll();
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("Error sending messages: ", error);
} else if (response.body.error) {
console.log("Error: ", response.body.error);
}
}
);
}
function tellScoredetails(sender, id, text) {
let messageData = getDetails(id, text);
console.log("got details");
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("Error sending messages: ", error);
} else if (response.body.error) {
console.log("Error: ", response.body.error);
}
}
);
}
/////////////////
//notice board new
//////////////
function sendGenericMessage(sender) {
var messageData = {
/* recipient: {
id: recipientId
},*/
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [ {
title: " একুশে ফেব্রুয়ারি উপলক্ষ আমাদের নতুন বট Noticebd24.com এ মেসেজ করুন এখনই ",
subtitle: " Noticebd24 বটে পাচ্ছেন নতুন নতুন সব এডুকেশনাল, চাকরির, সাপ্তাহিক চাকরির পত্রিকা এবং বিভিন্ন বই ডাউনলোড সহ বিভিন্ন অফারের নোটিশ ",
item_url: " https://www.facebook.com/noticebd24 ",
image_url: " https://cdn.glitch.com/d3e2a241-36ab-414f-9781-4e47668ac65e%2Fnoticebd24_logo.png?v=1582266849413 ",
buttons: [{
type: "web_url",
url: " https://www.facebook.com/noticebd24 ",
title: "View Page "
},{
type: "web_url",
title: "Send Message",
url: "http://m.me/noticebd24",
}],
} ]
}
}
};
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("Error sending messages: ", error);
} else if (response.body.error) {
console.log("Error: ", response.body.error);
}
}
);
}
function sendTextMessage(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("Error sending messages: ", error);
} else if (response.body.error) {
console.log("Error: ", response.body.error);
}
}
);
}
function sendButton(sender) {
var messageData = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
/* {
"title": "BPL will Start from 11 December 2019 to 18 January 2020.Share CLS Bot more.",
"buttons": [
{
"type": "postback",
"title": "BPL Score",
"payload": "bpl"
},
{
"type":"postback",
"title":"BPL Teams Name",
"payload":"name"
},
{
"type": "postback",
"title": "BPL Schedule",
"payload": "sendphoto"
}
]
}, */{
title: "Select which team is now playing otherwise select Random Score From menu.",
buttons: [
{
type: "postback",
title: "Bangladesh 🇧🇩",
payload: "bangladesh"
},
{
type: "postback",
title: "India 🇮🇳",
payload: "india"
},
{
type: "postback",
title: "Pakistan 🇵🇰",
payload: "pakistan"
}
]
},
{
title: "Select which team is now playing otherwise select Random score.",
buttons: [
{
type: "postback",
title: "Sri Lanka 🇱🇰",
payload: "srilanka"
},
{
type: "postback",
title: "South Africa 🇿🇦",
payload: "africa"
},
{
type: "postback",
title: "West Indies ",
payload: "windies"
}
]
},
{
title: "Select which team is now playing otherwise select Random Score.",
buttons: [
{
type: "postback",
title: "Australia 🇦🇺",
payload: "australia"
},
{
type: "postback",
title: "New Zealand 🇳🇿",
payload: "newzealand"
},
{
type: "postback",
title: "England 🇬🇧",
payload: "england"
}
]
},
{
title: "Select which team is now playing otherwise select Random Score.",
buttons: [
{
type: "postback",
title: "Afghanistan 🇦🇫",
payload: "afghan"
},
{
type: "postback",
title: "Zimbabwe 🇿🇼",
payload: "zimbabwe"
},
{
type: "postback",
title: "IPL will coming",
payload: "bangladesh"
}
]
}
]
}
}
};
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("Error sending messages: ", error);
} else if (response.body.error) {
console.log("Error: ", response.body.error);
}
}
);
}
// Setting persistent menu
var data_start = {
greeting: [
{
locale: 'default',
text: 'Hello {{user_first_name}}, this is your cricket live score teller assistant.This bot is gifted from Saiful islam'
}
]
};
setMessengerProgile(data_start);
var data_starts = {
get_started: {
payload: 'getstarted'
}
};
setMessengerProgile(data_starts);
var data_startd = {
persistent_menu: [
{
locale: 'default',
composer_input_disabled: false,
call_to_actions: [
{
title: 'Notice Board',
type: 'postback',
payload: 'generic'
},
{
title: 'Random Score',
type: 'postback',
payload: 'refresh'
},
{
title: 'User Option',
type: 'nested',
// payload: 'bangladeshScore'
call_to_actions:[
{
title:"User Manual",
type:"postback",
payload:"usermanual"
},
{
title:"Request to Admin",
type:"postback",
payload:"admin"
},
{
title:"More Notice",
type:"postback",
payload:"mornotice",
}
]
}
]
}
]
};
setMessengerProgile(data_startd);
//------------
function setMessengerProgile(messageData){
request({
uri: 'https://graph.facebook.com/v2.6/me/messenger_profile',
qs: { access_token: token},
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
//console.log("Welcome set!!");
} else {
//console.error("Unable to set messenger profile");
}
});
}
//send broadcast messages
function creativeBroadcastMessage(messageData) {
request({
uri: 'https://graph.facebook.com/v2.11/me/message_creatives',
qs: { access_token: token },
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_creative_id;
console.log("Successfully create broadcast message with id %s to recipient %s",
messageId, recipientId);
callSendAPIBroadcast(messageId);
} else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}
//----------
function callSendAPIBroadcast(messageBCID) {
var messageBC = {
message_creative_id: messageBCID,
notification_type: 'REGULAR',
//tag: 'PAIRING_UPDATE'
};
request({
uri: 'https://graph.facebook.com/v2.11/me/broadcast_messages',
qs: { access_token: token },
method: 'POST',
json: messageBC
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.broadcast_id;
console.log("Successfully send broadcast message with id %s to recipient %s",
messageId, recipientId);
} else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}
//--------