|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Facebook; |
|
4
|
|
|
|
|
5
|
|
|
use NotificationChannels\Facebook\Exceptions\CouldNotCreateMessage; |
|
6
|
|
|
use NotificationChannels\Facebook\Exceptions\CouldNotSendNotification; |
|
7
|
|
|
use NotificationChannels\Facebook\Enums\AttachmentType; |
|
8
|
|
|
use NotificationChannels\Facebook\Enums\NotificationType; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class FacebookMessage |
|
12
|
|
|
* @package NotificationChannels\Facebook |
|
13
|
|
|
*/ |
|
14
|
|
|
class FacebookMessage implements \JsonSerializable |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string Recipient's ID. */ |
|
17
|
|
|
public $recipient; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string Notification Text. */ |
|
20
|
|
|
public $text; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string Notification Type */ |
|
23
|
|
|
public $notificationType = 'REGULAR'; |
|
24
|
|
|
|
|
25
|
|
|
/** @var array Call to Action Buttons */ |
|
26
|
|
|
public $buttons = []; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array Generic Template Cards (items) */ |
|
29
|
|
|
public $cards = []; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string Notification Type */ |
|
32
|
|
|
public $notification_type = NotificationType::REGULAR; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string Attachment Type |
|
35
|
|
|
* Defaults to File |
|
36
|
|
|
*/ |
|
37
|
|
|
public $attachment_type = AttachmentType::FILE; |
|
38
|
|
|
|
|
39
|
|
|
/** @var string Attachment URL */ |
|
40
|
|
|
public $attachment_url; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var bool |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $has_attachment = false; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $has_text = false; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $text |
|
54
|
|
|
* |
|
55
|
|
|
* @return static |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function create($text = '') |
|
58
|
|
|
{ |
|
59
|
|
|
return new static($text); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $text |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct($text = '') |
|
66
|
|
|
{ |
|
67
|
|
|
if ($text != '') |
|
68
|
|
|
$this->text($text); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Recipient's PSID or Phone Number. |
|
73
|
|
|
* |
|
74
|
|
|
* The id must be an ID that was retrieved through the |
|
75
|
|
|
* Messenger entry points or through the Messenger webhooks. |
|
76
|
|
|
* |
|
77
|
|
|
* @param $recipient ID of recipient or Phone number of the recipient |
|
78
|
|
|
* with the format +1(212)555-2368 |
|
79
|
|
|
* |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function to($recipient) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->recipient = $recipient; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Notification text. |
|
91
|
|
|
* |
|
92
|
|
|
* @param $text |
|
93
|
|
|
* @throws CouldNotCreateMessage |
|
94
|
|
|
* |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
|
|
public function text($text) |
|
98
|
|
|
{ |
|
99
|
|
|
if (!mb_strlen($text) > 320) |
|
100
|
|
|
$this->text = $text; |
|
101
|
|
|
else |
|
102
|
|
|
throw CouldNotCreateMessage::textTooLong(); |
|
103
|
|
|
$this->has_text = true; |
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Add Attachment |
|
109
|
|
|
* |
|
110
|
|
|
* @param $attachment_type |
|
111
|
|
|
* @param $url |
|
112
|
|
|
* @throws CouldNotCreateMessage |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function attach($attachment_type, $url) |
|
117
|
|
|
{ |
|
118
|
|
|
if (in_array($attachment_type, [AttachmentType::FILE, AttachmentType::IMAGE, |
|
119
|
|
|
AttachmentType::VIDEO, AttachmentType::AUDIO])) |
|
120
|
|
|
$this->notificationType = $attachment_type; |
|
121
|
|
|
else |
|
122
|
|
|
throw CouldNotCreateMessage::invalidAttachmentType(); |
|
123
|
|
|
|
|
124
|
|
|
if (isset($url)) |
|
125
|
|
|
$this->attachment_url = $url; |
|
126
|
|
|
else |
|
127
|
|
|
throw CouldNotCreateMessage::urlNotProvided(); |
|
128
|
|
|
|
|
129
|
|
|
$this->has_attachment = true; |
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Push notification type. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $notificationType Possible values: REGULAR, SILENT_PUSH, NO_PUSH |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
|
|
public function notificationType($notificationType = 'REGULAR') |
|
141
|
|
|
{ |
|
142
|
|
|
$this->notificationType = $notificationType; |
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Add up to 3 call to action buttons. |
|
148
|
|
|
* |
|
149
|
|
|
* @param array $buttons |
|
|
|
|
|
|
150
|
|
|
* |
|
151
|
|
|
* @return $this |
|
152
|
|
|
* @throws CouldNotSendNotification |
|
153
|
|
|
*/ |
|
154
|
|
|
public function cards(array $cards = []) |
|
155
|
|
|
{ |
|
156
|
|
|
if (count($cards) > 10) { |
|
157
|
|
|
throw CouldNotCreateMessage::messageCardsLimitExceeded(); |
|
158
|
|
|
} |
|
159
|
|
|
$this->cards = $cards; |
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Add up to 3 call to action buttons. |
|
165
|
|
|
* |
|
166
|
|
|
* @param array $buttons |
|
167
|
|
|
* |
|
168
|
|
|
* @return $this |
|
169
|
|
|
* @throws CouldNotSendNotification |
|
170
|
|
|
*/ |
|
171
|
|
View Code Duplication |
public function buttons(array $buttons = []) |
|
|
|
|
|
|
172
|
|
|
{ |
|
173
|
|
|
if (count($buttons) > 3) { |
|
174
|
|
|
throw CouldNotCreateMessage::messageButtonsLimitExceeded(); |
|
175
|
|
|
} |
|
176
|
|
|
$this->buttons = $buttons; |
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Determine if user id is not given. |
|
182
|
|
|
* |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
|
|
public function toNotGiven() |
|
186
|
|
|
{ |
|
187
|
|
|
return !isset($this->recipient); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Convert the object into something JSON serializable. |
|
192
|
|
|
* |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
|
|
public function jsonSerialize() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->toArray(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns message payload for JSON conversion |
|
202
|
|
|
* @throws CouldNotCreateMessage |
|
203
|
|
|
* @return array |
|
204
|
|
|
*/ |
|
205
|
|
|
public function toArray() |
|
206
|
|
|
{ |
|
207
|
|
|
if ($this->has_attachment) |
|
208
|
|
|
return $this->attachmentMessageToArray(); |
|
209
|
|
|
if ($this->has_text) { |
|
210
|
|
|
//check if has buttons |
|
211
|
|
|
if (count($this->buttons) > 0) { |
|
212
|
|
|
return $this->buttonMessageToArray(); |
|
213
|
|
|
} |
|
214
|
|
|
return $this->textMessageToArray(); |
|
215
|
|
|
} |
|
216
|
|
|
if (count($this->cards) > 0) { |
|
217
|
|
|
return $this->genericMessageToArray(); |
|
218
|
|
|
} |
|
219
|
|
|
throw CouldNotCreateMessage::dataNotProvided(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Returns message for simple text message |
|
224
|
|
|
* @return array |
|
225
|
|
|
*/ |
|
226
|
|
|
protected function textMessageToArray() |
|
227
|
|
|
{ |
|
228
|
|
|
$message = []; |
|
229
|
|
|
$message['recipient'] = $this->recipient; |
|
230
|
|
|
$message['notification_type'] = $this->notificationType; |
|
231
|
|
|
$message['message']['text'] = $this->text; |
|
232
|
|
|
return $message; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Returns message for attachment message |
|
237
|
|
|
* @return array |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function attachmentMessageToArray() |
|
240
|
|
|
{ |
|
241
|
|
|
$message = []; |
|
242
|
|
|
$message['recipient'] = $this->recipient; |
|
243
|
|
|
$message['notification_type'] = $this->notificationType; |
|
244
|
|
|
$message['message']['attachment']['type'] = $this->attachment_type; |
|
245
|
|
|
$message['message']['attachment']['payload']['url'] = $this->attachment_url; |
|
246
|
|
|
return $message; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Returns message for Generic Template message |
|
251
|
|
|
* @return array |
|
252
|
|
|
*/ |
|
253
|
|
|
protected function genericMessageToArray() |
|
254
|
|
|
{ |
|
255
|
|
|
$message = []; |
|
256
|
|
|
$message['recipient'] = $this->recipient; |
|
257
|
|
|
$message['notification_type'] = $this->notificationType; |
|
258
|
|
|
$message['message']['attachment']['type'] = 'template'; |
|
259
|
|
|
$message['message']['attachment']['payload']['template_type'] = 'generic'; |
|
260
|
|
|
$message['message']['attachment']['payload']['elements'] = $this->cards; |
|
261
|
|
|
return $message; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Returns message for Button Template message |
|
266
|
|
|
* @return array |
|
267
|
|
|
*/ |
|
268
|
|
|
protected function buttonMessageToArray() |
|
269
|
|
|
{ |
|
270
|
|
|
$message = []; |
|
271
|
|
|
$message['recipient'] = $this->recipient; |
|
272
|
|
|
$message['notification_type'] = $this->notificationType; |
|
273
|
|
|
$message['message']['attachment']['type'] = 'template'; |
|
274
|
|
|
$message['message']['attachment']['payload']['template_type'] = 'button'; |
|
275
|
|
|
$message['message']['attachment']['payload']['text'] = $this->text; |
|
276
|
|
|
$message['message']['attachment']['payload']['buttons'] = $this->buttons; |
|
277
|
|
|
return $message; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.