21 public const ID =
'smslineby';
48 return is_array($from) ? $from : [];
60 public function register(array $fields)
62 $login = (string) $fields[
'login'];
63 $key = (string) $fields[
'api_key'];
65 $result = $this->sendGetRequest(
'balance/packets', $this->makeSignature(
'balancepackets', $key), $login);
66 if ($result->isSuccess())
89 return 'https://mobilemarketing.by/login/';
97 $result->addError(
new Error(
'Service is unavailable'));
102 'target' => $messageFields[
'MESSAGE_FROM'],
103 'msisdn' => str_replace(
'+',
'', $messageFields[
'MESSAGE_TO']),
109 $apiResult = $this->sendPostRequest(
'messages/single/sms', $message);
110 $result->setServiceRequest($apiResult->getHttpRequest());
111 $result->setServiceResponse($apiResult->getHttpResponse());
113 if (!$apiResult->isSuccess())
115 $result->addErrors($apiResult->getErrors());
119 $resultData = $apiResult->getData();
120 $smsData = $resultData[
'message'];
121 $smsId = $smsData[
'id_message'];
125 $result->addError(
new Error($this->getErrorMessage(-1000)));
129 $result->setExternalId($smsId);
130 $result->setAccepted();
140 $result->setId($messageFields[
'ID']);
141 $result->setExternalId($messageFields[
'EXTERNAL_ID']);
145 $result->addError(
new Error(
'Service is unavailable'));
149 $apiResult = $this->sendGetRequest(
'messages/'.$result->getExternalId());
150 if (!$apiResult->isSuccess())
152 $result->addErrors($apiResult->getErrors());
156 $resultData = $apiResult->getData();
157 $smsData = $resultData[
'message'];
161 $result->addError(
new Error($this->getErrorMessage(-1000)));
164 $result->setStatusText($smsData[
'state'][
'name']);
165 $result->setStatusCode(self::resolveStatus($smsData[
'state'][
'state']));
173 $status = parent::resolveStatus($serviceStatus);
175 switch ($serviceStatus)
178 return MessageService\MessageStatus::READ;
181 return MessageService\MessageStatus::DELIVERED;
184 return MessageService\MessageStatus::ACCEPTED;
187 return MessageService\MessageStatus::SENT;
189 case 'undeliverable':
192 return MessageService\MessageStatus::UNDELIVERED;
196 case 'textblacklist':
201 return MessageService\MessageStatus::FAILED;
212 $this->loadFromList();
217 private function loadFromList()
219 $result = $this->sendGetRequest(
'balance/packets');
221 if ($result->isSuccess())
224 $resultData = $result->getData();
225 foreach ($resultData[
'packets'] as $packet)
227 if (isset($packet[
'targets']) && is_array($packet[
'targets']))
229 foreach ($packet[
'targets'] as $target)
243 private function makeSignature($text, $key =
null)
250 return hash_hmac(
"sha256", $text, $key);
253 private function sendGetRequest($path, $signature =
null, $login =
null)
257 $signature = $this->makeSignature(str_replace(
'/',
'', $path));
268 private function sendPostRequest($path, array $params)
271 $requestBody = Json::encode($params);
272 $signature = $this->makeSignature(str_replace(
'/',
'', $path).$requestBody);
278 private function sendHttpRequest($method, $login, $signature, $path, $body =
null): Sender\Result\HttpRequestResult
280 $httpClient =
new HttpClient(array(
281 "socketTimeout" => $this->socketTimeout,
282 "streamTimeout" => $this->streamTimeout,
283 "waitResponse" =>
true,
285 $httpClient->setCharset(
'UTF-8');
286 $httpClient->setHeader(
'User-Agent',
'Bitrix24');
287 $httpClient->setHeader(
'Content-Type',
'application/json');
288 $httpClient->setHeader(
'Authorization-User', $login);
289 $httpClient->setHeader(
'Authorization',
"Bearer $signature");
291 $result =
new Sender\Result\HttpRequestResult();
292 $answer = [
'error' => -1000];
294 $url = self::JSON_API_URL.$path;
296 $result->setHttpRequest(
new MessageService\DTO\Request([
299 'headers' => method_exists($httpClient,
'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
302 if ($httpClient->query($method, $url, $body))
306 $answer = Json::decode($httpClient->getResult());
308 catch (\
Bitrix\Main\ArgumentException $e)
313 if (isset($answer[
'error']))
315 $result->addError(
new Error($this->getErrorMessage($answer[
'error'][
'code'], $answer[
'error'][
'message'])));
318 $result->setHttpResponse(
new MessageService\DTO\Response([
319 'statusCode' => $httpClient->getStatus(),
320 'headers' => $httpClient->getHeaders()->toArray(),
321 'body' => $httpClient->getResult(),
325 $result->setData($answer);
330 private function getErrorMessage($errorCode, $text =
null)
332 $message =
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSLINE_ERROR_'.$errorCode,
null,
'ru');
333 return $message ?: ($text ?:
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSLINE_ERROR_OTHER',
null,
'ru'));
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static isModuleInstalled($moduleName)