Skip to content

Commit 7392fcc

Browse files
committed
Add Twilio messages optional parameters
1 parent 4c36bab commit 7392fcc

9 files changed

+363
-18
lines changed

src/Twilio.php

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Twilio
2121
* Twilio constructor.
2222
*
2323
* @param TwilioService $twilioService
24-
* @param TwilioConfig $config
24+
* @param TwilioConfig $config
2525
*/
2626
public function __construct(TwilioService $twilioService, TwilioConfig $config)
2727
{
@@ -33,8 +33,8 @@ public function __construct(TwilioService $twilioService, TwilioConfig $config)
3333
* Send a TwilioMessage to the a phone number.
3434
*
3535
* @param TwilioMessage $message
36-
* @param string $to
37-
* @param bool $useAlphanumericSender
36+
* @param string $to
37+
* @param bool $useAlphanumericSender
3838
* @return mixed
3939
* @throws CouldNotSendNotification
4040
*/
@@ -59,7 +59,7 @@ public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender
5959
* Send an sms message using the Twilio Service.
6060
*
6161
* @param TwilioSmsMessage $message
62-
* @param string $to
62+
* @param string $to
6363
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
6464
* @throws CouldNotSendNotification
6565
*/
@@ -70,31 +70,55 @@ protected function sendSmsMessage(TwilioSmsMessage $message, $to)
7070
'body' => trim($message->content),
7171
];
7272

73-
if ($message instanceof TwilioMmsMessage) {
74-
$params['mediaUrl'] = $message->mediaUrl;
75-
}
76-
7773
if ($serviceSid = $this->config->getServiceSid()) {
7874
$params['messagingServiceSid'] = $serviceSid;
7975
}
8076

77+
$this->fillOptionalParams($params, $message, [
78+
'statusCallback',
79+
'statusCallbackMethod',
80+
'applicationSid',
81+
'maxPrice',
82+
'provideFeedback',
83+
'validityPeriod',
84+
]);
85+
86+
if ($message instanceof TwilioMmsMessage) {
87+
$this->fillOptionalParams($params, $message, [
88+
'mediaUrl',
89+
]);
90+
}
91+
8192
return $this->twilioService->messages->create($to, $params);
8293
}
8394

8495
/**
8596
* Make a call using the Twilio Service.
8697
*
8798
* @param TwilioCallMessage $message
88-
* @param string $to
99+
* @param string $to
89100
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
90101
* @throws CouldNotSendNotification
91102
*/
92103
protected function makeCall(TwilioCallMessage $message, $to)
93104
{
105+
$params = [
106+
'url' => trim($message->content),
107+
];
108+
109+
$this->fillOptionalParams($params, $message, [
110+
'statusCallback',
111+
'statusCallbackMethod',
112+
'method',
113+
'status',
114+
'fallbackUrl',
115+
'fallbackMethod',
116+
]);
117+
94118
return $this->twilioService->calls->create(
95119
$to,
96120
$this->getFrom($message),
97-
['url' => trim($message->content)]
121+
$params
98122
);
99123
}
100124

@@ -107,7 +131,7 @@ protected function makeCall(TwilioCallMessage $message, $to)
107131
*/
108132
protected function getFrom(TwilioMessage $message)
109133
{
110-
if (! $from = $message->getFrom() ?: $this->config->getFrom()) {
134+
if (!$from = $message->getFrom() ?: $this->config->getFrom()) {
111135
throw CouldNotSendNotification::missingFrom();
112136
}
113137

@@ -125,4 +149,19 @@ protected function getAlphanumericSender()
125149
return $sender;
126150
}
127151
}
152+
153+
/**
154+
* @param array $params
155+
* @param TwilioMessage $message
156+
* @param array $optionalParams
157+
* @return mixed
158+
*/
159+
protected function fillOptionalParams(&$params, $message, $optionalParams)
160+
{
161+
foreach ($optionalParams as $optionalParam) {
162+
if ($message->$optionalParam) {
163+
$params[$optionalParam] = $message->$optionalParam;
164+
}
165+
}
166+
}
128167
}

src/TwilioCallMessage.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44

