Skip to content

Commit f8a4f46

Browse files
committed
Add messaging type, recipient type and helpers for notification type.
Closes #32
1 parent 54719cb commit f8a4f46

File tree

1 file changed

+120
-12
lines changed

1 file changed

+120
-12
lines changed

src/FacebookMessage.php

Lines changed: 120 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use JsonSerializable;
66
use NotificationChannels\Facebook\Traits\HasButtons;
77
use NotificationChannels\Facebook\Exceptions\CouldNotCreateMessage;
8-
use NotificationChannels\Facebook\Enums\{AttachmentType, NotificationType};
8+
use NotificationChannels\Facebook\Enums\{MessagingType, RecipientType, AttachmentType, NotificationType};
99

1010
/**
1111
* Class FacebookMessage.
@@ -17,12 +17,18 @@ class FacebookMessage implements JsonSerializable
1717
/** @var string Recipient's ID. */
1818
public $recipient;
1919

20+
/** @var string Recipient Type */
21+
public $recipientType = RecipientType::ID;
22+
2023
/** @var string Notification Text. */
2124
public $text;
2225

2326
/** @var string Notification Type */
2427
public $notificationType = NotificationType::REGULAR;
2528

29+
/** @var string Messaging Type. Defaults to UPDATE */
30+
protected $messagingType = MessagingType::UPDATE;
31+
2632
/** @var array Generic Template Cards (items) */
2733
public $cards = [];
2834

@@ -38,6 +44,9 @@ class FacebookMessage implements JsonSerializable
3844
/** @var bool */
3945
protected $hasText = false;
4046

47+
/** @var string Message tag used with messaging type MESSAGE_TAG */
48+
protected $messageTag;
49+
4150
/**
4251
* @param string $text
4352
*
@@ -67,16 +76,20 @@ public function __construct(string $text = '')
6776
* The id must be an ID that was retrieved through the
6877
* Messenger entry points or through the Messenger webhooks.
6978
*
70-
* @param string $recipient ID of recipient or Phone number of the recipient
71-
* with the format +1(212)555-2368
72-
*
73-
* @param string $type Recipient Type: id, user_ref, phone_number, post_id, comment_id.
79+
* @param string|array $recipient ID of recipient or Phone number of the recipient with the format
80+
* +1(212)555-2368
81+
* @param string $type Recipient Type: id, user_ref, phone_number, post_id, comment_id.
7482
*
7583
* @return $this
7684
*/
77-
public function to(string $recipient, string $type = 'id'): self
85+
public function to($recipient, string $type = RecipientType::ID): self
7886
{
79-
$this->recipient = [$type => $recipient];
87+
if (is_array($recipient)) {
88+
[$type, $recipient] = $recipient;
89+
}
90+
91+
$this->recipient = $recipient;
92+
$this->recipientType = $type;
8093

8194
return $this;
8295
}
@@ -161,6 +174,81 @@ public function notificationType(string $notificationType): self
161174
return $this;
162175
}
163176

