1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Textlocal; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Illuminate\Support\Carbon; |
7
|
|
|
use GuzzleHttp\Exception\SeekException; |
8
|
|
|
use GuzzleHttp\Exception\RequestException; |
9
|
|
|
use GuzzleHttp\Exception\TransferException; |
10
|
|
|
use NotificationChannels\Textlocal\Exceptions\CouldNotSendNotification; |
11
|
|
|
use NotificationChannels\Textlocal\Exceptions\CouldNotAuthenticateAccount; |
12
|
|
|
|
13
|
|
|
class TextlocalClient |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The form params to be sent with the request to the api. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $params; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Guzzle http client. |
24
|
|
|
* |
25
|
|
|
* @var GuzzleHttp\Client |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
protected $http; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Api endpoint to send the request for sms. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $url; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create new TextlocalClient instance. |
38
|
|
|
* |
39
|
|
|
* @param \GuzzleHttp\Client $client |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Client $client) |
42
|
|
|
{ |
43
|
|
|
$this->http = $client; |
|
|
|
|
44
|
|
|
$this->url = config('services.textlocal.url'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set the Account to be used as Transactional Account. |
49
|
|
|
* |
50
|
|
|
* @return TextlocalClient |
51
|
|
|
*/ |
52
|
|
|
public function transactional() |
53
|
|
|
{ |
54
|
|
|
$apiKey = config('services.textlocal.transactional.apiKey'); |
55
|
|
|
if (empty($apiKey)) { |
56
|
|
|
throw CouldNotAuthenticateAccount::apiKeyMissing('transactional'); |
57
|
|
|
} |
58
|
|
|
$this->addParam('apiKey', urlencode($apiKey)); |
59
|
|
|
$this->addParam('sender', config('services.textlocal.transactional.from'), 'TXTLCL'); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the Account to be used as Promotional Account. |
66
|
|
|
* |
67
|
|
|
* @return TextlocalClient |
68
|
|
|
*/ |
69
|
|
|
public function promotional() |
70
|
|
|
{ |
71
|
|
|
$apiKey = config('services.textlocal.promotional.apiKey'); |
72
|
|
|
if (empty($apiKey)) { |
73
|
|
|
throw CouldNotAuthenticateAccount::apiKeyMissing('promotional'); |
74
|
|
|
} |
75
|
|
|
$this->addParam('apiKey', urlencode($apiKey)); |
76
|
|
|
$this->addParam('sender', config('services.textlocal.promotional.from'), 'TXTLCL'); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add parameter to the request parameters to be sent to the api endpoint. |
83
|
|
|
* |
84
|
|
|
* @param string $key |
85
|
|
|
* @param string|number $value |
86
|
|
|
* @return TextlocalClient |
87
|
|
|
*/ |
88
|
|
|
public function addParam($key, $value) |
89
|
|
|
{ |
90
|
|
|
$this->params[$key] = $value; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the time in future at which the message should be sent. |
97
|
|
|
* @param string|number $schedule |
98
|
|
|
* @return TextlocalClient |
99
|
|
|
*/ |
100
|
|
|
public function at($schedule) |
101
|
|
|
{ |
102
|
|
|
if ($schedule) { |
103
|
|
|
$time = is_numeric($schedule) ? $schedule : Carbon::parse($schedule)->timestamp; |
104
|
|
|
$this->addParam('schedule_time', $time); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* If provided, set the sender from which the message should be sent. |
112
|
|
|
* Otherwise let the sender be the default provided in the config. |
113
|
|
|
* |
114
|
|
|
* @param $sender |
115
|
|
|
* @return TextlocalClient |
116
|
|
|
*/ |
117
|
|
|
public function from($sender) |
118
|
|
|
{ |
119
|
|
|
if ($sender) { |
120
|
|
|
$this->addParam('sender', $sender); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Prepare comma separated list of numbers to which the message is to be sent. |
128
|
|
|
* @param $numbers |
129
|
|
|
* @return TextlocalClient |
130
|
|
|
*/ |
131
|
|
|
public function to($numbers) |
132
|
|
|
{ |
133
|
|
|
$this->addParam( |
134
|
|
|
'numbers', |
135
|
|
|
implode( |
136
|
|
|
',', |
137
|
|
|
is_string($numbers) ? explode(',', $numbers) : $numbers |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Prepare the params from the received message object and make the api request to send sms. |
146
|
|
|
* |
147
|
|
|
* @param string $to |
148
|
|
|
* @param TexlocalMessage $message |
|
|
|
|
149
|
|
|
* @return object |
150
|
|
|
*/ |
151
|
|
|
public function message($to, $message) |
152
|
|
|
{ |
153
|
|
|
$numbers = array_merge([$to], $message->cc); |
154
|
|
|
|
155
|
|
|
return $this->{$message->account}() |
156
|
|
|
->to($numbers) |
157
|
|
|
->from($message->from) |
158
|
|
|
->at($message->at) |
159
|
|
|
->test($message->test) |
160
|
|
|
->send($message->content); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Send the message, making a request to the api endpoint. |
165
|
|
|
* |
166
|
|
|
* @param string $message |
167
|
|
|
* @return object |
168
|
|
|
*/ |
169
|
|
|
public function send($message) |
170
|
|
|
{ |
171
|
|
|
$this->addParam('message', rawurlencode($message)); |
172
|
|
|
|
173
|
|
|
return $this->post(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Send the message after setting the flag for test. |
178
|
|
|
* |
179
|
|
|
* @param bool $test |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function test($test) |
183
|
|
|
{ |
184
|
|
|
if ($test) { |
185
|
|
|
$this->addParam('test', true); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Make the request to the api endpoint for sending the message. |
193
|
|
|
* |
194
|
|
|
* @return object |
195
|
|
|
* @throws \Exception |
196
|
|
|
*/ |
197
|
|
|
public function post() |
198
|
|
|
{ |
199
|
|
|
$params = ['form_params' => $this->params]; |
200
|
|
|
try { |
201
|
|
|
$response = $this->http->request('POST', $this->url, $params, ['verify' => false, 'timeout' => 60]); |
202
|
|
|
$data = json_decode($response->getBody()->getContents()); |
203
|
|
|
// var_dump('TextlocalClient post response'); |
204
|
|
|
// var_dump(json_encode($data)); |
205
|
|
|
|
206
|
|
|
return $this->handleResponse($data); |
207
|
|
|
} catch (RequestException | SeekException | TransferException $e) { |
208
|
|
|
throw $e; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Handle the response from the api endpoint. |
214
|
|
|
* |
215
|
|
|
* @param object $data |
216
|
|
|
* @return object |
217
|
|
|
* @throws CouldNotSendNotification |
218
|
|
|
*/ |
219
|
|
|
public function handleResponse($data) |
220
|
|
|
{ |
221
|
|
|
if ($data->status === 'failure') { |
222
|
|
|
foreach ($data->errors as $error) { |
223
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($error); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $data; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|