-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExampleBot.php
136 lines (124 loc) · 5.8 KB
/
ExampleBot.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
<?php
namespace ChatBot;
use ChatBot\Messages\ReceivedMessage;
use ChatBot\Messages\Message;
use ChatBot\Messages\ImageMessage;
use ChatBot\Messages\StructuredMessage;
use ChatBot\Messages\MessageButton;
use ChatBot\Messages\MessageReceiptElement;
use ChatBot\Messages\Address;
use ChatBot\Messages\Summary;
use ChatBot\Messages\Adjustment;
class ExampleBot extends BotApplication
{
public function receive($platform, ReceivedMessage $message)
{
switch ($message->getText())
{
// When bot receive "text"
case 'text':
$this->send(new Message($message->getSender(), 'This is a simple text message.'));
break;
// When bot receive "image"
case 'image':
$this->send(new ImageMessage($message->getSender(), 'https://developers.facebook.com/images/devsite/fb4d_logo-2x.png'));
break;
// When bot receive "profile"
case 'profile':
$user = $this->userProfile($message->getSender());
$this->send(new StructuredMessage($message->getSender(),
StructuredMessage::TYPE_GENERIC,
[
'elements' => [
new MessageElement($user->getFirstName()." ".$user->getLastName(), " ", $user->getPicture())
]
]
));
break;
// When bot receive "button"
case 'button':
$this->send(new StructuredMessage($message->getSender(),
StructuredMessage::TYPE_BUTTON,
[
'text' => 'Choose category',
'buttons' => [
new MessageButton(MessageButton::TYPE_POSTBACK, 'First button'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Second button'),
new MessageButton(MessageButton::TYPE_POSTBACK, 'Third button')
]
]
));
break;
// When bot receive "items"
case 'items':
$this->send(new StructuredMessage($message->getSender(),
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')
])
]
]
));
break;
// When bot receive "receipt"
case 'receipt':
if ($platform == self::PLATFORM_FB) {
$this->send(new StructuredMessage($message->getSender(),
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;
// Other message received
default:
$this->send(new Message($message->getSender(), 'Sorry. I don’t understand you.'));
}
}
}