Skip to content

Support message scheduling if message service is used #146

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions src/Twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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.
*
Expand Down
25 changes: 25 additions & 0 deletions src/TwilioSmsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace NotificationChannels\Twilio;

use DateTime;

class TwilioSmsMessage extends TwilioMessage
{
/**
Expand Down Expand Up @@ -34,6 +36,11 @@ class TwilioSmsMessage extends TwilioMessage
*/
public $provideFeedback;

/**
* @var null|DateTime
*/
public $sendAt;

/**
* @var null|int
*/
Expand Down Expand Up @@ -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).
*
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/TwilioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down