-
Notifications
You must be signed in to change notification settings - Fork 2
/
Messenger.php
620 lines (493 loc) · 18.8 KB
/
Messenger.php
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
<?php
// parameters
require_once("keys.php");
// check token at setup
if ($_REQUEST['hub_verify_token'] === $hubVerifyToken) {
echo $_REQUEST['hub_challenge'];
exit;
}
// handle bot's anwser
$input = json_decode(file_get_contents('php://input'), true);
$senderId = $input['entry'][0]['messaging'][0]['sender']['id'];
$messageText = $input['entry'][0]['messaging'][0]['message']['text'];
$optinMsg = $input['entry'][0]['messaging'][0]['optin']['ref'];
$isteamvalid = ""; // this variable will indicate if a team exist. I found out that when you request the status of a team with the TBA nightbot http request, it tells you if the team exist or not.
// These lines are getting the name of the user
$userinfojs = file_get_contents("https://graph.facebook.com/v2.6/{$senderId}?fields=first_name&access_token={$accessToken}");
$userinfo = json_decode($userinfojs);
$susername = $userinfo->{'first_name'};
$currentyear = '2017';
$answer = "I don't understand. Type 'help' for a list of commands!";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
// If the user clicked on a "Send to messenger button"
if (strpos($optinMsg, 'subscribe') !== false) {
//emulate a subscribe command
$optinsubteam = filter_var($optinMsg, FILTER_SANITIZE_NUMBER_INT);
$messageText = "subscribe {$optinsubteam}";
}
//Random crap
if(strpos(strtolower($messageText), 'shut up') !== false) {
$answer = "...";
$response = [
'recipient' => [ 'id' => $senderId ],
'sender_action' => 'mark_seen'
];
}
if ((strpos(strtolower($messageText), 'ok') !== false) || (strpos(strtolower($messageText), "\xf0\x9f\x91\x8d") !== false)) {
$answer = "\xf0\x9f\x91\x8d"; // Thumbs Up Emoji
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if (strpos(strtolower($messageText), 'ping') !== false) {
$answer = "Pong!";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if ((strpos(strtolower($messageText), 'water') !== false) && (strpos(strtolower($messageText), "game") !== false)) {
$answer = "Water Game"; // Thumbs Up Emoji
$wgimages = ['http://frcbot.com/chatbot/data/wg/1.jpg', 'http://frcbot.com/chatbot/data/wg/2.jpg', 'http://frcbot.com/chatbot/data/wg/3.jpg'];
$wgurl = $wgimages[mt_rand(0, 2)];
$response = array (
'recipient' =>
array (
'id' => $senderId,
),
'message' =>
array (
'attachment' =>
array (
'type' => 'image',
'payload' =>
array (
'url' => $wgurl,
),
),
),
);
}
if (strpos(strtolower($messageText), 'bye') !== false) {
$answer = "Goodbye {$susername}! Ttyl :)";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if (strpos(strtolower($messageText), 'thank') !== false) {
$answer = "You're welcome {$susername}!";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if ((strpos(strtolower($messageText), 'why') !== false) || (strpos(strtolower($messageText), 'what') !== false)) {
$answer = "I don't know! Type 'help' for a list of commands!";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if ((strpos(strtolower($messageText), 'what up') !== false) || (strpos(strtolower($messageText), "what's up") !== false)) {
$answer = "Nothing much!";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if ((strpos(strtolower($messageText), 'what') !== false) && (strpos(strtolower($messageText), 'your') !== false) && (strpos(strtolower($messageText), "favorite") !== false)) {
$answer = "I don't know! How about you?";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
if (strpos(strtolower($messageText), 'team') !== false) {
$answer = "";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if (strpos(strtolower($messageText), 'website') !== false) {
$answer = "My favorite websites are frcbot.com and thebluealliance.com !";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
}
if (strpos(strtolower($messageText), 'teaser') !== false) {
$answer = "You can take a look at the 2017 FIRST STEAMWORKS Teaser here: https://www.youtube.com/watch?v=37GBEBLfhWA";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
// Bot commands
if ((strpos(strtolower($messageText), 'hi') !== false) || (strpos(strtolower($messageText), 'hello') !== false) || (strpos(strtolower($messageText), 'hey') !== false)) {
$answer = "Hello {$susername}! I'm the Facebook ChatBot for FRC__Bot! Type 'help' for a list of commands!";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if (strpos(strtolower($messageText), 'help') !== false) {
$response = array (
'recipient' =>
array (
'id' => $senderId,
),
'message' =>
array (
'attachment' =>
array (
'type' => 'image',
'payload' =>
array (
'url' => 'https://frc-bot.github.io/FRCBot-images/messenger/frcbothelp.png',
),
),
),
);
}
if ((strpos(strtolower($messageText), 'social') !== false) || (strpos(strtolower($messageText), 'facebook') !== false) || (strpos(strtolower($messageText), 'twitter') !== false) || (strpos(strtolower($messageText), 'youtube') !== false) || (strpos(strtolower($messageText), 'instagram') !== false)) {
$answer = "Right now, I can't give you the social medias of teams but my developers are working on that!";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if(strpos(strtolower($messageText), 'nextmatch') !== false) {
$nmteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$answer = file_get_contents("http://www.thebluealliance.com/_/nightbot/nextmatch/{$nmteam}");
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if(strpos(strtolower($messageText), 'status') !== false) {
$stteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$answer = file_get_contents("http://www.thebluealliance.com/_/nightbot/status/{$stteam}");
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
// This part is based on TheBlueAlliance API https://www.thebluealliance.com/apidocs
if(strpos(strtolower($messageText), 'website') !== false) { //get the website of a team
$nbteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$json_string = file_get_contents("http://www.thebluealliance.com/api/v2/team/frc{$nbteam}?X-TBA-App-Id=frcbot:messengerchatbot:1");
$parsed_json = json_decode($json_string);
$answer = "The website of team {$nbteam} is " . $parsed_json->{'website'};
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
$isteamvalid = file_get_contents("http://www.thebluealliance.com/_/nightbot/status/{$nbteam}"); // this line check if the team exist with a http request to the status nightbot command
}
if(strpos(strtolower($messageText), 'location') !== false) { //get the location of a team
$nbteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$json_string = file_get_contents("http://www.thebluealliance.com/api/v2/team/frc{$nbteam}?X-TBA-App-Id=frcbot:messengerchatbot:1");
$parsed_json = json_decode($json_string);
$answer = "Team {$nbteam} is located in " . $parsed_json->{'location'};
$response = array (
'recipient' =>
array (
'id' => $senderId,
),
'message' =>
array (
'attachment' =>
array (
'type' => 'template',
'payload' =>
array (
'template_type' => 'button',
'text' => $answer,
'buttons' =>
array (
0 =>
array (
'type' => 'web_url',
'url' => "https://www.google.com/maps/place/" . $parsed_json->{'location'},
'title' => "View on Google Maps",
),
),
),
),
),
);
$isteamvalid = file_get_contents("http://www.thebluealliance.com/_/nightbot/status/{$nbteam}"); // this line check if the team exist with a http request to the status nightbot command
}
if(strpos(strtolower($messageText), 'nickname') !== false) { //get the nickname of a team
$nbteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$json_string = file_get_contents("http://www.thebluealliance.com/api/v2/team/frc{$nbteam}?X-TBA-App-Id=frcbot:messengerchatbot:1");
$parsed_json = json_decode($json_string);
$answer = "Team {$nbteam}'s nickname is " . $parsed_json->{'nickname'};
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
$isteamvalid = file_get_contents("http://www.thebluealliance.com/_/nightbot/status/{$nbteam}"); // this line check if the team exist with a http request to the status nightbot command
}
if((strpos(strtolower($messageText), 'sponsors') !== false) || (strpos(strtolower($messageText), 'partner') !== false)) { //get the sponsors of a team
$nbteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$json_string = file_get_contents("http://www.thebluealliance.com/api/v2/team/frc{$nbteam}?X-TBA-App-Id=frcbot:messengerchatbot:1");
$parsed_json = json_decode($json_string);
$answer = "Team {$nbteam}'s sponsors are " . $parsed_json->{'name'};
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
$isteamvalid = file_get_contents("http://www.thebluealliance.com/_/nightbot/status/{$nbteam}"); // this line check if the team exist with a http request to the status nightbot command
}
// This part is used to open pages on TheBlueAlliance from a user request
if ((strpos(strtolower($messageText), 'team') !== false) && (strpos(strtolower($messageText), 'page') !== false)) {
$nbteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$response = array (
'recipient' =>
array (
'id' => $senderId,
),
'message' =>
array (
'attachment' =>
array (
'type' => 'template',
'payload' =>
array (
'template_type' => 'button',
'text' => "Click to visit the team page of {$nbteam} on TBA",
'buttons' =>
array (
0 =>
array (
'type' => 'web_url',
'url' => "http://www.thebluealliance.com/team/{$nbteam}",
'title' => "Team {$nbteam} on TBA",
),
),
),
),
),
);
$isteamvalid = file_get_contents("http://www.thebluealliance.com/_/nightbot/status/{$nbteam}"); // this line check if the team exist with a http request to the status nightbot command
}
if((strpos(strtolower($messageText), 'media') !== false) || (strpos(strtolower($messageText), 'picture') !== false)) { //get the pictures of a team
$justnumbers = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT); // remove the text from the command and only keep numbers
$mediayear = substr($justnumbers, 0, 4);
$nbteam = str_replace($mediayear, '', $justnumbers);
$json_string = file_get_contents("http://www.thebluealliance.com/api/v2/team/frc{$nbteam}/{$mediayear}/media?X-TBA-App-Id=frcbot:messengerchatbot:1");
$parsed_json = json_decode($json_string);
$picturelink = "no link";
if ((strpos(strtolower($json_string), 'cdphotothread') !== false) || (strpos(strtolower($json_string), 'imgur') !== false)) {
$nbmedia = count($parsed_json);
if (strpos(strtolower($json_string), 'cdphotothread') !== false) {
for($i=0;$i<$nbmedia;$i++) {
if ($parsed_json[$i]->{'type'} == 'cdphotothread') {
$picturelink = $parsed_json[$i]->{'details'}->{'image_partial'};
}
}
$answer = "http://www.chiefdelphi.com/media/img/{$picturelink}";
}else{
for($i=0;$i<$nbmedia;$i++) {
if ($parsed_json[$i]->{'type'} == 'imgur') {
$picturelink = $parsed_json[$i]->{'foreign_key'};
}
}
$answer = "http://i.imgur.com/{$picturelink}h.jpg";
}
$response = array (
'recipient' =>
array (
'id' => $senderId,
),
'message' =>
array (
'attachment' =>
array (
'type' => 'image',
'payload' =>
array (
'url' => $answer,
),
),
),
);
}else{
$answer = "Team {$nbteam} didn't post any pictures in {$mediayear}";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
$isteamvalid = file_get_contents("http://www.thebluealliance.com/_/nightbot/status/{$nbteam}"); // this line check if the team exist with a http request to the status nightbot command
}
if(strpos(strtolower($messageText), 'rookie') !== false) { //get the rookie year of a team
$nbteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$json_string = file_get_contents("http://www.thebluealliance.com/api/v2/team/frc{$nbteam}?X-TBA-App-Id=frcbot:messengerchatbot:1");
$parsed_json = json_decode($json_string);
$answer = "Team {$nbteam} was created in " . $parsed_json->{'rookie_year'};
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
$isteamvalid = file_get_contents("http://www.thebluealliance.com/_/nightbot/status/{$nbteam}"); // this line check if the team exist with a http request to the status nightbot command
}
if(strpos(strtolower($messageText), 'motto') !== false) { //get the motto of a team
$nbteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$json_string = file_get_contents("http://www.thebluealliance.com/api/v2/team/frc{$nbteam}?X-TBA-App-Id=frcbot:messengerchatbot:1");
$parsed_json = json_decode($json_string);
$answer = $parsed_json->{'motto'};
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
$isteamvalid = file_get_contents("http://www.thebluealliance.com/_/nightbot/status/{$nbteam}"); // this line check if the team exist with a http request to the status nightbot command
}
// events
if(strpos(strtolower($messageText), 'date') !== false) { //get an event date
$event = str_replace('date', '', strtolower($messageText)); // remove eventdate to only keep the event key
$event = str_replace(' ', '', $event); // remove spaces
$evyear = filter_var($event, FILTER_SANITIZE_NUMBER_INT);
$event = str_replace($evyear, '', $event);
if("{$evyear}{$event}" == "{$event}"){
$evyear = $currentyear;
}
$event = file_get_contents("https://frcbot.com/chatbot/scripts/eventbyname.php?eventname={$event}");
$event = "{$evyear}{$event}";
$json_string = file_get_contents("http://www.thebluealliance.com/api/v2/event/{$event}?X-TBA-App-Id=frcbot:messengerchatbot:1");
$parsed_json = json_decode($json_string);
$evname = $parsed_json->{'name'};
$stdate = $parsed_json->{'start_date'};
$enddate = $parsed_json->{'end_date'};
$answer = "{$evname} will Start on {$stdate} and will end on {$enddate}";
if (strpos($json_string, 'name') !== false){}else{ //if there is an error
$answer = "Error: Invalid event name!";
}
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if(strpos(strtolower($messageText), 'ranking') !== false) { //get the ranking of an event
$event = str_replace('rankings', '', strtolower($messageText)); // remove rankings to only keep the event key
$event = str_replace('ranking', '', strtolower($messageText)); // remove ranking to only keep the event key
$event = str_replace(' ', '', $event); // remove spaces
$evyear = filter_var($event, FILTER_SANITIZE_NUMBER_INT);
$event = str_replace($evyear, '', $event);
if("{$evyear}{$event}" == "{$event}"){
$evyear = $currentyear;
}
$event = file_get_contents("https://frcbot.com/chatbot/scripts/eventbyname.php?eventname={$event}");
$event = "{$evyear}{$event}";
$json_string = file_get_contents("https://www.thebluealliance.com/api/v2/event/{$event}?X-TBA-App-Id=frcbot:messengerchatbot:1");
$parsed_json = json_decode($json_string);
$evname = $parsed_json->{'name'};
$answer = "https://www.thebluealliance.com/event/{$event}#rankings";
if(strpos(strtolower($json_string), 'name') !== false) {
$response = array (
'recipient' =>
array (
'id' => $senderId,
),
'message' =>
array (
'attachment' =>
array (
'type' => 'template',
'payload' =>
array (
'template_type' => 'button',
'text' => "Click to see the rankings of {$evname} on TBA",
'buttons' =>
array (
0 =>
array (
'type' => 'web_url',
'url' => $answer,
'title' => "{$event} on TBA",
),
),
),
),
),
);
}else{
$answer = "Error: Invalid event name!";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
}
//notification
if (strpos(strtolower($messageText), 'subscribe') !== false) {
$nbteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$answer = file_get_contents("http://frcbot.com/database/adddata.php?uid={$senderId}&datain=frc{$nbteam}&action=add&tore=team");
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
if (strpos(strtolower($messageText), 'unsubscribe') !== false) {
$nbteam = filter_var($messageText, FILTER_SANITIZE_NUMBER_INT);
$answer = file_get_contents("http://frcbot.com/database/adddata.php?uid={$senderId}&datain=frc{$nbteam}&action=delete&tore=team");
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
//matchs
if (strpos(strtolower($messageText), 'matchtest') !== false) {
$match = str_replace('matchtest', '', strtolower($messageText)); // remove match to only keep the match id
$match = str_replace(' ', '', $match); // remove spaces
$linkused = "";
if(strpos($match, '2016') !== false){
$linkused = "http://frcbot.com/chatbot/matchresultsimg/2016matchresult.php?matchid={$match}";
}else{
$linkused = "http://frcbot.com/chatbot/matchresultsimg/pre2016matchresult.php?matchid={$match}";
}
$response = array (
'recipient' =>
array (
'id' => $senderId,
),
'message' =>
array (
'attachment' =>
array (
'type' => 'image',
'payload' =>
array (
'url' => $linkused,
),
),
),
);
}
if (strpos(strtolower($messageText), 'idtestuser') !== false) {
$answer = "your id is: {$senderId}";
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
}
//this check if the team exist and write an error if it does not exist
if (strpos(strtolower($isteamvalid), 'does not exist') !== false) {
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $isteamvalid ]
];
}
$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token='.$accessToken);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_exec($ch);
curl_close($ch);