From c16b25fca4c316d4fa0cc89295abc36be3236857 Mon Sep 17 00:00:00 2001 From: James Wagoner Date: Tue, 4 Jun 2024 12:58:44 -0500 Subject: [PATCH] support message scheduling if messaging service is used --- phpunit.xml.dist | 55 +++++++++++++++++---------------------- src/Twilio.php | 17 ++++++++++++ src/TwilioSmsMessage.php | 26 ++++++++++++++++++ tests/Unit/TwilioTest.php | 31 ++++++++++++++++++++++ 4 files changed, 98 insertions(+), 31 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cc5cee4..96c9c8c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,33 +1,26 @@ - - - - tests/Unit - - - tests/Integration - - - - - src/ - - - - - - - - - - + + + + + + + + + + + tests/Unit + + + tests/Integration + + + + + + + + src/ + + 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..f4f7a96 100755 --- a/src/TwilioSmsMessage.php +++ b/src/TwilioSmsMessage.php @@ -2,6 +2,9 @@ namespace NotificationChannels\Twilio; +use DateTime; +use Illuminate\Support\Facades\Date; + class TwilioSmsMessage extends TwilioMessage { /** @@ -34,6 +37,11 @@ class TwilioSmsMessage extends TwilioMessage */ public $provideFeedback; + /** + * @var null|DateTime|string + */ + public $sendAt; + /** * @var null|int */ @@ -145,6 +153,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() {