55
class TwilioCallMessage extends TwilioMessage
66
{
7+
const STATUS_CANCELED = 'canceled';
8+
const STATUS_COMPLETED = 'completed';
9+
10+
/**
11+
* @var null|string
12+
*/
13+
public $method = null;
14+
15+
/**
16+
* @var null|string
17+
*/
18+
public $status = null;
19+
20+
/**
21+
* @var null|string
22+
*/
23+
public $fallbackUrl = null;
24+
25+
/**
26+
* @var null|string
27+
*/
28+
public $fallbackMethod = null;
29+
730
/**
831
* Set the message url.
932
*
@@ -16,4 +39,56 @@ public function url($url)
1639

1740
return $this;
1841
}
42+
43+
/**
44+
* Set the message url request method.
45+
*
46+
* @param string $method
47+
* @return $this
48+
*/
49+
public function method($method)
50+
{
51+
$this->method = $method;
52+
53+
return $this;
54+
}
55+
56+
/**
57+
* Set the status for the current calls.
58+
*
59+
* @param string $status
60+
* @return $this
61+
*/
62+
public function status($status)
63+
{
64+
$this->status = $status;
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* Set the fallback url.
71+
*
72+
* @param string $fallbackUrl
73+
* @return $this
74+
*/
75+
public function fallbackUrl($fallbackUrl)
76+
{
77+
$this->fallbackUrl = $fallbackUrl;
78+
79+
return $this;
80+
}
81+
82+
/**
83+
* Set the fallback url request method.
84+
*
85+
* @param string $fallbackMethod
86+
* @return $this
87+
*/
88+
public function fallbackMethod($fallbackMethod)
89+
{
90+
$this->fallbackMethod = $fallbackMethod;
91+
92+
return $this;
93+
}
1994
}

src/TwilioMessage.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ abstract class TwilioMessage
1818
*/
1919
public $from;
2020

21+
/**
22+
* @var null|string
23+
*/
24+
public $statusCallback = null;
25+
26+
/**
27+
* @var null|string
28+
*/
29+
public $statusCallbackMethod = null;
30+
2131
/**
2232
* Create a message object.
2333
* @param string $content
@@ -73,4 +83,30 @@ public function getFrom()
7383
{
7484
return $this->from;
7585
}
86+
87+
/**
88+
* Set the status callback.
89+
*
90+
* @param string $statusCallback
91+
* @return $this
92+
*/
93+
public function statusCallback($statusCallback)
94+
{
95+
$this->statusCallback = $statusCallback;
96+
97+
return $this;
98+
}
99+
100+
/**
101+
* Set the status callback request method.
102+
*
103+
* @param string $statusCallbackMethod
104+
* @return $this
105+
*/
106+
public function statusCallbackMethod($statusCallbackMethod)
107+
{
108+
$this->statusCallbackMethod = $statusCallbackMethod;
109+
110+
return $this;
111+
}
76112
}

src/TwilioMmsMessage.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
class TwilioMmsMessage extends TwilioSmsMessage
66
{
77
/**
8-
* The message media url (for MMS messages).
9-
*
10-
* @var string
8+
* @var string|null
119
*/
12-
public $mediaUrl;
10+
public $mediaUrl = null;
1311

1412
/**
15-
* Set the alphanumeric sender.
13+
* Set the message media url.
1614
*
17-
* @param $url
15+
* @param string $url
1816
* @return $this
1917
*/
2018
public function mediaUrl($url)

src/TwilioSmsMessage.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ class TwilioSmsMessage extends TwilioMessage
99
*/
1010
public $alphaNumSender = null;
1111

12+
/**
13+
* @var null|string
14+
*/
15+
public $applicationSid = null;
16+
17+
/**
18+
* @var null|float
19+
*/
20+
public $maxPrice = null;
21+
22+
/**
23+
* @var null|boolean
24+
*/
25+
public $provideFeedback = null;
26+
27+
/**
28+
* @var null|integer
29+
*/
30+
public $validityPeriod = null;
31+
1232
/**
1333
* Get the from address of this message.
1434
*
@@ -28,7 +48,7 @@ public function getFrom()
2848
/**
2949
* Set the alphanumeric sender.
3050
*
31-
* @param $sender
51+
* @param string $sender
3252
* @return $this
3353
*/
3454
public function sender($sender)
@@ -37,4 +57,56 @@ public function sender($sender)
3757

3858
return $this;
3959
}
60+
61+
/**
62+
* Set application SID for the message status callback.
63+
*
64+
* @param string $applicationSid
65+
* @return $this
66+
*/
67+
public function applicationSid($applicationSid)
68+
{
69+
$this->applicationSid = $applicationSid;
70+
71+
return $this;
72+
}
73+
74+
/**
75+
* Set the max price (in USD dollars).
76+
*
77+
* @param float $maxPrice
78+
* @return $this
79+
*/
80+
public function maxPrice($maxPrice)
81+
{
82+
$this->maxPrice = $maxPrice;
83+
84+
return $this;
85+
}
86+
87+
/**
88+
* Set the provide feedback option.
89+
*
90+
* @param boolean $provideFeedback
91+
* @return $this
92+
*/
93+
public function provideFeedback($provideFeedback)
94+
{
95+
$this->provideFeedback = $provideFeedback;
96+
97+
return $this;
98+
}
99+
100+
/**
101+
* Set the validity period (in seconds).
102+
*
103+
* @param integer $validityPeriod
104+
* @return $this
105+
*/
106+
public function validityPeriod($validityPeriod)
107+
{
108+
$this->validityPeriod = $validityPeriod;
109+
110+
return $this;
111+
}
40112
}

0 commit comments

Comments
 (0)