From f0128ce77f1f9935265ff95a6f14dbc9d494791c Mon Sep 17 00:00:00 2001 From: Jerome Bajou Date: Tue, 21 Feb 2023 23:35:58 +0200 Subject: [PATCH] Add option to use Twilio's URL shortener (#138) Co-authored-by: atymic --- README.md | 3 ++- config/twilio-notification-channel.php | 1 + src/Twilio.php | 4 ++++ src/TwilioConfig.php | 5 +++++ tests/Unit/IntegrationTest.php | 22 ++++++++++++++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 96b61a8..1eebad4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config/twilio-notification-channel.php b/config/twilio-notification-channel.php index 04e69c6..0b99103 100644 --- a/config/twilio-notification-channel.php +++ b/config/twilio-notification-channel.php @@ -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. diff --git a/src/Twilio.php b/src/Twilio.php index 91b6e12..76b6d62 100644 --- a/src/Twilio.php +++ b/src/Twilio.php @@ -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; } diff --git a/src/TwilioConfig.php b/src/TwilioConfig.php index 5334258..f2a8592 100644 --- a/src/TwilioConfig.php +++ b/src/TwilioConfig.php @@ -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; + } } diff --git a/tests/Unit/IntegrationTest.php b/tests/Unit/IntegrationTest.php index e046a26..f2fbcf9 100644 --- a/tests/Unit/IntegrationTest.php +++ b/tests/Unit/IntegrationTest.php @@ -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() {