23 use Sender\Traits\RussianProvider;
25 public const ID =
'smsastby';
27 const JSON_API_URL =
'https://userarea.sms-assistent.by/api/v1/json';
28 const PLAIN_API_URL =
'https://userarea.sms-assistent.by/api/v1/%s/plain';
42 return 'sms-assistent.by';
48 return is_array($from) ? $from : [];
60 public function register(array $fields)
63 'user' => $fields[
'account_user'],
64 'password' => $fields[
'account_password'],
67 $result = $this->callPlainApi(
'credits', $params);
68 if ($result->isSuccess())
71 $this->
setOption(
'user', $params[
'user']);
72 $this->
setOption(
'password', $params[
'password']);
83 'user_uuid' =>
'wsTKC5KdazmEjBNU8i5fqacddtOeqQBtRc45lcvO6WA=',
84 "diler_uuid" =>
'obUrbkWuyW0nYL9D2rR6s3EPMm1QHg9h/KWtO8jFIug=',
85 'email' => $fields[
'email'],
86 'tel' =>
'+'.\NormalizePhone($fields[
'tel']),
87 'unp' => $fields[
'unp']
91 $result = $this->callJsonApi(
'demo', $params);
93 if ($result->isSuccess())
96 $this->
setOption(
'demo_email', $fields[
'email']);
97 $this->
setOption(
'demo_tel',
'+'.\NormalizePhone($fields[
'tel']));
98 $this->
setOption(
'demo_unp', $fields[
'unp']);
122 'email' => $this->
getOption(
'demo_email'),
130 return 'https://userarea.sms-assistent.by/';
138 $result->addError(
new Error(
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSASTBY_CAN_USE_ERROR')));
143 'recipient' => $messageFields[
'MESSAGE_TO'],
145 'validity_period' => 24,
151 $message[
'Vendor'] =
'Bitrix24';
154 if ($messageFields[
'MESSAGE_FROM'])
156 $message[
'sender'] = $messageFields[
'MESSAGE_FROM'];
160 $apiResult = $this->callPlainApi(
'send_sms', $message);
161 $result->setServiceRequest($apiResult->getHttpRequest());
162 $result->setServiceResponse($apiResult->getHttpResponse());
163 $resultData = $apiResult->getData();
165 if (!$apiResult->isSuccess())
167 $result->addErrors($apiResult->getErrors());
171 $smsId = $resultData[
'response'];
175 $result->addError(
new Error($this->getErrorMessage(-1000)));
179 $result->setExternalId($smsId);
180 $result->setAccepted();
188 private function sendMessageBulk(array $messageFields)
193 $result->addError(
new Error(
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSASTBY_CAN_USE_ERROR')));
198 'recepient' => $messageFields[
'MESSAGE_TO'],
202 if ($messageFields[
'MESSAGE_FROM'])
204 $message[
'sender'] = $messageFields[
'MESSAGE_FROM'];
208 $apiResult = $this->callJsonApi(
'sms_send', [
211 'validity_period' => 24
218 $resultData = $apiResult->getData();
220 if (!$apiResult->isSuccess())
222 $result->addErrors($apiResult->getErrors());
226 $smsData = current($resultData[
'message'][
'msg']);
230 $result->addError(
new Error($this->getErrorMessage(-1000)));
234 if (isset($smsData[
'sms_id']))
236 $result->setExternalId($smsData[
'sms_id']);
239 if ($smsData[
'error_code'] > 0)
241 $result->addError(
new Error($this->getErrorMessage($smsData[
'error_code'])));
245 $result->setAccepted();
256 $result->setId($messageFields[
'ID']);
257 $result->setExternalId($messageFields[
'EXTERNAL_ID']);
261 $result->addError(
new Error(
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSASTBY_CAN_USE_ERROR')));
269 'sms_id' => $result->getExternalId()
275 $apiResult = $this->callJsonApi(
'statuses', $params);
276 if (!$apiResult->isSuccess())
278 $result->addErrors($apiResult->getErrors());
282 $resultData = $apiResult->getData();
283 $smsData = current($resultData[
'status'][
'msg']);
287 $result->addError(
new Error($this->getErrorMessage(-1000)));
290 $result->setStatusText($smsData[
'sms_status']);
291 $result->setStatusCode(self::resolveStatus($smsData[
'sms_status']));
299 $status = parent::resolveStatus($serviceStatus);
301 switch ($serviceStatus)
304 return MessageService\MessageStatus::ACCEPTED;
307 return MessageService\MessageStatus::SENT;
310 return MessageService\MessageStatus::DELIVERED;
316 return MessageService\MessageStatus::UNDELIVERED;
327 $this->loadFromList();
334 $url = sprintf(self::PLAIN_API_URL, $command);
337 "socketTimeout" => $this->socketTimeout,
338 "streamTimeout" => $this->streamTimeout,
339 "waitResponse" =>
true,
341 $httpClient->setHeader(
'User-Agent',
'Bitrix24');
342 $httpClient->setCharset(
'UTF-8');
346 if (!isset($params[
'user']))
348 $params[
'user'] = $this->
getOption(
'user');
351 if (!isset($params[
'password']))
353 $params[
'password'] = $this->
getOption(
'password');
358 $params = \Bitrix\Main\Text\Encoding::convertEncoding($params, SITE_CHARSET,
'UTF-8');
361 $result =
new Sender\Result\HttpRequestResult();
362 $result->setHttpRequest(
new MessageService\DTO\Request([
365 'headers' => method_exists($httpClient,
'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
372 $answer = $httpClient->getResult();
375 if (is_numeric($answer) && $answer < 0)
377 $result->addError(
new Error($this->getErrorMessage($answer)));
379 $result->setHttpResponse(
new MessageService\DTO\Response([
380 'statusCode' => $httpClient->getStatus(),
381 'headers' => $httpClient->getHeaders()->toArray(),
382 'body' => $httpClient->getResult(),
386 $result->setData([
'response' => $answer]);
391 private function callJsonApi($command, array $params = []): Sender\Result\HttpRequestResult
393 $httpClient =
new HttpClient(array(
394 "socketTimeout" => $this->socketTimeout,
395 "streamTimeout" => $this->streamTimeout,
396 "waitResponse" =>
true,
398 $httpClient->setHeader(
'User-Agent',
'Bitrix24');
399 $httpClient->setCharset(
'UTF-8');
400 $httpClient->setHeader(
'Content-Type',
'application/json');
404 $params[
'login'] = $this->
getOption(
'user');
407 if (!isset($params[
'password']) && $this->
isRegistered())
409 $params[
'password'] = $this->
getOption(
'password');
412 $params[
'command'] = $command;
414 $params = Json::encode($params);
416 $result =
new Sender\Result\HttpRequestResult();
417 $result->setHttpRequest(
new MessageService\DTO\Request([
419 'uri' => self::JSON_API_URL,
420 'headers' => method_exists($httpClient,
'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
425 if ($httpClient->query(
HttpClient::HTTP_POST, self::JSON_API_URL, $params) && $httpClient->getStatus() ==
'200')
429 $answer = Json::decode($httpClient->getResult());
431 catch (\
Bitrix\Main\ArgumentException $e)
433 $answer = [
'error' => -1000];
437 if (isset($answer[
'error']))
439 $result->addError(
new Error($this->getErrorMessage($answer[
'error'], $answer)));
442 if (isset($answer[
'status']) && $answer[
'status'] === -1)
444 $msg = !empty($answer[
'message']) ? $answer[
'message'] : $this->getErrorMessage($answer[
'error'], 1000);
445 $result->addError(
new Error($msg));
448 $result->setHttpResponse(
new MessageService\DTO\Response([
449 'statusCode' => $httpClient->getStatus(),
450 'headers' => $httpClient->getHeaders()->toArray(),
451 'body' => $httpClient->getResult(),
454 $result->setData($answer);
459 private function getErrorMessage($errorCode)
461 $errorCode = abs($errorCode);
462 $message =
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSASTBY_ERROR_'.$errorCode);
463 return $message ?:
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSASTBY_ERROR_OTHER');
466 private function loadFromList()
468 $result = $this->callJsonApi(
'get_senders');
470 if ($result->isSuccess())
473 $resultData = $result->getData();
474 foreach ($resultData[
'senders'] as
$sender)
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static isModuleInstalled($moduleName)