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