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