1 | <?php |
||||
2 | namespace mihaiomg\WebAlert; |
||||
3 | |||||
4 | class WebAlert |
||||
5 | { |
||||
6 | |||||
7 | private $baseUrl = 'https://www.app.webalert.ro/'; |
||||
8 | function __construct() |
||||
9 | { |
||||
10 | $this->apikey = config('webalert.apikey'); |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
11 | $this->USE_SPECIFIED = config('webalert.USE_SPECIFIED'); |
||||
0 ignored issues
–
show
|
|||||
12 | $this->USE_ALL_DEVICES = config('webalert.USE_ALL_DEVICES'); |
||||
0 ignored issues
–
show
|
|||||
13 | $this->USE_ALL_SIMS = config('webalert.USE_ALL_SIMS'); |
||||
0 ignored issues
–
show
|
|||||
14 | } |
||||
15 | |||||
16 | /** |
||||
17 | * @param string $number The mobile number where you want to send message. |
||||
18 | * @param string $message The message you want to send. |
||||
19 | * @param int|string $device The ID of a device you want to use to send this message. |
||||
20 | * @param int $schedule Set it to timestamp when you want to send this message. |
||||
21 | * @param bool $isMMS Set it to true if you want to send MMS message instead of SMS. |
||||
22 | * @param string $attachments Comma separated list of image links you want to attach to the message. Only works for MMS messages. |
||||
23 | * @param bool $prioritize Set it to true if you want to prioritize this message. |
||||
24 | * |
||||
25 | * @return array Returns The array containing information about the message. |
||||
26 | * @throws Exception If there is an error while sending a message. |
||||
27 | */ |
||||
28 | function sendSingleMessage($number, $message, $device = 0, $schedule = null, $isMMS = false, $attachments = null, $prioritize = false) |
||||
29 | { |
||||
30 | |||||
31 | $url = $this->baseUrl."/services/send.php"; |
||||
32 | $postData = array( |
||||
33 | 'number' => $number, |
||||
34 | 'message' => $message, |
||||
35 | 'schedule' => $schedule, |
||||
36 | 'key' => $this->apikey, |
||||
37 | 'devices' => $device, |
||||
38 | 'type' => $isMMS ? "mms" : "sms", |
||||
39 | 'attachments' => $attachments, |
||||
40 | 'prioritize' => $prioritize ? 1 : 0 |
||||
41 | ); |
||||
42 | return $this->sendRequest($url, $postData)["messages"][0]; |
||||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * @param array $messages The array containing numbers and messages. |
||||
47 | * @param int $option Set this to USE_SPECIFIED if you want to use devices and SIMs specified in devices argument. |
||||
48 | * Set this to USE_ALL_DEVICES if you want to use all available devices and their default SIM to send messages. |
||||
49 | * Set this to USE_ALL_SIMS if you want to use all available devices and all their SIMs to send messages. |
||||
50 | * @param array $devices The array of ID of devices you want to use to send these messages. |
||||
51 | * @param int $schedule Set it to timestamp when you want to send these messages. |
||||
52 | * @param bool $useRandomDevice Set it to true if you want to send messages using only one random device from selected devices. |
||||
53 | * |
||||
54 | * @return array Returns The array containing messages. |
||||
55 | * For example :- |
||||
56 | * [ |
||||
57 | * 0 => [ |
||||
58 | * "ID" => "1", |
||||
59 | * "number" => "+11234567890", |
||||
60 | * "message" => "This is a test message.", |
||||
61 | * "deviceID" => "1", |
||||
62 | * "simSlot" => "0", |
||||
63 | * "userID" => "1", |
||||
64 | * "status" => "Pending", |
||||
65 | * "type" => "sms", |
||||
66 | * "attachments" => null, |
||||
67 | * "sentDate" => "2018-10-20T00:00:00+02:00", |
||||
68 | * "deliveredDate" => null |
||||
69 | * "groupID" => ")V5LxqyBMEbQrl9*J$5bb4c03e8a07b7.62193871" |
||||
70 | * ] |
||||
71 | * ] |
||||
72 | * @throws Exception If there is an error while sending messages. |
||||
73 | */ |
||||
74 | function sendMessages($messages, $devices = [], $schedule = null, $useRandomDevice = false) |
||||
75 | { |
||||
76 | $url = $this->baseUrl."/services/send.php"; |
||||
77 | $postData = [ |
||||
78 | 'messages' => json_encode($messages), |
||||
79 | 'schedule' => $schedule, |
||||
80 | 'key' => $this->apikey, |
||||
81 | 'devices' => json_encode($devices), |
||||
82 | 'option' => $this->USE_SPECIFIED, |
||||
83 | 'useRandomDevice' => $useRandomDevice |
||||
84 | ]; |
||||
85 | return $this->sendRequest($url, $postData)["messages"]; |
||||
86 | } |
||||
87 | |||||
88 | /** |
||||
89 | * @param int $listID The ID of the contacts list where you want to send this message. |
||||
90 | * @param string $message The message you want to send. |
||||
91 | * @param int $option Set this to USE_SPECIFIED if you want to use devices and SIMs specified in devices argument. |
||||
92 | * Set this to USE_ALL_DEVICES if you want to use all available devices and their default SIM to send messages. |
||||
93 | * Set this to USE_ALL_SIMS if you want to use all available devices and all their SIMs to send messages. |
||||
94 | * @param array $devices The array of ID of devices you want to use to send the message. |
||||
95 | * @param int $schedule Set it to timestamp when you want to send this message. |
||||
96 | * @param bool $isMMS Set it to true if you want to send MMS message instead of SMS. |
||||
97 | * @param string $attachments Comma separated list of image links you want to attach to the message. Only works for MMS messages. |
||||
98 | * |
||||
99 | * @return array Returns The array containing messages. |
||||
100 | * @throws Exception If there is an error while sending messages. |
||||
101 | */ |
||||
102 | function sendMessageToContactsList($listID, $message, $devices = [], $schedule = null, $isMMS = false, $attachments = null) |
||||
103 | { |
||||
104 | $url = $this->baseUrl."/services/send.php"; |
||||
105 | $postData = [ |
||||
106 | 'listID' => $listID, |
||||
107 | 'message' => $message, |
||||
108 | 'schedule' => $schedule, |
||||
109 | 'key' => $this->apikey, |
||||
110 | 'devices' => json_encode($devices), |
||||
111 | 'option' => $$this->USE_SPECIFIED, |
||||
112 | 'type' => $isMMS ? "mms" : "sms", |
||||
113 | 'attachments' => $attachments |
||||
114 | ]; |
||||
115 | return $this->sendRequest($url, $postData)["messages"]; |
||||
116 | } |
||||
117 | |||||
118 | /** |
||||
119 | * @param int $id The ID of a message you want to retrieve. |
||||
120 | * |
||||
121 | * @return array The array containing a message. |
||||
122 | * @throws Exception If there is an error while getting a message. |
||||
123 | */ |
||||
124 | function getMessageByID($id) |
||||
125 | { |
||||
126 | $url = $this->baseUrl."/services/read-messages.php"; |
||||
127 | $postData = [ |
||||
128 | 'key' => $this->apikey, |
||||
129 | 'id' => $id |
||||
130 | ]; |
||||
131 | return $this->sendRequest($url, $postData)["messages"][0]; |
||||
132 | } |
||||
133 | |||||
134 | /** |
||||
135 | * @param string $groupID The group ID of messages you want to retrieve. |
||||
136 | * |
||||
137 | * @return array The array containing messages. |
||||
138 | * @throws Exception If there is an error while getting messages. |
||||
139 | */ |
||||
140 | function getMessagesByGroupID($groupID) |
||||
141 | { |
||||
142 | $url = $this->baseUrl."/services/read-messages.php"; |
||||
143 | $postData = [ |
||||
144 | 'key' => $this->apikey, |
||||
145 | 'groupId' => $groupID |
||||
146 | ]; |
||||
147 | return $this->sendRequest($url, $postData)["messages"]; |
||||
148 | } |
||||
149 | |||||
150 | /** |
||||
151 | * @param string $status The status of messages you want to retrieve. |
||||
152 | * @param int $deviceID The deviceID of the device which messages you want to retrieve. |
||||
153 | * @param int $simSlot Sim slot of the device which messages you want to retrieve. Similar to array index. 1st slot is 0 and 2nd is 1. |
||||
154 | * @param int $startTimestamp Search for messages sent or received after this time. |
||||
155 | * @param int $endTimestamp Search for messages sent or received before this time. |
||||
156 | * |
||||
157 | * @return array The array containing messages. |
||||
158 | * @throws Exception If there is an error while getting messages. |
||||
159 | */ |
||||
160 | function getMessagesByStatus($status, $deviceID = null, $simSlot = null, $startTimestamp = null, $endTimestamp = null) |
||||
161 | { |
||||
162 | $url = $this->baseUrl."/services/read-messages.php"; |
||||
163 | $postData = [ |
||||
164 | 'key' => $this->apikey, |
||||
165 | 'status' => $status, |
||||
166 | 'deviceID' => $deviceID, |
||||
167 | 'simSlot' => $simSlot, |
||||
168 | 'startTimestamp' => $startTimestamp, |
||||
169 | 'endTimestamp' => $endTimestamp |
||||
170 | ]; |
||||
171 | return $this->sendRequest($url, $postData)["messages"]; |
||||
172 | } |
||||
173 | |||||
174 | /** |
||||
175 | * @param int $id The ID of a message you want to resend. |
||||
176 | * |
||||
177 | * @return array The array containing a message. |
||||
178 | * @throws Exception If there is an error while resending a message. |
||||
179 | */ |
||||
180 | function resendMessageByID($id) |
||||
181 | { |
||||
182 | $url = $this->baseUrl."/services/resend.php"; |
||||
183 | $postData = [ |
||||
184 | 'key' => $this->apikey, |
||||
185 | 'id' => $id |
||||
186 | ]; |
||||
187 | return $this->sendRequest($url, $postData)["messages"][0]; |
||||
188 | } |
||||
189 | |||||
190 | /** |
||||
191 | * @param string $groupID The group ID of messages you want to resend. |
||||
192 | * @param string $status The status of messages you want to resend. |
||||
193 | * |
||||
194 | * @return array The array containing messages. |
||||
195 | * @throws Exception If there is an error while resending messages. |
||||
196 | */ |
||||
197 | function resendMessagesByGroupID($groupID, $status = null) |
||||
198 | { |
||||
199 | $url = $this->baseUrl."/services/resend.php"; |
||||
200 | $postData = [ |
||||
201 | 'key' => $this->apikey, |
||||
202 | 'groupId' => $groupID, |
||||
203 | 'status' => $status |
||||
204 | ]; |
||||
205 | return $this->sendRequest($url, $postData)["messages"]; |
||||
206 | } |
||||
207 | |||||
208 | /** |
||||
209 | * @param string $status The status of messages you want to resend. |
||||
210 | * @param int $deviceID The deviceID of the device which messages you want to resend. |
||||
211 | * @param int $simSlot Sim slot of the device which messages you want to resend. Similar to array index. 1st slot is 0 and 2nd is 1. |
||||
212 | * @param int $startTimestamp Resend messages sent or received after this time. |
||||
213 | * @param int $endTimestamp Resend messages sent or received before this time. |
||||
214 | * |
||||
215 | * @return array The array containing messages. |
||||
216 | * @throws Exception If there is an error while resending messages. |
||||
217 | */ |
||||
218 | function resendMessagesByStatus($status, $deviceID = null, $simSlot = null, $startTimestamp = null, $endTimestamp = null) |
||||
219 | { |
||||
220 | $url = $this->baseUrl."/services/resend.php"; |
||||
221 | $postData = [ |
||||
222 | 'key' => $this->apikey, |
||||
223 | 'status' => $status, |
||||
224 | 'deviceID' => $deviceID, |
||||
225 | 'simSlot' => $simSlot, |
||||
226 | 'startTimestamp' => $startTimestamp, |
||||
227 | 'endTimestamp' => $endTimestamp |
||||
228 | ]; |
||||
229 | return $this->sendRequest($url, $postData)["messages"]; |
||||
230 | } |
||||
231 | |||||
232 | /** |
||||
233 | * @param int $listID The ID of the contacts list where you want to add this contact. |
||||
234 | * @param string $number The mobile number of the contact. |
||||
235 | * @param string $name The name of the contact. |
||||
236 | * @param bool $resubscribe Set it to true if you want to resubscribe this contact if it already exists. |
||||
237 | * |
||||
238 | * @return array The array containing a newly added contact. |
||||
239 | * @throws Exception If there is an error while adding a new contact. |
||||
240 | */ |
||||
241 | function addContact($listID, $number, $name = null, $resubscribe = false) |
||||
242 | { |
||||
243 | $url = $this->baseUrl."/services/manage-contacts.php"; |
||||
244 | $postData = [ |
||||
245 | 'key' => $this->apikey, |
||||
246 | 'listID' => $listID, |
||||
247 | 'number' => $number, |
||||
248 | 'name' => $name, |
||||
249 | 'resubscribe' => $resubscribe |
||||
250 | ]; |
||||
251 | return $this->sendRequest($url, $postData)["contact"]; |
||||
252 | } |
||||
253 | |||||
254 | /** |
||||
255 | * @param int $listID The ID of the contacts list from which you want to unsubscribe this contact. |
||||
256 | * @param string $number The mobile number of the contact. |
||||
257 | * |
||||
258 | * @return array The array containing the unsubscribed contact. |
||||
259 | * @throws Exception If there is an error while setting subscription to false. |
||||
260 | */ |
||||
261 | function unsubscribeContact($listID, $number) |
||||
262 | { |
||||
263 | $url = $this->baseUrl."/services/manage-contacts.php"; |
||||
264 | $postData = [ |
||||
265 | 'key' => $this->apikey, |
||||
266 | 'listID' => $listID, |
||||
267 | 'number' => $number, |
||||
268 | 'unsubscribe' => true |
||||
269 | ]; |
||||
270 | return $this->sendRequest($url, $postData)["contact"]; |
||||
271 | } |
||||
272 | |||||
273 | /** |
||||
274 | * @return string The amount of message credits left. |
||||
275 | * @throws Exception If there is an error while getting message credits. |
||||
276 | */ |
||||
277 | function getBalance() |
||||
278 | { |
||||
279 | $url = "https://www.webalert.ro/services/send.php"; |
||||
280 | $postData = [ |
||||
281 | 'key' => $this->apikey, |
||||
282 | ]; |
||||
283 | $credits = $this->sendRequest($url, $postData)["credits"]; |
||||
284 | return is_null($credits) ? "Nelimitat" : $credits; |
||||
285 | } |
||||
286 | |||||
287 | /** |
||||
288 | * @param string $request USSD request you want to execute. e.g. *150# |
||||
289 | * @param int $device The ID of a device you want to use to send this message. |
||||
290 | * @param int|null $simSlot Sim you want to use for this USSD request. Similar to array index. 1st slot is 0 and 2nd is 1. |
||||
291 | * |
||||
292 | * @return array The array containing details about USSD request that was sent. |
||||
293 | * @throws Exception If there is an error while sending a USSD request. |
||||
294 | */ |
||||
295 | function sendUssdRequest($request, $device, $simSlot = null) |
||||
296 | { |
||||
297 | $url = $this->baseUrl."/services/send-ussd-request.php"; |
||||
298 | $postData = [ |
||||
299 | 'key' => $this->apikey, |
||||
300 | 'request' => $request, |
||||
301 | 'device' => $device, |
||||
302 | 'sim' => $simSlot |
||||
303 | ]; |
||||
304 | return $this->sendRequest($url, $postData)["request"]; |
||||
305 | } |
||||
306 | |||||
307 | /** |
||||
308 | * @param int $id The ID of a USSD request you want to retrieve. |
||||
309 | * |
||||
310 | * @return array The array containing details about USSD request you requested. |
||||
311 | * @throws Exception If there is an error while getting a USSD request. |
||||
312 | */ |
||||
313 | function getUssdRequestByID($id) |
||||
314 | { |
||||
315 | $url = $this->baseUrl."/services/read-ussd-requests.php"; |
||||
316 | $postData = [ |
||||
317 | 'key' => $this->apikey, |
||||
318 | 'id' => $id |
||||
319 | ]; |
||||
320 | return $this->sendRequest($url, $postData)["requests"][0]; |
||||
321 | } |
||||
322 | |||||
323 | /** |
||||
324 | * @param string $request The request text you want to look for. |
||||
325 | * @param int $deviceID The deviceID of the device which USSD requests you want to retrieve. |
||||
326 | * @param int $simSlot Sim slot of the device which USSD requests you want to retrieve. Similar to array index. 1st slot is 0 and 2nd is 1. |
||||
327 | * @param int|null $startTimestamp Search for USSD requests sent after this time. |
||||
328 | * @param int|null $endTimestamp Search for USSD requests sent before this time. |
||||
329 | * |
||||
330 | * @return array The array containing USSD requests. |
||||
331 | * @throws Exception If there is an error while getting USSD requests. |
||||
332 | */ |
||||
333 | function getUssdRequests($request, $deviceID = null, $simSlot = null, $startTimestamp = null, $endTimestamp = null) |
||||
334 | { |
||||
335 | $url = $this->baseUrl."/services/read-ussd-requests.php"; |
||||
336 | $postData = [ |
||||
337 | 'key' => $this->apikey, |
||||
338 | 'request' => $request, |
||||
339 | 'deviceID' => $deviceID, |
||||
340 | 'simSlot' => $simSlot, |
||||
341 | 'startTimestamp' => $startTimestamp, |
||||
342 | 'endTimestamp' => $endTimestamp |
||||
343 | ]; |
||||
344 | return $this->sendRequest($url, $postData)["requests"]; |
||||
345 | } |
||||
346 | |||||
347 | function sendRequest($url, $postData) |
||||
348 | { |
||||
349 | $ch = curl_init(); |
||||
350 | curl_setopt($ch, CURLOPT_URL, $url); |
||||
351 | curl_setopt($ch, CURLOPT_POST, true); |
||||
352 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
353 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); |
||||
354 | $response = curl_exec($ch); |
||||
355 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||||
356 | if (curl_errno($ch)) { |
||||
357 | throw new \Exception(curl_error($ch)); |
||||
358 | } |
||||
359 | curl_close($ch); |
||||
360 | if ($httpCode == 200) { |
||||
361 | $json = json_decode($response, true); |
||||
362 | if ($json == false) { |
||||
363 | if (empty($response)) { |
||||
364 | throw new \Exception("Missing data in request. Please provide all the required information to send messages."); |
||||
365 | } else { |
||||
366 | throw new \Exception($response); |
||||
367 | } |
||||
368 | } else { |
||||
369 | if ($json["success"]) { |
||||
370 | return $json["data"]; |
||||
371 | } else { |
||||
372 | throw new \Exception($json["error"]["message"]); |
||||
373 | } |
||||
374 | } |
||||
375 | } else { |
||||
376 | throw new \Exception("HTTP Error Code : {$httpCode}"); |
||||
377 | } |
||||
378 | } |
||||
379 | } |
||||
380 | |||||
381 | ?> |
||||
382 |