Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ismscenter.php
1<?php
2
4
11
16
18
20{
21 public const ID = 'ismscenter';
22
23 private const JSON_API_URL = 'http://isms.center/api/sms/';
24
25 public static function isSupported()
26 {
27 return (
29 || Option::get('messageservice', 'ismscenter_enabled', 'N') === 'Y'
30 );
31 }
32
33 public function getId()
34 {
35 return static::ID;
36 }
37
38 public function getName()
39 {
40 return 'isms.center';
41 }
42
43 public function getShortName()
44 {
45 return 'isms.center';
46 }
47
48 public function getFromList()
49 {
50 $from = $this->getOption('from_list');
51
52 return is_array($from) ? $from : [];
53 }
54
55 public function isRegistered()
56 {
57 return (
58 $this->getOption('login') !== null
59 &&
60 $this->getOption('psw') !== null
61 );
62 }
63
64 public function register(array $fields)
65 {
66 $login = (string)$fields['login'];
67 $psw = (string)$fields['psw'];
68 $from = (string) $fields['from_list'];
69
70 $result = new Result();
71
72 if ($login && $psw && $from)
73 {
74 $this->setOption('login', $login);
75 $this->setOption('psw', $psw);
76
77 $from = array_map(
78 function($v) {
79 $v = trim($v);
80
81 return ['id' => $v, 'name' => $v];
82 },
83 explode(';', $from)
84 );
85
86 $this->setOption('from_list', $from);
87 }
88 else
89 {
90 $result->addError(new Error('Empty required fields.'));
91 }
92
93 return $result;
94 }
95
102 public function getOwnerInfo()
103 {
104 return [
105 'login' => $this->getOption('login'),
106 'fromList' => $this->getFromList(),
107 ];
108 }
109
110 public function getExternalManageUrl()
111 {
112 return 'https://isms.center/ru';
113 }
114
115 public function sendMessage(array $messageFields)
116 {
117 if (!$this->canUse())
118 {
119 $result = new SendMessage();
120 $result->addError(new Error('Service is unavailable'));
121
122 return $result;
123 }
124
125 $message = [
126 'from' => $messageFields['MESSAGE_FROM'],
127 'to' => str_replace('+', '', $messageFields['MESSAGE_TO']),
128 'text' => $this->prepareMessageBodyForSend($messageFields['MESSAGE_BODY']),
129 'notify_url' => $this->getCallbackUrl()
130 ];
131
132 $result = new SendMessage();
133 $apiResult = $this->sendApiRequest('send', $message);
134
135 if (!$apiResult->isSuccess())
136 {
137 $result->addErrors($apiResult->getErrors());
138 }
139 else
140 {
141 $smsData = $apiResult->getData();
142 $smsId = $smsData['message_id'];
143
144 if (!$smsId)
145 {
146 $result->addError(new Error('Service error.'));
147 }
148 else
149 {
150 $result->setExternalId($smsId);
151 $result->setAccepted();
152 }
153 }
154
155 return $result;
156 }
157
158 public function getMessageStatus(array $messageFields)
159 {
160 $result = new MessageStatus();
161 $result->setId($messageFields['ID']);
162 $result->setExternalId($messageFields['EXTERNAL_ID']);
163
164 if (!$this->canUse())
165 {
166 $result->addError(new Error('Service is unavailable'));
167
168 return $result;
169 }
170
171 $apiResult = $this->sendApiRequest('report', [
172 'message_id' => $result->getExternalId(),
173 ]);
174
175 if (!$apiResult->isSuccess())
176 {
177 $result->addErrors($apiResult->getErrors());
178 }
179 else
180 {
181 $smsData = $apiResult->getData();
182
183 if (!$smsData)
184 {
185 $result->addError(new Error('Service error.'));
186 }
187
188 $result->setStatusText($smsData['status']);
189 $result->setStatusCode(self::resolveStatus($smsData['status']));
190 }
191
192 return $result;
193 }
194
195 public static function resolveStatus($serviceStatus)
196 {
197 switch ((string)$serviceStatus)
198 {
199 case 'send':
200 return MessageService\MessageStatus::QUEUED;
201 break;
202 case 'sending':
203 return MessageService\MessageStatus::SENDING;
204 break;
205 case 'sent':
206 return MessageService\MessageStatus::SENT;
207 break;
208 case 'delivered':
209 return MessageService\MessageStatus::DELIVERED;
210 break;
211 case 'undelivered':
212 return MessageService\MessageStatus::UNDELIVERED;
213 break;
214 }
215
216 return parent::resolveStatus($serviceStatus);
217 }
218
219 private function sendApiRequest($path, array $params)
220 {
221 $login = $this->getOption('login');
222 $psw = $this->getOption('psw');
223
224 return $this->sendHttpRequest($path, $login, $psw, $params);
225 }
226
227 private function sendHttpRequest($path, $login, $psw, array $params): Sender\Result\HttpRequestResult
228 {
229 $httpClient = new HttpClient([
230 "socketTimeout" => $this->socketTimeout,
231 "streamTimeout" => $this->streamTimeout,
232 'waitResponse' => true,
233 ]);
234 $httpClient->setCharset('UTF-8');
235 $httpClient->setHeader('User-Agent', 'Bitrix24');
236 $httpClient->setHeader('Content-Type', 'application/json');
237 $httpClient->setAuthorization($login, $psw);
238
239 $result = new Sender\Result\HttpRequestResult();
240 $answer = [
241 'error_code' => 500,
242 'error_message' => 'Service error'
243 ];
244
245 $method = HttpClient::HTTP_POST;
246 $url = self::JSON_API_URL . $path;
247 $body = Json::encode($params);
248
249 if ($path === 'report')
250 {
251 $method = HttpClient::HTTP_GET;
252
253 $url .= '?' . http_build_query($params);
254 $body = null;
255 }
256
257 $result->setHttpRequest(new DTO\Request([
258 'method' => $method,
259 'uri' => $url,
260 'headers' => method_exists($httpClient, 'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
261 'body' => $params
262 ]));
263
264 if ($httpClient->query($method, $url, $body))
265 {
266 try
267 {
268 $answer = Json::decode($httpClient->getResult());
269 } catch (\Bitrix\Main\ArgumentException $e)
270 {
271 }
272 }
273
274 if (isset($answer['error_code']))
275 {
276 $result->addError(new Error($answer['error_message'], $answer['error_code']));
277 }
278
279 $result->setHttpResponse(new DTO\Response([
280 'statusCode' => $httpClient->getStatus(),
281 'headers' => $httpClient->getHeaders()->toArray(),
282 'body' => $httpClient->getResult(),
283 'error' => Sender\Util::getHttpClientErrorString($httpClient)
284 ]));
285
286 $result->setData($answer);
287
288 return $result;
289 }
290}
static isModuleInstalled($moduleName)
getOption($optionName, $defaultValue=null)
prepareMessageBodyForSend(string $text)
Definition base.php:176
static getHttpClientErrorString(HttpClient $httpClient)
Definition util.php:24