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