Skip to content

Commit 9876810

Browse files
committed
Allow optional "from" when sending SMS using "messagingServiceSid"
1 parent 1781dd1 commit 9876810

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

src/Twilio.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(TwilioService $twilioService, TwilioConfig $config)
3636
* @param string $to
3737
* @param bool $useAlphanumericSender
3838
* @return mixed
39-
* @throws CouldNotSendNotification
39+
* @throws \Twilio\Exceptions\TwilioException
4040
*/
4141
public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
4242
{
@@ -61,17 +61,23 @@ public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender
6161
* @param TwilioSmsMessage $message
6262
* @param string $to
6363
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
64-
* @throws CouldNotSendNotification
6564
*/
6665
protected function sendSmsMessage(TwilioSmsMessage $message, $to)
6766
{
6867
$params = [
69-
'from' => $this->getFrom($message),
7068
'body' => trim($message->content),
7169
];
7270

73-
if ($serviceSid = $this->config->getServiceSid()) {
74-
$params['messagingServiceSid'] = $serviceSid;
71+
if ($messagingServiceSid = $this->getMessagingServiceSid($message)) {
72+
$params['messagingServiceSid'] = $messagingServiceSid;
73+
}
74+
75+
if ($from = $this->getFrom($message)) {
76+
$params['from'] = $from;
77+
}
78+
79+
if (!$from && !$messagingServiceSid) {
80+
throw CouldNotSendNotification::missingFrom();
7581
}
7682

7783
$this->fillOptionalParams($params, $message, [
@@ -98,7 +104,7 @@ protected function sendSmsMessage(TwilioSmsMessage $message, $to)
98104
* @param TwilioCallMessage $message
99105
* @param string $to
100106
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
101-
* @throws CouldNotSendNotification
107+
* @throws \Twilio\Exceptions\TwilioException
102108
*/
103109
protected function makeCall(TwilioCallMessage $message, $to)
104110
{
@@ -115,9 +121,13 @@ protected function makeCall(TwilioCallMessage $message, $to)
115121
'fallbackMethod',
116122
]);
117123

124+
if (! $from = $this->getFrom($message)) {
125+
throw CouldNotSendNotification::missingFrom();
126+
}
127+
118128
return $this->twilioService->calls->create(
119129
$to,
120-
$this->getFrom($message),
130+
$from,
121131
$params
122132
);
123133
}
@@ -127,15 +137,21 @@ protected function makeCall(TwilioCallMessage $message, $to)
127137
*
128138
* @param TwilioMessage $message
129139
* @return string
130-
* @throws CouldNotSendNotification
131140
*/
132141
protected function getFrom(TwilioMessage $message)
133142
{
134-
if (! $from = $message->getFrom() ?: $this->config->getFrom()) {
135-
throw CouldNotSendNotification::missingFrom();
136-
}
143+
return $message->getFrom() ?: $this->config->getFrom();
144+
}
137145

138-
return $from;
146+
/**
147+
* Get the messaging service SID from message, or config.
148+
*
149+
* @param TwilioSmsMessage $message
150+
* @return string
151+
*/
152+
protected function getMessagingServiceSid(TwilioSmsMessage $message)
153+
{
154+
return $message->getMessagingServiceSid() ?: $this->config->getServiceSid();
139155
}
140156

141157
/**
@@ -148,13 +164,15 @@ protected function getAlphanumericSender()
148164
if ($sender = $this->config->getAlphanumericSender()) {
149165
return $sender;
150166
}
167+
168+
return null;
151169
}
152170

153171
/**
154172
* @param array $params
155173
* @param TwilioMessage $message
156174
* @param array $optionalParams
157-
* @return mixed
175+
* @return Twilio
158176
*/
159177
protected function fillOptionalParams(&$params, $message, $optionalParams)
160178
{
@@ -163,5 +181,7 @@ protected function fillOptionalParams(&$params, $message, $optionalParams)
163181
$params[$optionalParam] = $message->$optionalParam;
164182
}
165183
}
184+
185+
return $this;
166186
}
167187
}

src/TwilioSmsMessage.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class TwilioSmsMessage extends TwilioMessage
99
*/
1010
public $alphaNumSender = null;
1111

12+
/**
13+
* @var null|string
14+
*/
15+
public $messagingServiceSid = null;
16+
1217
/**
1318
* @var null|string
1419
*/
@@ -43,6 +48,31 @@ public function getFrom()
4348
if ($this->alphaNumSender && strlen($this->alphaNumSender) > 0) {
4449
return $this->alphaNumSender;
4550
}
51+
52+
return null;
53+
}
54+
55+
/**
56+
* Set the messaging service SID.
57+
*
58+
* @param string $messagingServiceSid
59+
* @return $this
60+
*/
61+
public function messagingServiceSid($messagingServiceSid)
62+
{
63+
$this->messagingServiceSid = $messagingServiceSid;
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* Get the messaging service SID of this message.
70+
*
71+
* @return null|string
72+
*/
73+
public function getMessagingServiceSid()
74+
{
75+
return $this->messagingServiceSid;
4676
}
4777

4878
/**

tests/TwilioTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ public function it_will_throw_an_exception_in_case_of_a_missing_from_number()
213213
->once()
214214
->andReturn(null);
215215

216+
$this->config->shouldReceive('getServiceSid')
217+
->once()
218+
->andReturn(null);
219+
216220
$this->twilio->sendMessage($smsMessage, null);
217221
}
218222

0 commit comments

Comments
 (0)