diff --git a/README.md b/README.md index 1eebad4..202b8fc 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,7 @@ public function routeNotificationForTwilio() - `from('')`: Accepts a phone to use as the notification sender. - `content('')`: Accepts a string value for the notification body. - `messagingServiceSid('')`: Accepts a messaging service SID to handle configuration. +- `sendAt(DateTime)`: Accepts a DateTime object to schedule a message. Only is used if messaging service is set. #### TwilioCallMessage diff --git a/src/Twilio.php b/src/Twilio.php index 76b6d62..7eda0bf 100644 --- a/src/Twilio.php +++ b/src/Twilio.php @@ -2,6 +2,7 @@ namespace NotificationChannels\Twilio; +use DateTime; use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification; use Twilio\Exceptions\TwilioException; use Twilio\Rest\Api\V2010\Account\CallInstance; @@ -74,6 +75,11 @@ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): Messa if ($messagingServiceSid = $this->getMessagingServiceSid($message)) { $params['messagingServiceSid'] = $messagingServiceSid; + + if ($sendAt = $this->getSendAt($message)) { + $params['sendAt'] = $sendAt; + $params['scheduleType'] = 'fixed'; + } } if ($this->config->isShortenUrlsEnabled()) { @@ -171,6 +177,17 @@ protected function getMessagingServiceSid(TwilioSmsMessage $message): ?string return $message->getMessagingServiceSid() ?: $this->config->getServiceSid(); } + /** + * Get the send at time from the message. + * + * @param TwilioSmsMessage $message + * @return DateTime|null + */ + protected function getSendAt(TwilioSmsMessage $message): ?DateTime + { + return $message->getSendAt(); + } + /** * Get the alphanumeric sender from config, if one exists. * diff --git a/src/TwilioSmsMessage.php b/src/TwilioSmsMessage.php index f97c258..5389a9b 100755 --- a/src/TwilioSmsMessage.php +++ b/src/TwilioSmsMessage.php @@ -2,6 +2,8 @@ namespace NotificationChannels\Twilio; +use DateTime; + class TwilioSmsMessage extends TwilioMessage { /** @@ -34,6 +36,11 @@ class TwilioSmsMessage extends TwilioMessage */ public $provideFeedback; + /** + * @var null|DateTime + */ + public $sendAt; + /** * @var null|int */ @@ -145,6 +152,24 @@ public function provideFeedback(bool $provideFeedback): self return $this; } + /** + * Set the date and time at which the message will be sent. + */ + public function sendAt(DateTime $sendAt): self + { + $this->sendAt = $sendAt; + + return $this; + } + + /** + * Get sendAt of this message. + */ + public function getSendAt(): ?DateTime + { + return $this->sendAt; + } + /** * Set the validity period (in seconds). * diff --git a/tests/Unit/TwilioTest.php b/tests/Unit/TwilioTest.php index b32ae2c..5666c1d 100644 --- a/tests/Unit/TwilioTest.php +++ b/tests/Unit/TwilioTest.php @@ -189,6 +189,37 @@ public function it_can_send_a_sms_message_to_twilio_with_messaging_service() $this->twilio->sendMessage($message, '+1111111111'); } + /** @test */ + public function it_can_schedule_a_sms_message_to_twilio_with_messaging_service() + { + $message = new TwilioSmsMessage('Message text'); + + $this->config->shouldReceive('getFrom') + ->once() + ->andReturn('+1234567890'); + + $this->config->shouldReceive('getServiceSid') + ->once() + ->andReturn('service_sid'); + + $this->config->shouldReceive('getDebugTo') + ->once() + ->andReturn(null); + + $this->twilioService->messages->shouldReceive('create') + ->atLeast()->once() + ->with('+1111111111', [ + 'from' => '+1234567890', + 'body' => 'Message text', + 'messagingServiceSid' => 'service_sid', + "sendAt" => new \DateTime('+30 min'), + "scheduleType" => "fixed" + ]) + ->andReturn(Mockery::mock(MessageInstance::class)); + + $this->twilio->sendMessage($message, '+1111111111'); + } + /** @test */ public function it_can_send_a_call_to_twilio() {