21 public const ID =
'ismscenter';
23 private const JSON_API_URL =
'http://isms.center/api/sms/';
29 || Option::get(
'messageservice',
'ismscenter_enabled',
'N') ===
'Y'
52 return is_array($from) ? $from : [];
64 public function register(array $fields)
66 $login = (string)$fields[
'login'];
67 $psw = (string)$fields[
'psw'];
68 $from = (string) $fields[
'from_list'];
72 if ($login && $psw && $from)
81 return [
'id' => $v,
'name' => $v];
90 $result->addError(
new Error(
'Empty required fields.'));
112 return 'https://isms.center/ru';
120 $result->addError(
new Error(
'Service is unavailable'));
126 'from' => $messageFields[
'MESSAGE_FROM'],
127 'to' => str_replace(
'+',
'', $messageFields[
'MESSAGE_TO']),
133 $apiResult = $this->sendApiRequest(
'send', $message);
135 if (!$apiResult->isSuccess())
137 $result->addErrors($apiResult->getErrors());
141 $smsData = $apiResult->getData();
142 $smsId = $smsData[
'message_id'];
146 $result->addError(
new Error(
'Service error.'));
150 $result->setExternalId($smsId);
151 $result->setAccepted();
161 $result->setId($messageFields[
'ID']);
162 $result->setExternalId($messageFields[
'EXTERNAL_ID']);
166 $result->addError(
new Error(
'Service is unavailable'));
171 $apiResult = $this->sendApiRequest(
'report', [
172 'message_id' => $result->getExternalId(),
175 if (!$apiResult->isSuccess())
177 $result->addErrors($apiResult->getErrors());
181 $smsData = $apiResult->getData();
185 $result->addError(
new Error(
'Service error.'));
188 $result->setStatusText($smsData[
'status']);
189 $result->setStatusCode(self::resolveStatus($smsData[
'status']));
197 switch ((
string)$serviceStatus)
200 return MessageService\MessageStatus::QUEUED;
203 return MessageService\MessageStatus::SENDING;
206 return MessageService\MessageStatus::SENT;
209 return MessageService\MessageStatus::DELIVERED;
212 return MessageService\MessageStatus::UNDELIVERED;
216 return parent::resolveStatus($serviceStatus);
219 private function sendApiRequest($path, array $params)
224 return $this->sendHttpRequest($path, $login, $psw, $params);
230 "socketTimeout" => $this->socketTimeout,
231 "streamTimeout" => $this->streamTimeout,
232 'waitResponse' =>
true,
234 $httpClient->setCharset(
'UTF-8');
235 $httpClient->setHeader(
'User-Agent',
'Bitrix24');
236 $httpClient->setHeader(
'Content-Type',
'application/json');
237 $httpClient->setAuthorization($login, $psw);
242 'error_message' =>
'Service error'
246 $url = self::JSON_API_URL . $path;
247 $body = Json::encode($params);
249 if ($path ===
'report')
253 $url .=
'?' . http_build_query($params);
257 $result->setHttpRequest(
new DTO\
Request([
260 'headers' => method_exists($httpClient,
'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
264 if ($httpClient->query($method, $url, $body))
268 $answer = Json::decode($httpClient->getResult());
269 }
catch (\
Bitrix\Main\ArgumentException $e)
274 if (isset($answer[
'error_code']))
276 $result->addError(
new Error($answer[
'error_message'], $answer[
'error_code']));
279 $result->setHttpResponse(
new DTO\Response([
280 'statusCode' => $httpClient->getStatus(),
281 'headers' => $httpClient->getHeaders()->toArray(),
282 'body' => $httpClient->getResult(),
286 $result->setData($answer);
static isModuleInstalled($moduleName)