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