19 public const ID =
'mfmsru';
25 || Option::get(
'messageservice',
'mfmsru_enabled',
'N') ===
'Y'
47 return is_array($from) ? $from : [];
59 public function register(array $fields)
61 $login = (string) $fields[
'login'];
62 $password = (string) $fields[
'password'];
63 $from = (string) $fields[
'from_list'];
64 $sendUrl = (string) $fields[
'hpg_send_url'];
65 $statusUrl = (string) $fields[
'hpg_status_url'];
68 if ($login && $password && $from && $sendUrl && $statusUrl)
72 $this->
setOption(
'hpg_send_url', $sendUrl);
73 $this->
setOption(
'hpg_status_url', $statusUrl);
75 $from = array_map(
function($v) {
77 return [
'id' => $v,
'name' => $v];
78 }, explode(
';', $from));
84 $result->addError(
new Error(
'Empty required fields.'));
105 return 'https://mfms.ru';
113 $result->addError(
new Error(
'Service is unavailable'));
117 return $this->sendViaHpg($messageFields);
125 $result->addError(
new Error(
'Service is unavailable'));
129 return $this->getStatusViaHpg($messageFields);
134 $status = parent::resolveStatus($serviceStatus);
136 switch ($serviceStatus)
140 return MessageService\MessageStatus::ACCEPTED;
143 return MessageService\MessageStatus::SENT;
146 return MessageService\MessageStatus::DELIVERED;
149 return MessageService\MessageStatus::UNDELIVERED;
153 return MessageService\MessageStatus::FAILED;
160 private function sendViaHpg(array $messageFields)
167 'subject' => $messageFields[
'MESSAGE_FROM'],
168 'address' => str_replace(
'+',
'', $messageFields[
'MESSAGE_TO']),
172 $remoteCallResult = $this->touchHpg($this->
getOption(
'hpg_send_url'), $params);
173 $result->setServiceRequest($remoteCallResult->getHttpRequest());
174 $result->setServiceResponse($remoteCallResult->getHttpResponse());
175 if (!$remoteCallResult->isSuccess())
177 $result->addErrors($remoteCallResult->getErrors());
181 $answer = $remoteCallResult->getData();
182 [$code, $index, $msgId] = $answer;
186 $result->setExternalId($msgId);
194 private function getStatusViaHpg(array $messageFields)
196 $result =
new MessageStatus();
197 $result->setId($messageFields[
'ID']);
198 $result->setExternalId($messageFields[
'EXTERNAL_ID']);
202 'password' => $this->
getOption(
'password'),
203 'providerId' => [$messageFields[
'EXTERNAL_ID']],
206 $remoteCallResult= $this->touchHpg($this->
getOption(
'hpg_status_url'), $params);
208 if (!$remoteCallResult->isSuccess())
210 $result->addErrors($remoteCallResult->getErrors());
214 $answer = $remoteCallResult->getData();
215 [$code, $msgId, $status, $date, $reason] = $answer;
219 $result->setStatusText($status);
220 $result->setStatusCode(self::resolveStatus($status));
227 private function touchHpg($url, array $params): Sender\Result\HttpRequestResult
229 $result =
new Sender\Result\HttpRequestResult();
232 $params = \Bitrix\Main\Text\Encoding::convertEncoding($params, SITE_CHARSET,
'UTF-8');
235 $httpClient =
new HttpClient(array(
236 "socketTimeout" => $this->socketTimeout,
237 "streamTimeout" => $this->streamTimeout,
238 "waitResponse" =>
true,
240 $httpClient->setHeader(
'User-Agent',
'Bitrix24');
241 $httpClient->setCharset(
'UTF-8');
245 $url .=
'?'.http_build_query($params);
249 $answer = $httpClient->getResult();
252 if ($httpClient->getStatus() !=
'200')
254 $result->addError(
new Error($answer));
258 $status = explode(
';', $answer)[0];
259 if ($status !==
'ok')
261 $result->addError(
new Error($status));
265 $result->setHttpRequest(
new MessageService\DTO\Request([
268 'headers' => method_exists($httpClient,
'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
270 $result->setHttpResponse(
new MessageService\DTO\Response([
271 'statusCode' => $httpClient->getStatus(),
272 'headers' => $httpClient->getHeaders()->toArray(),
276 $result->setData(explode(
';', $answer));
static isModuleInstalled($moduleName)