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