-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathindex.php
435 lines (382 loc) · 18.9 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
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
<?php
$verify_token = ""; // Verify token
$token = ""; // Page token
if (file_exists(__DIR__ . '/config.php')) {
$config = include __DIR__ . '/config.php';
$verify_token = $config['verify_token'];
$token = $config['token'];
}
require_once(dirname(__FILE__) . '/vendor/autoload.php');
use pimax\FbBotApp;
use pimax\Menu\MenuItem;
use pimax\Menu\LocalizedMenu;
use pimax\Messages\Message;
use pimax\Messages\MessageButton;
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;
use pimax\Messages\ImageMessage;
use pimax\Messages\QuickReply;
use pimax\Messages\QuickReplyButton;
use pimax\Messages\SenderAction;
// Make Bot Instance
$bot = new FbBotApp($token);
if (!empty($_REQUEST['local'])) {
$message = new ImageMessage(1585388421775947, dirname(__FILE__).'/fb4d_logo-2x.png');
$message_data = $message->getData();
$message_data['message']['attachment']['payload']['url'] = 'fb4d_logo-2x.png';
echo '<pre>', print_r($message->getData()), '</pre>';
$res = $bot->send($message);
echo '<pre>', print_r($res), '</pre>';
}
// 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 = trim($message['message']['text']);
// When bot receive button click from user
} else if (!empty($message['postback'])) {
$text = "Postback received: ".trim($message['postback']['payload']);
$bot->send(new Message($message['sender']['id'], $text));
continue;
}
// Handle command
switch ($command) {
// When bot receive "text"
case 'text':
$bot->send(new Message($message['sender']['id'], 'This is a simple text message.'));
break;
// When bot receive "image"
case 'image':
$bot->send(new ImageMessage($message['sender']['id'], 'http://bit.ly/2p9WZBi'));
break;
// When bot receive "local image"
//case 'local image':
//$bot->send(new ImageMessage($message['sender']['id'], dirname(__FILE__).'/fb_logo.png'));
//break;
// When bot receive "profile"
case 'profile':
$user = $bot->userProfile($message['sender']['id']);
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_GENERIC,
[
'elements' => [
new MessageElement($user->getFirstName()." ".$user->getLastName(), " ", $user->getPicture())
]
],
[
new QuickReplyButton(QuickReplyButton::TYPE_TEXT, 'QR button','PAYLOAD')
]
));
break;
// When bot receive "button"
case 'button':
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_BUTTON,
[
'text' => 'Choose category',
'buttons' => [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First button', 'PAYLOAD 1'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Second button', 'PAYLOAD 2'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Third button', 'PAYLOAD 3')
]
],
[
new QuickReplyButton(QuickReplyButton::TYPE_TEXT, 'QR button','PAYLOAD')
]
));
break;
// When bot receive "quick reply"
case 'quick reply':
$bot->send(new QuickReply($message['sender']['id'], 'Your ad here!',
[
new QuickReplyButton(QuickReplyButton::TYPE_TEXT, 'QR button 1', 'PAYLOAD 1'),
new QuickReplyButton(QuickReplyButton::TYPE_TEXT, 'QR button 2', 'PAYLOAD 2'),
new QuickReplyButton(QuickReplyButton::TYPE_TEXT, 'QR button 3', 'PAYLOAD 3'),
]
));
break;
// When bot receive "location"
case 'location':
$bot->send(new QuickReply($message['sender']['id'], 'Please share your location',
[
new QuickReplyButton(QuickReplyButton::TYPE_LOCATION),
]
));
break;
// When bot receive "generic"
case 'generic':
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_GENERIC,
[
'elements' => [
new MessageElement("First item", "Item description", "", [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First button'),
new MessageButton(MessageButton::TYPE_WEB, 'Web link', 'http://facebook.com')
]),
new MessageElement("Second item", "Item description", "", [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First button'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Second button')
]),
new MessageElement("Third item", "Item description", "", [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First button'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Second button')
])
]
],
[
new QuickReplyButton(QuickReplyButton::TYPE_TEXT, 'QR button','PAYLOAD')
]
));
break;
// When bot receive "list"
case 'list':
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_LIST,
[
'elements' => [
new MessageElement(
'Classic T-Shirt Collection', // title
'See all our colors', // subtitle
'http://bit.ly/2pYCuIB', // image_url
[ // buttons
new MessageButton(MessageButton::TYPE_POSTBACK, // type
'View', // title
'POSTBACK' // postback value
)
]
),
new MessageElement(
'Classic White T-Shirt', // title
'100% Cotton, 200% Comfortable', // subtitle
'http://bit.ly/2pb1hqh', // image_url
[ // buttons
new MessageButton(MessageButton::TYPE_WEB, // type
'View', // title
'https://google.com' // url
)
]
)
],
'buttons' => [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First button', 'PAYLOAD 1')
]
],
[
new QuickReplyButton(QuickReplyButton::TYPE_TEXT, 'QR button','PAYLOAD')
]
));
break;
// When bot receive "receipt"
case 'receipt':
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_RECEIPT,
[
'recipient_name' => 'Fox Brown',
'order_number' => rand(10000, 99999),
'currency' => 'USD',
'payment_method' => 'VISA',
'order_url' => 'http://facebook.com',
'timestamp' => time(),
'elements' => [
new MessageReceiptElement("First item", "Item description", "", 1, 300, "USD"),
new MessageReceiptElement("Second item", "Item description", "", 2, 200, "USD"),
new MessageReceiptElement("Third item", "Item description", "", 3, 1800, "USD"),
],
'address' => new Address([
'country' => 'US',
'state' => 'CA',
'postal_code' => 94025,
'city' => 'Menlo Park',
'street_1' => '1 Hacker Way',
'street_2' => ''
]),
'summary' => new Summary([
'subtotal' => 2300,
'shipping_cost' => 150,
'total_tax' => 50,
'total_cost' => 2500,
]),
'adjustments' => [
new Adjustment([
'name' => 'New Customer Discount',
'amount' => 20
]),
new Adjustment([
'name' => '$10 Off Coupon',
'amount' => 10
])
]
]
));
break;
// When bot receive "set menu"
case 'set menu':
$bot->deletePersistentMenu();
$bot->setPersistentMenu([
new LocalizedMenu('default', false, [
new MenuItem(MenuItem::TYPE_NESTED, 'My Account', [
new MenuItem(MenuItem::TYPE_NESTED, 'History', [
new MenuItem(MenuItem::TYPE_POSTBACK, 'History Old', 'HISTORY_OLD_PAYLOAD'),
new MenuItem(MenuItem::TYPE_POSTBACK, 'History New', 'HISTORY_NEW_PAYLOAD')
]),
new MenuItem(MenuItem::TYPE_POSTBACK, 'Contact Info', 'CONTACT_INFO_PAYLOAD')
])
])
]);
break;
// When bot receive "delete menu"
case 'delete menu':
$bot->deletePersistentMenu();
break;
// When bot receive "login"
case 'login':
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_GENERIC,
[
'elements' => [
new AccountLink(
'Welcome to Bank',
'To be sure, everything is safe, you have to login to your administration.',
'https://www.example.com/oauth/authorize',
'https://www.facebook.com/images/fb_icon_325x325.png')
]
]
));
break;
// When bot receive "logout"
case 'logout':
$bot->send(new StructuredMessage($message['sender']['id'],
StructuredMessage::TYPE_GENERIC,
[
'elements' => [
new AccountLink(
'Welcome to Bank',
'To be sure, everything is safe, you have to login to your administration.',
'',
'https://www.facebook.com/images/fb_icon_325x325.png',
TRUE)
]
]
));
break;
// When bot receive "sender action on"
case 'sender action on':
$bot->send(new SenderAction($message['sender']['id'], SenderAction::ACTION_TYPING_ON));
break;
// When bot receive "sender action off"
case 'sender action off':
$bot->send(new SenderAction($message['sender']['id'], SenderAction::ACTION_TYPING_OFF));
break;
// When bot receive "set get started button"
case 'set get started button':
$bot->setGetStartedButton('PAYLOAD - get started button');
break;
// When bot receive "delete get started button"
case 'delete get started button':
$bot->deleteGetStartedButton();
break;
// When bot receive "show greeting text"
case 'show greeting text':
$response = $bot->getGreetingText();
$text = "";
if(isset($response['data'][0]['greeting']) AND is_array($response['data'][0]['greeting'])){
foreach ($response['data'][0]['greeting'] as $greeting)
{
$text .= $greeting['locale']. ": ".$greeting['text']."\n";
}
} else {
$text = "Greeting text not set!";
}
$bot->send(new Message($message['sender']['id'], $text));
break;
// When bot receive "delete greeting text"
case 'delete greeting text':
$bot->deleteGreetingText();
break;
// When bot receive "set greeting text"
case 'set greeting text':
$bot->setGreetingText([
[
"locale" => "default",
"text" => "Hello {{user_full_name}}"
],
[
"locale" => "en_US",
"text" => "Hi {{user_first_name}}, welcome to this bot."
],
[
"locale" => "de_DE",
"text" => "Hallo {{user_first_name}}, herzlich willkommen."
]
]);
break;
// When bot receive "set target audience"
case 'show target audience':
$response = $bot->getTargetAudience();
break;
// When bot receive "set target audience"
case 'set target audience':
$bot->setTargetAudience("all");
//$bot->setTargetAudience("none");
//$bot->setTargetAudience("custom", "whitelist", ["US", "CA"]);
//$bot->setTargetAudience("custom", "blacklist", ["US", "CA"]);
break;
// When bot receive "delete target audience"
case 'delete target audience':
$bot->deleteTargetAudience();
break;
// When bot receive "show domain whitelist"
case 'show domain whitelist':
$response = $bot->getDomainWhitelist();
$text = "";
if(isset($response['data'][0]['whitelisted_domains']) AND is_array($response['data'][0]['whitelisted_domains'])){
foreach ($response['data'][0]['whitelisted_domains'] as $domains)
{
$text .= $domains."\n";
}
} else {
$text = "No domains in whitelist!";
}
$bot->send(new Message($message['sender']['id'], $text));
break;
// When bot receive "set domain whitelist"
case 'set domain whitelist':
//$bot->setDomainWhitelist("https://petersfancyapparel.com");
$bot->setDomainWhitelist([
"https://petersfancyapparel-1.com",
"https://petersfancyapparel-2.com",
]);
break;
// When bot receive "delete domain whitelist"
case 'delete domain whitelist':
$bot->deleteDomainWhitelist();
break;
// Other message received
default:
if (!empty($command)) // otherwise "empty message" wont be understood either
$bot->send(new Message($message['sender']['id'], 'Sorry. I don’t understand you.'));
}
}
}
}