5use Bitrix\ImConnector\Library;
6use Bitrix\ImOpenLines\Im;
7use Bitrix\ImOpenLines\Session;
25 Providers\Edna\
EdnaRu $utils,
29 parent::__construct($optionManager, $supportChecker, $utils);
30 $this->emoji = $emoji;
37 return Providers\Edna\Constants\Method::SEND_MESSAGE;
49 return $this->emoji->convertEmoji($text, InternalOption::EMOJI_ENCODE);
54 $cascadeResult =
new Result();
55 if (is_numeric($messageFields[
'MESSAGE_FROM']))
57 $cascadeResult = $this->utils->getCascadeIdFromSubject(
58 (
int)$messageFields[
'MESSAGE_FROM'],
59 static function(array $externalSubjectData,
int $internalSubject)
61 return $externalSubjectData[
'id'] === $internalSubject;
65 elseif (is_string($messageFields[
'MESSAGE_FROM']))
67 $cascadeResult = $this->utils->getCascadeIdFromSubject(
68 $messageFields[
'MESSAGE_FROM'],
69 static function(array $externalSubjectData,
string $internalSubject)
71 return $externalSubjectData[
'subject'] === $internalSubject;
77 return $cascadeResult->addError(
new Error(
'Invalid subject id'));
80 if (!$cascadeResult->isSuccess())
82 return $cascadeResult;
85 $messageFields[
'MESSAGE_BODY'] = $this->emoji->convertEmoji($messageFields[
'MESSAGE_BODY'], InternalOption::EMOJI_DECODE);
88 'requestId' => uniqid(
'',
true),
89 'cascadeId' => $cascadeResult->getData()[
'cascadeId'],
90 'subscriberFilter' => [
91 'address' => str_replace(
'+',
'', $messageFields[
'MESSAGE_TO']),
98 $result->setData($params);
114 if (isset($messageFields[
'MESSAGE_HEADERS'][
'template'][
'text']))
130 ? $this->getHSMContent($messageFields)
131 : $this->getSimpleMessageContent($messageFields)
135 'whatsappContent' => $whatsAppContent
143 private function getHSMContent(array $messageFields): array
146 'contentType' => Providers\Edna\Constants\ContentType::TEXT,
147 'text' => $messageFields[
'MESSAGE_HEADERS'][
'template'][
'text']
150 foreach ([
'header',
'footer',
'keyboard'] as $templateField)
153 isset($messageFields[
'MESSAGE_HEADERS'][
'template'][$templateField])
154 && count($messageFields[
'MESSAGE_HEADERS'][
'template'][$templateField]) > 0
157 $params[$templateField] = $messageFields[
'MESSAGE_HEADERS'][
'template'][$templateField];
161 return $this->emoji->convertEmojiInTemplate($params, InternalOption::EMOJI_DECODE);
168 private function getSimpleMessageContent(array $messageFields): array
171 'contentType' =>
'TEXT',
172 'text' => $messageFields[
'MESSAGE_BODY']
178 if (!Loader::includeModule(
'imopenlines') || !Loader::includeModule(
'imconnector'))
183 $externalChatId = str_replace(
'+',
'', $messageFields[
'MESSAGE_TO']);
187 return (
new Result())->addError(
new Error(
'Missing User Id'));
190 $from = $messageFields[
'MESSAGE_FROM'];
191 $lineId = $this->connectorLine->getLineId();
192 $userSessionCode = $this->
getSessionUserCode($lineId, $externalChatId, $from, $userId);
196 return (
new Result())->addError(
new Error(
'Missing Chat Id'));
199 $messageId = Im::addMessage([
200 'TO_CHAT_ID' => $chatId,
201 'MESSAGE' => $this->utils->prepareTemplateMessageText($messageFields),
203 'SKIP_COMMAND' =>
'Y',
204 'NO_SESSION_OL' =>
'Y',
206 'CLASS' =>
'bx-messenger-content-item-ol-output'
211 $resultData = $messageFields;
212 $resultData[
'messageId'] = $messageId;
213 $resultData[
'chatId'] = $chatId;
214 $result->setData($resultData);
218 $result->addError(
new Error(
'Error sending a message to the chat'));
225 $userXmlId = Library::ID_EDNA_WHATSAPP_CONNECTOR .
'|' . $externalChatId;
226 $user = \Bitrix\Main\UserTable::getRow([
228 'filter' => [
'=XML_ID' => $userXmlId],
231 return $user ? $user[
'ID'] :
null;
234 protected function getSessionUserCode(
string $lineId,
string $externalChatId,
string $from,
string $userId): string
236 return Library::ID_EDNA_WHATSAPP_CONNECTOR.
'|'. $lineId.
'|'. $externalChatId.
'@'. $from.
'|' . $userId;
241 $session =
new Session();
242 $sessionLoadResult = $session->getLast([
'USER_CODE' => $userSessionCode]);
243 if (!$sessionLoadResult->isSuccess())
247 $sessionData = $session->getData();
248 $chatId = $sessionData[
'CHAT_ID'];
249 $closed = $sessionData[
'CLOSED'] ===
'Y';
261 $this->optionManager->getOption(InternalOption::API_KEY),
262 RegionHelper::getApiEndPoint(),
263 $this->optionManager->getSocketTimeout(),
264 $this->optionManager->getStreamTimeout()
getSendMessageParams(array $messageFields)
Providers OptionManager $optionManager
getSessionUserCode(string $lineId, string $externalChatId, string $from, string $userId)
__construct(Providers\OptionManager $optionManager, Providers\SupportChecker $supportChecker, Providers\Edna\EdnaRu $utils, EmojiConverter $emoji)
sendHSMtoChat(array $messageFields)
getOpenedSessionChatId(string $userSessionCode)
prepareMessageBodyForSave(string $text)
ConnectorLine $connectorLine
isTemplateMessage(array $messageFields)
getSendMessageMethod(array $messageFields)
initializeDefaultExternalSender()
getImconnectorUserId(string $externalChatId)
Providers Edna EdnaRu $utils
getMessageContent(array $messageFields)
Providers SupportChecker $supportChecker