Skip to content

Commit f0128ce

Browse files
jbajouatymic
andauthored
Add option to use Twilio's URL shortener (#138)
Co-authored-by: atymic <[email protected]>
1 parent 8cf2074 commit f0128ce

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ TWILIO_ACCOUNT_SID=1234 # always required
4444
TWILIO_FROM=100000000 # optional default from
4545
TWILIO_ALPHA_SENDER=HELLO # optional
4646
TWILIO_DEBUG_TO=23423423423 # Set a number that call calls/messages should be routed to for debugging
47-
TWILIO_SMS_SERVICE_SID=MG0a0aaaaaa00aa00a00a000a00000a00a # Optional but recommended
47+
TWILIO_SMS_SERVICE_SID=MG0a0aaaaaa00aa00a00a000a00000a00a # Optional but recommended
48+
TWILIO_SHORTEN_URLS=true # optional, enable URL shortener
4849
```
4950

5051
### Advanced configuration

config/twilio-notification-channel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

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

1213
/**
1314
* See https://www.twilio.com/docs/sms/services.

src/Twilio.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): Messa
7676
$params['messagingServiceSid'] = $messagingServiceSid;
7777
}
7878

79+
if ($this->config->isShortenUrlsEnabled()) {
80+
$params['ShortenUrls'] = "true";
81+
}
82+
7983
if ($from = $this->getFrom($message)) {
8084
$params['from'] = $from;
8185
}

src/TwilioConfig.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,9 @@ public function isIgnoredErrorCode(int $code): bool
7878

7979
return in_array($code, $this->getIgnoredErrorCodes(), true);
8080
}
81+
82+
public function isShortenUrlsEnabled(): bool
83+
{
84+
return $this->config['shorten_urls'] ?? false;
85+
}
8186
}

tests/Unit/IntegrationTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ public function it_can_send_a_sms_message_using_service()
8282
$channel->send(new NotifiableWithAttribute(), $this->notification);
8383
}
8484

85+
/** @test */
86+
public function it_can_send_a_sms_message_using_url_shortener()
87+
{
88+
$message = TwilioSmsMessage::create('Message text');
89+
$this->notification->shouldReceive('toTwilio')->andReturn($message);
90+
91+
$config = new TwilioConfig([
92+
'from' => '+31612345678',
93+
'ShortenUrls' => true,
94+
]);
95+
$twilio = new Twilio($this->twilioService, $config);
96+
$channel = new TwilioChannel($twilio, $this->events);
97+
98+
$this->smsMessageWillBeSentToTwilioWith('+22222222222', [
99+
'from' => '+31612345678',
100+
'body' => 'Message text',
101+
'ShortenUrls' => 'true',
102+
]);
103+
104+
$channel->send(new NotifiableWithAttribute(), $this->notification);
105+
}
106+
85107
/** @test */
86108
public function it_can_send_a_sms_message_using_alphanumeric_sender()
87109
{

0 commit comments

Comments
 (0)