diff --git a/src/TwilioChannel.php b/src/TwilioChannel.php index e5cc8f9..edc6ee4 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,13 +80,17 @@ 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 ($message->getTo()) { + return $message->getTo(); + } if ($notifiable->routeNotificationFor(self::class, $notification)) { return $notifiable->routeNotificationFor(self::class, $notification); } 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.