-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
345 lines (276 loc) · 8.99 KB
/
index.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
<?php
use pimax\FbBotApp;
use pimax\Messages\Message;
use pimax\Messages\ImageMessage;
use pimax\UserProfile;
use pimax\Messages\MessageButton;
use pimax\Messages\QuickReply;
use pimax\Messages\StructuredMessage;
use pimax\Messages\MessageElement;
use pimax\Messages\MessageReceiptElement;
use pimax\Messages\Address;
use pimax\Messages\Summary;
use pimax\Messages\Adjustment;
use pimax\Messages\AccountLink;
require_once(dirname(__FILE__) . '/lib.php');
$HANDLE_BAD_WORDS = true;
$verify_token = ""; // Verify token
$token = ""; // Page token
$log = true; // Log user entries
if (file_exists(__DIR__.'/config.php') && empty($verify_token) && empty($token)) {
$config = include __DIR__.'/config.php';
$verify_token = $config['verify_token'];
$token = $config['token'];
$log = $config['log'];
}
$BadWordsAnswer = getBadWordAnswers();
require_once(dirname(__FILE__) . '/vendor/autoload.php');
// Make Bot Instance
$bot = new FbBotApp($token);
// Receive something
if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] == 'subscribe' && $_REQUEST['hub_verify_token'] == $verify_token) {
// Webhook setup request
echo $_REQUEST['hub_challenge'];
} else {
// Other event
$data = json_decode(file_get_contents("php://input"), true, 512, JSON_BIGINT_AS_STRING);
if (!empty($data['entry'][0]['messaging'])) {
foreach ($data['entry'][0]['messaging'] as $message) {
// Skipping delivery messages
if (!empty($message['delivery'])) {
continue;
}
// skip the echo of my own messages
if ((@$message['message']['is_echo'] == "true")) {
continue;
}
$command = "";
// When bot receive message from user
if (!empty($message['message'])) {
// $command = $message['message']['text'];
$command = strtolower(remove_accents(remove_emoji($message['message']['text'])));
if ($HANDLE_BAD_WORDS && haveBadWords($command)) {
$command = "BAD_WORDS";
}
// When bot receive button click from user
} else if (!empty($message['postback'])) {
$command = $message['postback']['payload'];
}
if (!empty($command) && $log) {
logInput($message['sender']['id'], $message);
}
// Handle command
switch ($command) {
case 'text':
case 'text message':
typing();
$bot->send(new Message($message['sender']['id'], 'We got your text message'));
break;
case 'image':
case 'image message':
typing();
$bot->send(new ImageMessage($message['sender']['id'], 'https://scontent.ftun3-1.fna.fbcdn.net/v/t1.0-9/10410431_315620208595783_2872781494615039522_n.jpg?oh=30f79c848c693932d7d60974c80b0633&oe=5997F412'));
break;
// When bot receive "profile"
case 'profile':
case 'my profile':
case 'me':
typing();
$user = $bot->userProfile($message['sender']['id']);
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_GENERIC,
[
'elements' => [
new MessageElement($user->getFirstName()." ".$user->getLastName(), "Hey profile", $user->getPicture())
]
]
));
break;
//delete the bot context menu
case 'delete menu':
typing();
$bot->deletePersistentMenu();
break;
// When bot receive "options"
case 'options':
case 'buttons':
typing();
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_BUTTON,
[
'text' => 'Choose an option',
'buttons' => [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First option'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Second option'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Third option')
]
]
));
break;
// When bot receive "generic"
case 'generic':
case 'generic options':
typing();
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_GENERIC,
[
'elements' => [
new MessageElement("First option", "Option description", "", [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First option'),
new MessageButton(MessageButton::TYPE_WEB, 'Web link', 'http://facebook.com')
]),
new MessageElement("Second option", "Option description", "", [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First option'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Second option')
]),
new MessageElement("Third option", "Option description", "", [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First option'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Second option')
])
]
]
));
break;
// When bot receive "receipt"
case 'receipt':
case 'buy':
typing();
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_RECEIPT,
[
'recipient_name' => 'Your name',
'order_number' => rand(10000, 99999),
'currency' => 'TND',
'payment_method' => 'VISA',
'order_url' => 'http://facebook.com',
'timestamp' => time(),
'elements' => [
new MessageReceiptElement("1st item", "1st item description", "", 1, 300, "TND"),
new MessageReceiptElement("2nd item", "2nd item description", "", 2, 200, "TND"),
new MessageReceiptElement("3rd item", "3rd item description", "", 3, 1800, "TND"),
],
'address' => new Address([
'country' => 'TN',
'postal_code' => 1337,
'city' => 'Tunis',
'street_1' => '1, ThunderWay',
'street_2' => ''
]),
'summary' => new Summary([
'subtotal' => 3200,
'shipping_cost' => 170,
'total_tax' => 20,
'total_cost' => 2420,
]),
'adjustments' => [
new Adjustment([
'name' => 'You got Discount',
'amount' => 12
]),
new Adjustment([
'name' => '$25 Off Coupon Discount',
'amount' => 25
])
]
]
));
break;
case 'menu':
typing();
$bot->setPersistentMenu([
new MessageButton(MessageButton::TYPE_WEB, "1st link", "https://github.com/jamaity-tn/ThunderBot-Boilerplate"),
new MessageButton(MessageButton::TYPE_WEB, "2nd link", "https://github.com/jamaity-tn/ThunderBot-Boilerplate")
]);
break;
case 'login':
typing();
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_GENERIC,
[
'elements' => [
new AccountLink(
'Welcome to My Online Service',
'You are going to login with your Online Service Account',
'https://www.service.com/oauth/authorize',
'https://www.facebook.com/images/fb_icon_325x325.png')
]
]
));
break;
case 'logout':
typing();
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_GENERIC,
[
'elements' => [
new AccountLink(
'Welcome to My Online Service',
'You are going to login out with your Online Service Account',
'',
'https://www.facebook.com/images/fb_icon_325x325.png',
TRUE)
]
]
));
break;
// Politesse
case 'thanks':
case 'thx':
case 'merci':
case '3aychik':
case 'thank you':
case 'cool':
typing();
$bot->send(new Message($message['sender']['id'], '+1'));
break;
// Politesse to insults
case 'BAD_WORDS':
typing();
$bot->send(new Message($message['sender']['id'], $BadWordsAnswer[rand(0,count($BadWordsAnswer)-1)]));
break;
// About
case 'about':
case '?':
typing();
$bot->send(new Message($message['sender']['id'], "Welcome to ThunderBot ! 🎉"));
$bot->send(new Message($message['sender']['id'], "With this bot, you can go from 0 to Hero making your simple bot"));
$bot->send(new Message($message['sender']['id'], "Visit https://github.com/jamaity-tn/ThunderBot-Boilerplate to know more"));
break;
// Politesse
case 'i love thunderbot':
case 'i <3 you':
case 'i <3 u':
case 'LOVE_YOU_PAYLOAD':
typing();
$bot->send(new Message($message['sender']['id'], 'We love you too ! <3'));
break;
break;
// Get started (onboarding)
case 'hi':
case 'hey':
case 'hello':
case 'cc':
case 'salut':
case 'demarrer':
case 'bonjour':
case 'welcome':
case 'bienvenue':
case 'comment demarrer?':
case 'comment demarrer':
case 'GET_STARTED_PAYLOAD':
typing();
$user = $bot->userProfile($message['sender']['id']);
$bot->send(new Message($message['sender']['id'], 'Welcome '.$user->getFirstName(). " !"));
$bot->send(new Message($message['sender']['id'], "I'm ThunderBot, nice to meet you"));
break;
// Other message received
default:
if (!empty($command)){
// otherwise "empty message" wont be understood either
$bot->send(new Message($message['sender']['id'], 'Thank you for your message'));
$bot->send(new Message($message['sender']['id'], "Wait for people to answer you, we are not smart enough to do it in realtime"));
}
}
}
}
}