Skip to content

Commit

Permalink
Add option to use Twilio's URL shortener (#138)
Browse files Browse the repository at this point in the history
Co-authored-by: atymic <atymicq@gmail.com>
  • Loading branch information
jbajou and atymic authored Feb 21, 2023
1 parent 8cf2074 commit f0128ce
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ TWILIO_ACCOUNT_SID=1234 # always required
TWILIO_FROM=100000000 # optional default from
TWILIO_ALPHA_SENDER=HELLO # optional
TWILIO_DEBUG_TO=23423423423 # Set a number that call calls/messages should be routed to for debugging
TWILIO_SMS_SERVICE_SID=MG0a0aaaaaa00aa00a00a000a00000a00a # Optional but recommended
TWILIO_SMS_SERVICE_SID=MG0a0aaaaaa00aa00a00a000a00000a00a # Optional but recommended
TWILIO_SHORTEN_URLS=true # optional, enable URL shortener
```

### Advanced configuration
Expand Down
1 change: 1 addition & 0 deletions config/twilio-notification-channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

'from' => env('TWILIO_FROM'), // optional
'alphanumeric_sender' => env('TWILIO_ALPHA_SENDER'),
'shorten_urls' => env('TWILIO_SHORTEN_URLS', false), // optional, enable twilio URL shortener

/**
* See https://www.twilio.com/docs/sms/services.
Expand Down
4 changes: 4 additions & 0 deletions src/Twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): Messa
$params['messagingServiceSid'] = $messagingServiceSid;
}

if ($this->config->isShortenUrlsEnabled()) {
$params['ShortenUrls'] = "true";
}

if ($from = $this->getFrom($message)) {
$params['from'] = $from;
}
Expand Down
5 changes: 5 additions & 0 deletions src/TwilioConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ public function isIgnoredErrorCode(int $code): bool

return in_array($code, $this->getIgnoredErrorCodes(), true);
}

public function isShortenUrlsEnabled(): bool
{
return $this->config['shorten_urls'] ?? false;
}
}
22 changes: 22 additions & 0 deletions tests/Unit/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ public function it_can_send_a_sms_message_using_service()
$channel->send(new NotifiableWithAttribute(), $this->notification);
}

/** @test */
public function it_can_send_a_sms_message_using_url_shortener()
{
$message = TwilioSmsMessage::create('Message text');
$this->notification->shouldReceive('toTwilio')->andReturn($message);

$config = new TwilioConfig([
'from' => '+31612345678',
'ShortenUrls' => true,
]);
$twilio = new Twilio($this->twilioService, $config);
$channel = new TwilioChannel($twilio, $this->events);

$this->smsMessageWillBeSentToTwilioWith('+22222222222', [
'from' => '+31612345678',
'body' => 'Message text',
'ShortenUrls' => 'true',
]);

$channel->send(new NotifiableWithAttribute(), $this->notification);
}

/** @test */
public function it_can_send_a_sms_message_using_alphanumeric_sender()
{
Expand Down

0 comments on commit f0128ce

Please sign in to comment.