From a938e5352006f4406fee474c342ddacb5eedab18 Mon Sep 17 00:00:00 2001 From: Sky Bit <43996164+skybitbbsr@users.noreply.github.com> Date: Wed, 3 Mar 2021 14:54:25 +0530 Subject: [PATCH 1/3] override to number in message Add method in message base class to override to number in message --- src/TwilioMessage.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/TwilioMessage.php b/src/TwilioMessage.php index 8e44f8a..f9e1a44 100755 --- a/src/TwilioMessage.php +++ b/src/TwilioMessage.php @@ -17,6 +17,14 @@ abstract class TwilioMessage * @var string */ public $from; + + /** + * The phone number the message should be sent to. + * This is optional. If available, will override $notifiable phone number + * + * @var string + */ + public $to; /** * @var null|string @@ -83,6 +91,29 @@ public function getFrom(): ?string { return $this->from; } + + /** + * Set the phone number the message should be sent to. + * + * @param string $to + * @return $this + */ + public function to(string $to): self + { + $this->to = $to; + + return $this; + } + + /** + * Get the to address. + * + * @return string|null + */ + public function getTo(): ?string + { + return $this->to; + } /** * Set the status callback. From 15bada345194c329093f27384033287e67699537 Mon Sep 17 00:00:00 2001 From: Sky Bit <43996164+skybitbbsr@users.noreply.github.com> Date: Wed, 3 Mar 2021 15:00:37 +0530 Subject: [PATCH 2/3] use to number from message if defined --- src/TwilioChannel.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/TwilioChannel.php b/src/TwilioChannel.php index e5cc8f9..7871c4f 100755 --- a/src/TwilioChannel.php +++ b/src/TwilioChannel.php @@ -44,9 +44,7 @@ public function __construct(Twilio $twilio, Dispatcher $events) public function send($notifiable, Notification $notification) { try { - $to = $this->getTo($notifiable, $notification); $message = $notification->toTwilio($notifiable); - $useSender = $this->canReceiveAlphanumericSender($notifiable); if (is_string($message)) { $message = new TwilioSmsMessage($message); @@ -56,6 +54,9 @@ public function send($notifiable, Notification $notification) throw CouldNotSendNotification::invalidMessageObject($message); } + $to = $this->getTo($notifiable, $notification, $message); + $useSender = $this->canReceiveAlphanumericSender($notifiable); + return $this->twilio->sendMessage($message, $to, $useSender); } catch (Exception $exception) { $event = new NotificationFailed( @@ -79,15 +80,16 @@ public function send($notifiable, Notification $notification) * Get the address to send a notification to. * * @param mixed $notifiable - * @param Notification|null $notification + * @param Notification $notification + * @param TwilioMessage $message * * @return mixed * @throws CouldNotSendNotification */ - protected function getTo($notifiable, $notification = null) + protected function getTo($notifiable, $notification, $message) { - if ($notifiable->routeNotificationFor(self::class, $notification)) { - return $notifiable->routeNotificationFor(self::class, $notification); + if ($message->getTo()) { + return $message->getTo(); } if ($notifiable->routeNotificationFor('twilio', $notification)) { return $notifiable->routeNotificationFor('twilio', $notification); From 8c625b3c0846406a961cd19ab5d4f1805fce10f6 Mon Sep 17 00:00:00 2001 From: Surya Kanta Date: Wed, 3 Mar 2021 15:13:03 +0530 Subject: [PATCH 3/3] added removed logic --- src/TwilioChannel.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/TwilioChannel.php b/src/TwilioChannel.php index 7871c4f..edc6ee4 100755 --- a/src/TwilioChannel.php +++ b/src/TwilioChannel.php @@ -91,6 +91,9 @@ protected function getTo($notifiable, $notification, $message) if ($message->getTo()) { return $message->getTo(); } + if ($notifiable->routeNotificationFor(self::class, $notification)) { + return $notifiable->routeNotificationFor(self::class, $notification); + } if ($notifiable->routeNotificationFor('twilio', $notification)) { return $notifiable->routeNotificationFor('twilio', $notification); }