177+
/**
178+
* Helper to set notification type as REGULAR.
179+
*
180+
* @return $this
181+
*/
182+
public function isTypeRegular(): self
183+
{
184+
$this->notificationType = NotificationType::REGULAR;
185+
186+
return $this;
187+
}
188+
189+
/**
190+
* Helper to set notification type as SILENT_PUSH.
191+
*
192+
* @return $this
193+
*/
194+
public function isTypeSilentPush(): self
195+
{
196+
$this->notificationType = NotificationType::SILENT_PUSH;
197+
198+
return $this;
199+
}
200+
201+
/**
202+
* Helper to set notification type as NO_PUSH.
203+
*
204+
* @return $this
205+
*/
206+
public function isTypeNoPush(): self
207+
{
208+
$this->notificationType = NotificationType::NO_PUSH;
209+
210+
return $this;
211+
}
212+
213+
/**
214+
* Helper to set messaging type as RESPONSE.
215+
*
216+
* @return $this
217+
*/
218+
public function isResponse(): self
219+
{
220+
$this->messagingType = MessagingType::RESPONSE;
221+
222+
return $this;
223+
}
224+
225+
/**
226+
* Helper to set messaging type as UPDATE.
227+
*
228+
* @return $this
229+
*/
230+
public function isUpdate(): self
231+
{
232+
$this->messagingType = MessagingType::UPDATE;
233+
234+
return $this;
235+
}
236+
237+
/**
238+
* Helper to set messaging type as MESSAGE_TAG.
239+
*
240+
* @param $messageTag
241+
*
242+
* @return $this
243+
*/
244+
public function isMessageTag($messageTag): self
245+
{
246+
$this->messagingType = MessagingType::MESSAGE_TAG;
247+
$this->messageTag = $messageTag;
248+
249+
return $this;
250+
}
251+
164252
/**
165253
* Add up to 10 cards to be displayed in a carousel.
166254
*
@@ -214,7 +302,7 @@ public function toArray(): array
214302
}
215303

216304
if ($this->hasText) {
217-
//check if it has buttons
305+
// check if it has buttons
218306
if (count($this->buttons) > 0) {
219307
return $this->buttonMessageToArray();
220308
}
@@ -237,9 +325,14 @@ public function toArray(): array
237325
protected function textMessageToArray(): array
238326
{
239327
$message = [];
240-
$message['recipient'] = $this->recipient;
328+
$message['recipient'][$this->recipientType] = $this->recipient;
241329
$message['notification_type'] = $this->notificationType;
242330
$message['message']['text'] = $this->text;
331+
$message['messaging_type'] = $this->messagingType;
332+
333+
if (filled($this->messageTag)) {
334+
$message['tag'] = $this->messageTag;
335+
}
243336

244337
return $message;
245338
}
@@ -252,10 +345,15 @@ protected function textMessageToArray(): array
252345
protected function attachmentMessageToArray(): array
253346
{
254347
$message = [];
255-
$message['recipient'] = $this->recipient;
348+
$message['recipient'][$this->recipientType] = $this->recipient;
256349
$message['notification_type'] = $this->notificationType;
257350
$message['message']['attachment']['type'] = $this->attachmentType;
258351
$message['message']['attachment']['payload']['url'] = $this->attachmentUrl;
352+
$message['messaging_type'] = $this->messagingType;
353+
354+
if (filled($this->messageTag)) {
355+
$message['tag'] = $this->messageTag;
356+
}
259357

260358
return $message;
261359
}
@@ -268,11 +366,16 @@ protected function attachmentMessageToArray(): array
268366
protected function genericMessageToArray(): array
269367
{
270368
$message = [];
271-
$message['recipient'] = $this->recipient;
369+
$message['recipient'][$this->recipientType] = $this->recipient;
272370
$message['notification_type'] = $this->notificationType;
273371
$message['message']['attachment']['type'] = 'template';
274372
$message['message']['attachment']['payload']['template_type'] = 'generic';
275373
$message['message']['attachment']['payload']['elements'] = $this->cards;
374+
$message['messaging_type'] = $this->messagingType;
375+
376+
if (filled($this->messageTag)) {
377+
$message['tag'] = $this->messageTag;
378+
}
276379

277380
return $message;
278381
}
@@ -285,12 +388,17 @@ protected function genericMessageToArray(): array
285388
protected function buttonMessageToArray(): array
286389
{
287390
$message = [];
288-
$message['recipient'] = $this->recipient;
391+
$message['recipient'][$this->recipientType] = $this->recipient;
289392
$message['notification_type'] = $this->notificationType;
290393
$message['message']['attachment']['type'] = 'template';
291394
$message['message']['attachment']['payload']['template_type'] = 'button';
292395
$message['message']['attachment']['payload']['text'] = $this->text;
293396
$message['message']['attachment']['payload']['buttons'] = $this->buttons;
397+
$message['messaging_type'] = $this->messagingType;
398+
399+
if (filled($this->messageTag)) {
400+
$message['tag'] = $this->messageTag;
401+
}
294402

295403
return $message;
296404
}

0 commit comments

Comments
 (0)