1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger; |
4
|
|
|
|
5
|
|
|
use Tgallice\FBMessenger\Exception\ApiException; |
6
|
|
|
use Tgallice\FBMessenger\Model\Attachment; |
7
|
|
|
use Tgallice\FBMessenger\Model\Attachment\Template; |
8
|
|
|
use Tgallice\FBMessenger\Model\Button; |
9
|
|
|
use Tgallice\FBMessenger\Model\Message; |
10
|
|
|
use Tgallice\FBMessenger\Model\MessageResponse; |
11
|
|
|
use Tgallice\FBMessenger\Model\ThreadSetting; |
12
|
|
|
use Tgallice\FBMessenger\Model\ThreadSetting\GreetingText; |
13
|
|
|
use Tgallice\FBMessenger\Model\ThreadSetting\StartedButton; |
14
|
|
|
use Tgallice\FBMessenger\Model\ThreadSetting\DomainWhitelisting; |
15
|
|
|
use Tgallice\FBMessenger\Model\UserProfile; |
16
|
|
|
use Tgallice\FBMessenger\Model\WhitelistedDomains; |
17
|
|
|
|
18
|
|
|
class Messenger |
19
|
|
|
{ |
20
|
|
|
use ResponseHandler; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Client $client |
29
|
|
|
*/ |
30
|
18 |
|
public function __construct(Client $client) |
31
|
|
|
{ |
32
|
18 |
|
$this->client = $client; |
33
|
18 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $recipient |
37
|
|
|
* @param string|Message|Attachment|Template $message |
38
|
|
|
* @param string $notificationType |
39
|
|
|
* |
40
|
|
|
* @return MessageResponse |
41
|
|
|
* |
42
|
|
|
* @throws ApiException |
43
|
|
|
*/ |
44
|
5 |
|
public function sendMessage($recipient, $message, $notificationType = NotificationType::REGULAR) |
45
|
|
|
{ |
46
|
5 |
|
$message = $this->createMessage($message); |
47
|
4 |
|
$options = RequestOptionsFactory::createForMessage($recipient, $message, $notificationType); |
48
|
4 |
|
$response = $this->client->send('POST', '/me/messages', null, [], [], $options); |
49
|
4 |
|
$responseData = $this->decodeResponse($response); |
50
|
|
|
|
51
|
4 |
|
return new MessageResponse($responseData['recipient_id'], $responseData['message_id']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $recipient |
56
|
|
|
* @param string $typingIndicator |
57
|
|
|
*/ |
58
|
1 |
|
public function setTypingStatus($recipient, $typingIndicator) { |
59
|
1 |
|
$options = RequestOptionsFactory::createForTyping($recipient, $typingIndicator); |
60
|
1 |
|
$this->client->send('POST', '/me/messages', null, [], [], $options); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $userId |
65
|
|
|
* @param array $fields |
66
|
|
|
* |
67
|
|
|
* @return UserProfile |
68
|
|
|
*/ |
69
|
1 |
|
public function getUserProfile( |
70
|
|
|
$userId, |
71
|
|
|
array $fields = [ |
72
|
|
|
UserProfile::FIRST_NAME, |
73
|
|
|
UserProfile::LAST_NAME, |
74
|
|
|
UserProfile::PROFILE_PIC, |
75
|
|
|
UserProfile::LOCALE, |
76
|
|
|
UserProfile::TIMEZONE, |
77
|
|
|
UserProfile::GENDER, |
78
|
|
|
UserProfile::PAYMENT_ENABLED, |
79
|
|
|
] |
80
|
|
|
) { |
81
|
|
|
$query = [ |
82
|
1 |
|
'fields' => implode(',', $fields) |
83
|
1 |
|
]; |
84
|
|
|
|
85
|
1 |
|
$response = $this->client->get(sprintf('/%s', $userId), $query); |
86
|
1 |
|
$data = $this->decodeResponse($response); |
87
|
|
|
|
88
|
1 |
|
return UserProfile::create($data); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Subscribe the app to the page |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
1 |
|
public function subscribe() |
97
|
|
|
{ |
98
|
1 |
|
$response = $this->client->post('/me/subscribed_apps'); |
99
|
1 |
|
$decoded = $this->decodeResponse($response); |
100
|
|
|
|
101
|
1 |
|
return $decoded['success']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $domains |
106
|
|
|
* @param string $action |
107
|
|
|
*/ |
108
|
1 |
|
public function setDomainWhitelisting($domains, $action = 'add') |
109
|
|
|
{ |
110
|
1 |
|
$domainWhitelisting = new DomainWhitelisting($domains, $action); |
111
|
1 |
|
$setting = $this->buildSetting(ThreadSetting::TYPE_DOMAIN_WHITELISTING, null, $domainWhitelisting, true); |
112
|
|
|
|
113
|
1 |
|
$this->postThreadSettings($setting); |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
1 |
|
public function getDomainWhitelisting() |
120
|
|
|
{ |
121
|
|
|
$query = [ |
122
|
|
|
'fields' => DomainWhitelisting::WHITELISTED_DOMAINS |
123
|
1 |
|
]; |
124
|
|
|
|
125
|
1 |
|
$response = $this->client->get('/me/thread_settings', $query); |
126
|
1 |
|
$data = $this->decodeResponse($response); |
127
|
|
|
|
128
|
1 |
|
return WhitelistedDomains::create($data); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $text |
133
|
|
|
*/ |
134
|
1 |
|
public function setGreetingText($text) |
135
|
|
|
{ |
136
|
1 |
|
$greeting = new GreetingText($text); |
137
|
1 |
|
$setting = $this->buildSetting(ThreadSetting::TYPE_GREETING, null, $greeting); |
138
|
|
|
|
139
|
1 |
|
$this->postThreadSettings($setting); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $payload |
144
|
|
|
*/ |
145
|
1 |
|
public function setStartedButton($payload) |
146
|
|
|
{ |
147
|
1 |
|
$startedButton = new StartedButton($payload); |
148
|
1 |
|
$setting = $this->buildSetting( |
149
|
1 |
|
ThreadSetting::TYPE_CALL_TO_ACTIONS, |
150
|
1 |
|
ThreadSetting::NEW_THREAD, |
151
|
1 |
|
[$startedButton] |
152
|
1 |
|
); |
153
|
|
|
|
154
|
1 |
|
$this->postThreadSettings($setting); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
1 |
|
public function deleteStartedButton() |
158
|
|
|
{ |
159
|
1 |
|
$setting = $this->buildSetting( |
160
|
1 |
|
ThreadSetting::TYPE_CALL_TO_ACTIONS, |
161
|
|
|
ThreadSetting::NEW_THREAD |
162
|
1 |
|
); |
163
|
|
|
|
164
|
1 |
|
$this->deleteThreadSettings($setting); |
165
|
1 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param Button[] $menuButtons |
169
|
|
|
*/ |
170
|
2 |
|
public function setPersistentMenu(array $menuButtons) |
171
|
|
|
{ |
172
|
2 |
|
if (count($menuButtons) > 5) { |
173
|
1 |
|
throw new \InvalidArgumentException('You should not set more than 5 menu items.'); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
$setting = $this->buildSetting( |
177
|
1 |
|
ThreadSetting::TYPE_CALL_TO_ACTIONS, |
178
|
1 |
|
ThreadSetting::EXISTING_THREAD, |
179
|
|
|
$menuButtons |
180
|
1 |
|
); |
181
|
|
|
|
182
|
1 |
|
$this->postThreadSettings($setting); |
183
|
1 |
|
} |
184
|
|
|
|
185
|
1 |
|
public function deletePersistentMenu() |
186
|
|
|
{ |
187
|
1 |
|
$setting = $this->buildSetting( |
188
|
1 |
|
ThreadSetting::TYPE_CALL_TO_ACTIONS, |
189
|
|
|
ThreadSetting::EXISTING_THREAD |
190
|
1 |
|
); |
191
|
|
|
|
192
|
1 |
|
$this->deleteThreadSettings($setting); |
193
|
1 |
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Messenger Factory |
197
|
|
|
* |
198
|
|
|
* @param string $token |
199
|
|
|
* |
200
|
|
|
* @return Messenger |
201
|
|
|
*/ |
202
|
|
|
public static function create($token) |
203
|
|
|
{ |
204
|
|
|
$client = new Client($token); |
205
|
|
|
|
206
|
|
|
return new self($client); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param array $setting |
211
|
|
|
*/ |
212
|
4 |
|
private function postThreadSettings(array $setting) |
213
|
|
|
{ |
214
|
4 |
|
$this->client->post('/me/thread_settings', $setting); |
215
|
4 |
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param array $setting |
219
|
|
|
*/ |
220
|
3 |
|
private function deleteThreadSettings(array $setting) |
221
|
|
|
{ |
222
|
3 |
|
$this->client->send('DELETE', '/me/thread_settings', $setting); |
223
|
3 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $type |
227
|
|
|
* @param null|string $threadState |
228
|
|
|
* @param mixed $value |
229
|
|
|
* @param bool |
230
|
|
|
* |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
6 |
|
private function buildSetting($type, $threadState = null, $value = null, $callJsonSerialize = false) |
234
|
|
|
{ |
235
|
|
|
$setting = [ |
236
|
6 |
|
'setting_type' => $type, |
237
|
6 |
|
]; |
238
|
|
|
|
239
|
6 |
|
if (!empty($threadState)) { |
240
|
4 |
|
$setting['thread_state'] = $threadState; |
241
|
4 |
|
} |
242
|
|
|
|
243
|
|
|
//For the custom payload format... |
244
|
6 |
|
if($callJsonSerialize) { |
245
|
1 |
|
return array_merge($setting, $value->jsonSerialize()); |
246
|
|
|
} |
247
|
|
|
|
248
|
5 |
|
if (!empty($value)) { |
249
|
3 |
|
$setting[$type] = $value; |
250
|
3 |
|
} |
251
|
|
|
|
252
|
5 |
|
return $setting; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string|Message|Attachment|Template $message |
257
|
|
|
* |
258
|
|
|
* @return Message |
259
|
|
|
*/ |
260
|
5 |
|
private function createMessage($message) |
261
|
|
|
{ |
262
|
5 |
|
if ($message instanceof Message) { |
263
|
1 |
|
return $message; |
264
|
|
|
} |
265
|
|
|
|
266
|
4 |
|
if ($message instanceof Template) { |
267
|
1 |
|
$message = new Attachment(Attachment::TYPE_TEMPLATE, $message); |
268
|
1 |
|
} |
269
|
|
|
|
270
|
4 |
|
if (is_string($message) || $message instanceof Attachment) { |
271
|
3 |
|
return new Message($message); |
272
|
|
|
} |
273
|
|
|
|
274
|
1 |
|
throw new \InvalidArgumentException('$message should be a string, Message, Attachment or Template'); |
275
|
|
|
} |
276
|
|
|
|
277
|
1 |
|
public function deleteGreetingText() |
278
|
|
|
{ |
279
|
|
|
$setting = [ |
280
|
|
|
'setting_type' => 'greeting' |
281
|
1 |
|
]; |
282
|
|
|
|
283
|
1 |
|
$this->deleteThreadSettings($setting); |
284
|
1 |
|
} |
285
|
|
|
} |
286
|
|
|
|