1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twilio; |
4
|
|
|
|
5
|
|
|
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification; |
6
|
|
|
use Twilio\Exceptions\RestException; |
7
|
|
|
use Twilio\Exceptions\TwilioException; |
8
|
|
|
use Twilio\Rest\Api\V2010\Account\CallInstance; |
9
|
|
|
use Twilio\Rest\Api\V2010\Account\MessageInstance; |
10
|
|
|
use Twilio\Rest\Client as TwilioService; |
11
|
|
|
|
12
|
|
|
class Twilio |
13
|
|
|
{ |
14
|
|
|
/** @var TwilioService */ |
15
|
|
|
protected $twilioService; |
16
|
|
|
|
17
|
|
|
/** @var TwilioConfig */ |
18
|
|
|
private $config; |
19
|
|
|
|
20
|
|
|
public function __construct(TwilioService $twilioService, TwilioConfig $config) |
21
|
|
|
{ |
22
|
|
|
$this->twilioService = $twilioService; |
23
|
|
|
$this->config = $config; |
24
|
|
|
} |
25
|
|
|
|
26
|
13 |
|
/** |
27
|
|
|
* Send a TwilioMessage to the a phone number. |
28
|
13 |
|
* |
29
|
13 |
|
* @param TwilioMessage $message |
30
|
13 |
|
* @param string|null $to |
31
|
|
|
* @param bool $useAlphanumericSender |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
* @throws TwilioException |
35
|
|
|
* @throws CouldNotSendNotification |
36
|
|
|
*/ |
37
|
|
|
public function sendMessage(TwilioMessage $message, ?string $to, bool $useAlphanumericSender = false) |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
|
|
if ($message instanceof TwilioSmsMessage) { |
41
|
11 |
|
if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) { |
42
|
|
|
$message->from($sender); |
43
|
11 |
|
} |
44
|
8 |
|
|
45
|
2 |
|
return $this->sendSmsMessage($message, $to); |
46
|
|
|
} |
47
|
|
|
|
48
|
8 |
|
if ($message instanceof TwilioCallMessage) { |
49
|
|
|
return $this->makeCall($message, $to); |
50
|
|
|
} |
51
|
3 |
|
} catch (TwilioException $e) { |
52
|
2 |
|
if ($this->config->isIgnoredErrorCode($e->getCode())) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
1 |
|
|
56
|
|
|
throw $e; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
throw CouldNotSendNotification::invalidMessageObject($message); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Send an sms message using the Twilio Service. |
64
|
|
|
* |
65
|
8 |
|
* @param TwilioSmsMessage $message |
66
|
|
|
* @param string|null $to |
67
|
|
|
* |
68
|
8 |
|
* @return MessageInstance |
69
|
|
|
* @throws CouldNotSendNotification |
70
|
|
|
* @throws TwilioException |
71
|
8 |
|
*/ |
72
|
2 |
|
protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): MessageInstance |
73
|
|
|
{ |
74
|
|
|
$params = [ |
75
|
8 |
|
'body' => trim($message->content), |
76
|
7 |
|
]; |
77
|
|
|
|
78
|
|
|
if ($messagingServiceSid = $this->getMessagingServiceSid($message)) { |
79
|
8 |
|
$params['messagingServiceSid'] = $messagingServiceSid; |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
if ($from = $this->getFrom($message)) { |
83
|
7 |
|
$params['from'] = $from; |
84
|
7 |
|
} |
85
|
|
|
|
86
|
|
|
if (! $from && ! $messagingServiceSid) { |
|
|
|
|
87
|
|
|
throw CouldNotSendNotification::missingFrom(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->fillOptionalParams($params, $message, [ |
91
|
|
|
'statusCallback', |
92
|
|
|
'statusCallbackMethod', |
93
|
7 |
|
'applicationSid', |
94
|
1 |
|
'forceDelivery', |
95
|
1 |
|
'maxPrice', |
96
|
|
|
'provideFeedback', |
97
|
|
|
'validityPeriod', |
98
|
|
|
]); |
99
|
7 |
|
|
100
|
|
|
if ($message instanceof TwilioMmsMessage) { |
101
|
|
|
$this->fillOptionalParams($params, $message, [ |
102
|
|
|
'mediaUrl', |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->twilioService->messages->create($to, $params); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
2 |
|
* Make a call using the Twilio Service. |
111
|
|
|
* |
112
|
|
|
* @param TwilioCallMessage $message |
113
|
2 |
|
* @param string|null $to |
114
|
|
|
* |
115
|
|
|
* @return CallInstance |
116
|
2 |
|
* @throws TwilioException |
117
|
2 |
|
* @throws CouldNotSendNotification |
118
|
|
|
*/ |
119
|
|
|
protected function makeCall(TwilioCallMessage $message, ?string $to): CallInstance |
120
|
|
|
{ |
121
|
|
|
$params = [ |
122
|
|
|
'url' => trim($message->content), |
123
|
|
|
]; |
124
|
|
|
|
125
|
2 |
|
$this->fillOptionalParams($params, $message, [ |
126
|
|
|
'statusCallback', |
127
|
|
|
'statusCallbackMethod', |
128
|
|
|
'method', |
129
|
2 |
|
'status', |
130
|
2 |
|
'fallbackUrl', |
131
|
|
|
'fallbackMethod', |
132
|
|
|
]); |
133
|
|
|
|
134
|
|
|
if (! $from = $this->getFrom($message)) { |
135
|
|
|
throw CouldNotSendNotification::missingFrom(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->twilioService->calls->create( |
139
|
|
|
$to, |
140
|
|
|
$from, |
141
|
|
|
$params |
142
|
10 |
|
); |
143
|
|
|
} |
144
|
10 |
|
|
145
|
|
|
/** |
146
|
|
|
* Get the from address from message, or config. |
147
|
|
|
* |
148
|
|
|
* @param TwilioMessage $message |
149
|
|
|
* @return string|null |
150
|
|
|
*/ |
151
|
|
|
protected function getFrom(TwilioMessage $message): ?string |
152
|
|
|
{ |
153
|
8 |
|
return $message->getFrom() ?: $this->config->getFrom(); |
154
|
|
|
} |
155
|
8 |
|
|
156
|
|
|
/** |
157
|
|
|
* Get the messaging service SID from message, or config. |
158
|
|
|
* |
159
|
|
|
* @param TwilioSmsMessage $message |
160
|
|
|
* @return string|null |
161
|
|
|
*/ |
162
|
|
|
protected function getMessagingServiceSid(TwilioSmsMessage $message): ?string |
163
|
2 |
|
{ |
164
|
|
|
return $message->getMessagingServiceSid() ?: $this->config->getServiceSid(); |
165
|
2 |
|
} |
166
|
2 |
|
|
167
|
|
|
/** |
168
|
|
|
* Get the alphanumeric sender from config, if one exists. |
169
|
|
|
* |
170
|
|
|
* @return string|null |
171
|
|
|
*/ |
172
|
|
|
protected function getAlphanumericSender(): ?string |
173
|
|
|
{ |
174
|
|
|
return $this->config->getAlphanumericSender(); |
175
|
|
|
} |
176
|
9 |
|
|
177
|
|
|
/** |
178
|
9 |
|
* @param array $params |
179
|
9 |
|
* @param TwilioMessage $message |
180
|
3 |
|
* @param array $optionalParams |
181
|
|
|
* @return Twilio |
182
|
|
|
*/ |
183
|
|
|
protected function fillOptionalParams(&$params, $message, $optionalParams): Twilio |
184
|
9 |
|
{ |
185
|
|
|
foreach ($optionalParams as $optionalParam) { |
186
|
|
|
if ($message->$optionalParam) { |
187
|
|
|
$params[$optionalParam] = $message->$optionalParam; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: