Skip to content

Commit

Permalink
Add Twilio messages optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
gregoriohc committed Mar 17, 2018
1 parent 4c36bab commit 7392fcc
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 18 deletions.
61 changes: 50 additions & 11 deletions src/Twilio.php
Expand Up @@ -21,7 +21,7 @@ class Twilio
* Twilio constructor.
*
* @param TwilioService $twilioService
* @param TwilioConfig $config
* @param TwilioConfig $config
*/
public function __construct(TwilioService $twilioService, TwilioConfig $config)
{
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -70,31 +70,55 @@ 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);
}

/**
* 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
);
}

Expand All @@ -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();
}

Expand All @@ -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;
}
}
}
}
75 changes: 75 additions & 0 deletions src/TwilioCallMessage.php
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
36 changes: 36 additions & 0 deletions src/TwilioMessage.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
10 changes: 4 additions & 6 deletions src/TwilioMmsMessage.php
Expand Up @@ -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)
Expand Down
74 changes: 73 additions & 1 deletion src/TwilioSmsMessage.php
Expand Up @@ -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.
*
Expand All @@ -28,7 +48,7 @@ public function getFrom()
/**
* Set the alphanumeric sender.
*
* @param $sender
* @param string $sender
* @return $this
*/
public function sender($sender)
Expand All @@ -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;
}
}

0 comments on commit 7392fcc

Please sign in to comment.