From 1c7dcbbce84ceeaf1168694a2edbee86dd63aecb Mon Sep 17 00:00:00 2001 From: Alex Whiteside <1505496+alexw23@users.noreply.github.com> Date: Sat, 14 Dec 2024 01:27:03 +1100 Subject: [PATCH 1/3] Added support for content templates (i.e. whatsapp) --- src/Twilio.php | 7 +++++ src/TwilioContentTemplateMessage.php | 44 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 src/TwilioContentTemplateMessage.php diff --git a/src/Twilio.php b/src/Twilio.php index a764712..97b76bd 100644 --- a/src/Twilio.php +++ b/src/Twilio.php @@ -89,6 +89,13 @@ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): Messa ]); } + if ($message instanceof TwilioContentTemplateMessage) { + $this->fillOptionalParams($params, $message, [ + 'contentSid', + 'contentVariables', + ]); + } + return $this->twilioService->messages->create($to, $params); } diff --git a/src/TwilioContentTemplateMessage.php b/src/TwilioContentTemplateMessage.php new file mode 100755 index 0000000..8167a1b --- /dev/null +++ b/src/TwilioContentTemplateMessage.php @@ -0,0 +1,44 @@ +<?php + +namespace NotificationChannels\Twilio; + +class TwilioContentTemplateMessage extends TwilioSmsMessage +{ + /** + * The SID of the content template (starting with H) + * @var null|string + */ + public $contentSid; + + /** + * The variables to replace in the content template + * @var null|array|string + */ + public $contentVariables; + + /** + * Set the content sid (starting with H). + * + * @param string $contentSid + * @return $this + */ + public function contentSid(string $contentSid): self + { + $this->contentSid = $contentSid; + + return $this; + } + + /** + * Set the content variables. + * + * @param array $contentVariables The variables to replace in the content template (i.e. ['1' => 'John Doe']) + * @return $this + */ + public function contentVariables(array $contentVariables): self + { + $this->contentVariables = json_encode($contentVariables); + + return $this; + } +} From f5e834a15e82016ed110b85f804152e00d5d4fac Mon Sep 17 00:00:00 2001 From: Alex Whiteside <1505496+alexw23@users.noreply.github.com> Date: Sat, 14 Dec 2024 01:29:14 +1100 Subject: [PATCH 2/3] Added documentation --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 7710444..6876b79 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,32 @@ class AccountApproved extends Notification } ``` +You can also send using Content Templates: + +``` php +use NotificationChannels\Twilio\TwilioChannel; +use NotificationChannels\Twilio\TwilioContentTemplateMessage; +use Illuminate\Notifications\Notification; + +class AccountApproved extends Notification +{ + public function via($notifiable) + { + return [TwilioChannel::class]; + } + + public function toTwilio($notifiable) + { + return (new TwilioContentTemplateMessage()) + ->contentSid("HXXXXXXXXXXXXXXXXXXXXXXXX") + ->contentVariables([ + '1' => 'John Doe', + '2' => 'ACME Inc.', + ]); + } +} +``` + Or create a Twilio call: ``` php From 74dd90cee2f4e8465a4f5193a93dd617d1653ad7 Mon Sep 17 00:00:00 2001 From: Alex Whiteside <1505496+alexw23@users.noreply.github.com> Date: Sat, 14 Dec 2024 01:32:06 +1100 Subject: [PATCH 3/3] Added clarification about whatsapp --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6876b79..f2b2f48 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,8 @@ class AccountApproved extends Notification } ``` +*Note: if sending via WhatsApp, you must add `whatsapp:` to the beginning of the phone number (i.e. `->from('whatsapp:+61428000382')`). The number must also be approved as a [WhatsApp Sender](https://www.twilio.com/console/sms/whatsapp/senders).* + Or create a Twilio call: ``` php