From 7392fccfcbb836c256426cc2c13b2b9e664ab9b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregorio=20Hern=C3=A1ndez=20Caso?= Date: Sat, 17 Mar 2018 12:48:24 +0100 Subject: [PATCH] Add Twilio messages optional parameters --- src/Twilio.php | 61 ++++++++++++++++++++++----- src/TwilioCallMessage.php | 75 +++++++++++++++++++++++++++++++++ src/TwilioMessage.php | 36 ++++++++++++++++ src/TwilioMmsMessage.php | 10 ++--- src/TwilioSmsMessage.php | 74 +++++++++++++++++++++++++++++++- tests/TwilioCallMessageTest.php | 22 ++++++++++ tests/TwilioMmsMessageTest.php | 21 +++++++++ tests/TwilioSmsMessageTest.php | 19 +++++++++ tests/TwilioTest.php | 63 +++++++++++++++++++++++++++ 9 files changed, 363 insertions(+), 18 deletions(-) diff --git a/src/Twilio.php b/src/Twilio.php index a9a69f3..3ded52f 100644 --- a/src/Twilio.php +++ b/src/Twilio.php @@ -21,7 +21,7 @@ class Twilio * Twilio constructor. * * @param TwilioService $twilioService - * @param TwilioConfig $config + * @param TwilioConfig $config */ public function __construct(TwilioService $twilioService, TwilioConfig $config) { @@ -33,8 +33,8 @@ public function __construct(TwilioService $twilioService, TwilioConfig $config) * Send a TwilioMessage to the a phone number. * * @param TwilioMessage $message - * @param string $to - * @param bool $useAlphanumericSender + * @param string $to + * @param bool $useAlphanumericSender * @return mixed * @throws CouldNotSendNotification */ @@ -59,7 +59,7 @@ public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender * Send an sms message using the Twilio Service. * * @param TwilioSmsMessage $message - * @param string $to + * @param string $to * @return \Twilio\Rest\Api\V2010\Account\MessageInstance * @throws CouldNotSendNotification */ @@ -70,14 +70,25 @@ protected function sendSmsMessage(TwilioSmsMessage $message, $to) 'body' => trim($message->content), ]; - if ($message instanceof TwilioMmsMessage) { - $params['mediaUrl'] = $message->mediaUrl; - } - if ($serviceSid = $this->config->getServiceSid()) { $params['messagingServiceSid'] = $serviceSid; } + $this->fillOptionalParams($params, $message, [ + 'statusCallback', + 'statusCallbackMethod', + 'applicationSid', + 'maxPrice', + 'provideFeedback', + 'validityPeriod', + ]); + + if ($message instanceof TwilioMmsMessage) { + $this->fillOptionalParams($params, $message, [ + 'mediaUrl', + ]); + } + return $this->twilioService->messages->create($to, $params); } @@ -85,16 +96,29 @@ protected function sendSmsMessage(TwilioSmsMessage $message, $to) * Make a call using the Twilio Service. * * @param TwilioCallMessage $message - * @param string $to + * @param string $to * @return \Twilio\Rest\Api\V2010\Account\CallInstance * @throws CouldNotSendNotification */ protected function makeCall(TwilioCallMessage $message, $to) { + $params = [ + 'url' => trim($message->content), + ]; + + $this->fillOptionalParams($params, $message, [ + 'statusCallback', + 'statusCallbackMethod', + 'method', + 'status', + 'fallbackUrl', + 'fallbackMethod', + ]); + return $this->twilioService->calls->create( $to, $this->getFrom($message), - ['url' => trim($message->content)] + $params ); } @@ -107,7 +131,7 @@ protected function makeCall(TwilioCallMessage $message, $to) */ protected function getFrom(TwilioMessage $message) { - if (! $from = $message->getFrom() ?: $this->config->getFrom()) { + if (!$from = $message->getFrom() ?: $this->config->getFrom()) { throw CouldNotSendNotification::missingFrom(); } @@ -125,4 +149,19 @@ protected function getAlphanumericSender() return $sender; } } + + /** + * @param array $params + * @param TwilioMessage $message + * @param array $optionalParams + * @return mixed + */ + protected function fillOptionalParams(&$params, $message, $optionalParams) + { + foreach ($optionalParams as $optionalParam) { + if ($message->$optionalParam) { + $params[$optionalParam] = $message->$optionalParam; + } + } + } } diff --git a/src/TwilioCallMessage.php b/src/TwilioCallMessage.php index 86b09d5..ea499ee 100755 --- a/src/TwilioCallMessage.php +++ b/src/TwilioCallMessage.php @@ -4,6 +4,29 @@ class TwilioCallMessage extends TwilioMessage { + const STATUS_CANCELED = 'canceled'; + const STATUS_COMPLETED = 'completed'; + + /** + * @var null|string + */ + public $method = null; + + /** + * @var null|string + */ + public $status = null; + + /** + * @var null|string + */ + public $fallbackUrl = null; + + /** + * @var null|string + */ + public $fallbackMethod = null; + /** * Set the message url. * @@ -16,4 +39,56 @@ public function url($url) return $this; } + + /** + * Set the message url request method. + * + * @param string $method + * @return $this + */ + public function method($method) + { + $this->method = $method; + + return $this; + } + + /** + * Set the status for the current calls. + * + * @param string $status + * @return $this + */ + public function status($status) + { + $this->status = $status; + + return $this; + } + + /** + * Set the fallback url. + * + * @param string $fallbackUrl + * @return $this + */ + public function fallbackUrl($fallbackUrl) + { + $this->fallbackUrl = $fallbackUrl; + + return $this; + } + + /** + * Set the fallback url request method. + * + * @param string $fallbackMethod + * @return $this + */ + public function fallbackMethod($fallbackMethod) + { + $this->fallbackMethod = $fallbackMethod; + + return $this; + } } diff --git a/src/TwilioMessage.php b/src/TwilioMessage.php index a9b274f..e84a8ae 100755 --- a/src/TwilioMessage.php +++ b/src/TwilioMessage.php @@ -18,6 +18,16 @@ abstract class TwilioMessage */ public $from; + /** + * @var null|string + */ + public $statusCallback = null; + + /** + * @var null|string + */ + public $statusCallbackMethod = null; + /** * Create a message object. * @param string $content @@ -73,4 +83,30 @@ public function getFrom() { return $this->from; } + + /** + * Set the status callback. + * + * @param string $statusCallback + * @return $this + */ + public function statusCallback($statusCallback) + { + $this->statusCallback = $statusCallback; + + return $this; + } + + /** + * Set the status callback request method. + * + * @param string $statusCallbackMethod + * @return $this + */ + public function statusCallbackMethod($statusCallbackMethod) + { + $this->statusCallbackMethod = $statusCallbackMethod; + + return $this; + } } diff --git a/src/TwilioMmsMessage.php b/src/TwilioMmsMessage.php index c04128e..f127399 100755 --- a/src/TwilioMmsMessage.php +++ b/src/TwilioMmsMessage.php @@ -5,16 +5,14 @@ class TwilioMmsMessage extends TwilioSmsMessage { /** - * The message media url (for MMS messages). - * - * @var string + * @var string|null */ - public $mediaUrl; + public $mediaUrl = null; /** - * Set the alphanumeric sender. + * Set the message media url. * - * @param $url + * @param string $url * @return $this */ public function mediaUrl($url) diff --git a/src/TwilioSmsMessage.php b/src/TwilioSmsMessage.php index 39de9fc..c75b851 100755 --- a/src/TwilioSmsMessage.php +++ b/src/TwilioSmsMessage.php @@ -9,6 +9,26 @@ class TwilioSmsMessage extends TwilioMessage */ public $alphaNumSender = null; + /** + * @var null|string + */ + public $applicationSid = null; + + /** + * @var null|float + */ + public $maxPrice = null; + + /** + * @var null|boolean + */ + public $provideFeedback = null; + + /** + * @var null|integer + */ + public $validityPeriod = null; + /** * Get the from address of this message. * @@ -28,7 +48,7 @@ public function getFrom() /** * Set the alphanumeric sender. * - * @param $sender + * @param string $sender * @return $this */ public function sender($sender) @@ -37,4 +57,56 @@ public function sender($sender) return $this; } + + /** + * Set application SID for the message status callback. + * + * @param string $applicationSid + * @return $this + */ + public function applicationSid($applicationSid) + { + $this->applicationSid = $applicationSid; + + return $this; + } + + /** + * Set the max price (in USD dollars). + * + * @param float $maxPrice + * @return $this + */ + public function maxPrice($maxPrice) + { + $this->maxPrice = $maxPrice; + + return $this; + } + + /** + * Set the provide feedback option. + * + * @param boolean $provideFeedback + * @return $this + */ + public function provideFeedback($provideFeedback) + { + $this->provideFeedback = $provideFeedback; + + return $this; + } + + /** + * Set the validity period (in seconds). + * + * @param integer $validityPeriod + * @return $this + */ + public function validityPeriod($validityPeriod) + { + $this->validityPeriod = $validityPeriod; + + return $this; + } } diff --git a/tests/TwilioCallMessageTest.php b/tests/TwilioCallMessageTest.php index 060bea2..c0e876b 100644 --- a/tests/TwilioCallMessageTest.php +++ b/tests/TwilioCallMessageTest.php @@ -6,6 +6,9 @@ class TwilioCallMessageTest extends TwilioMessageTest { + /** @var TwilioCallMessage */ + protected $message; + public function setUp() { parent::setUp(); @@ -36,4 +39,23 @@ public function it_can_set_the_url() $this->assertEquals('http://example.com', $this->message->content); } + + /** @test */ + public function it_can_set_optional_parameters() + { + $message = TwilioCallMessage::create('myMessage'); + $message->status(TwilioCallMessage::STATUS_CANCELED); + $message->method('PUT'); + $message->statusCallback('http://example.com'); + $message->statusCallbackMethod('PUT'); + $message->fallbackUrl('http://example.com'); + $message->fallbackMethod('PUT'); + + $this->assertEquals(TwilioCallMessage::STATUS_CANCELED, $message->status); + $this->assertEquals('PUT', $message->method); + $this->assertEquals('http://example.com', $message->statusCallback); + $this->assertEquals('PUT', $message->statusCallbackMethod); + $this->assertEquals('http://example.com', $message->fallbackUrl); + $this->assertEquals('PUT', $message->fallbackMethod); + } } diff --git a/tests/TwilioMmsMessageTest.php b/tests/TwilioMmsMessageTest.php index 0dcc4e3..c7dec85 100644 --- a/tests/TwilioMmsMessageTest.php +++ b/tests/TwilioMmsMessageTest.php @@ -55,4 +55,25 @@ public function it_can_return_the_alphanumeric_sender_if_set() $this->assertEquals('TestSender', $message->getFrom()); } + + /** @test */ + public function it_can_set_optional_parameters() + { + $message = TwilioMmsMessage::create('myMessage'); + $message->statusCallback('http://example.com'); + $message->statusCallbackMethod('PUT'); + $message->applicationSid('ABCD1234'); + $message->maxPrice(0.05); + $message->provideFeedback(true); + $message->validityPeriod(120); + $message->mediaUrl('http://example.com'); + + $this->assertEquals('http://example.com', $message->statusCallback); + $this->assertEquals('PUT', $message->statusCallbackMethod); + $this->assertEquals('ABCD1234', $message->applicationSid); + $this->assertEquals(0.05, $message->maxPrice); + $this->assertEquals(true, $message->provideFeedback); + $this->assertEquals(120, $message->validityPeriod); + $this->assertEquals('http://example.com', $message->mediaUrl); + } } diff --git a/tests/TwilioSmsMessageTest.php b/tests/TwilioSmsMessageTest.php index 1d184e2..62e622a 100644 --- a/tests/TwilioSmsMessageTest.php +++ b/tests/TwilioSmsMessageTest.php @@ -46,4 +46,23 @@ public function it_can_return_the_alphanumeric_sender_if_set() $this->assertEquals('TestSender', $message->getFrom()); } + + /** @test */ + public function it_can_set_optional_parameters() + { + $message = TwilioSmsMessage::create('myMessage'); + $message->statusCallback('http://example.com'); + $message->statusCallbackMethod('PUT'); + $message->applicationSid('ABCD1234'); + $message->maxPrice(0.05); + $message->provideFeedback(true); + $message->validityPeriod(120); + + $this->assertEquals('http://example.com', $message->statusCallback); + $this->assertEquals('PUT', $message->statusCallbackMethod); + $this->assertEquals('ABCD1234', $message->applicationSid); + $this->assertEquals(0.05, $message->maxPrice); + $this->assertEquals(true, $message->provideFeedback); + $this->assertEquals(120, $message->validityPeriod); + } } diff --git a/tests/TwilioTest.php b/tests/TwilioTest.php index 476691e..dd804cc 100644 --- a/tests/TwilioTest.php +++ b/tests/TwilioTest.php @@ -3,6 +3,7 @@ namespace NotificationChannels\Twilio\Test; use Mockery; +use NotificationChannels\Twilio\TwilioMmsMessage; use Services_Twilio_Rest_Calls; use Services_Twilio_Rest_Messages; use NotificationChannels\Twilio\Twilio; @@ -49,6 +50,12 @@ public function setUp() public function it_can_send_a_sms_message_to_twilio() { $message = new TwilioSmsMessage('Message text'); + $message->statusCallback('http://example.com'); + $message->statusCallbackMethod('PUT'); + $message->applicationSid('ABCD1234'); + $message->maxPrice(0.05); + $message->provideFeedback(true); + $message->validityPeriod(120); $this->config->shouldReceive('getFrom') ->once() @@ -63,6 +70,50 @@ public function it_can_send_a_sms_message_to_twilio() ->with('+1111111111', [ 'from' => '+1234567890', 'body' => 'Message text', + 'statusCallback' => 'http://example.com', + 'statusCallbackMethod' => 'PUT', + 'applicationSid' => 'ABCD1234', + 'maxPrice' => 0.05, + 'provideFeedback' => true, + 'validityPeriod' => 120, + ]) + ->andReturn(true); + + $this->twilio->sendMessage($message, '+1111111111'); + } + + /** @test */ + public function it_can_send_a_mms_message_to_twilio() + { + $message = new TwilioMmsMessage('Message text'); + $message->statusCallback('http://example.com'); + $message->statusCallbackMethod('PUT'); + $message->applicationSid('ABCD1234'); + $message->maxPrice(0.05); + $message->provideFeedback(true); + $message->validityPeriod(120); + $message->mediaUrl('http://example.com'); + + $this->config->shouldReceive('getFrom') + ->once() + ->andReturn('+1234567890'); + + $this->config->shouldReceive('getServiceSid') + ->once() + ->andReturn(null); + + $this->twilioService->messages->shouldReceive('create') + ->atLeast()->once() + ->with('+1111111111', [ + 'from' => '+1234567890', + 'body' => 'Message text', + 'statusCallback' => 'http://example.com', + 'statusCallbackMethod' => 'PUT', + 'applicationSid' => 'ABCD1234', + 'maxPrice' => 0.05, + 'provideFeedback' => true, + 'validityPeriod' => 120, + 'mediaUrl' => 'http://example.com', ]) ->andReturn(true); @@ -125,11 +176,23 @@ public function it_can_send_a_call_to_twilio() { $message = new TwilioCallMessage('http://example.com'); $message->from = '+2222222222'; + $message->status(TwilioCallMessage::STATUS_CANCELED); + $message->method('PUT'); + $message->statusCallback('http://example.com'); + $message->statusCallbackMethod('PUT'); + $message->fallbackUrl('http://example.com'); + $message->fallbackMethod('PUT'); $this->twilioService->calls->shouldReceive('create') ->atLeast()->once() ->with('+1111111111', '+2222222222', [ 'url' => 'http://example.com', + 'status' => TwilioCallMessage::STATUS_CANCELED, + 'method' => 'PUT', + 'statusCallback' => 'http://example.com', + 'statusCallbackMethod' => 'PUT', + 'fallbackUrl' => 'http://example.com', + 'fallbackMethod' => 'PUT', ]) ->andReturn(true);