35 public static function register(array $fields)
37 $code = isset($fields[
'CODE'])? $fields[
'CODE']:
'';
38 $type = in_array($fields[
'TYPE'], [self::TYPE_BOT, self::TYPE_SUPERVISOR, self::TYPE_NETWORK, self::TYPE_OPENLINE])
41 $moduleId = $fields[
'MODULE_ID'];
42 $installType = in_array($fields[
'INSTALL_TYPE'], [self::INSTALL_TYPE_SYSTEM, self::INSTALL_TYPE_USER, self::INSTALL_TYPE_SILENT])
43 ? $fields[
'INSTALL_TYPE']
44 : self::INSTALL_TYPE_SILENT;
45 $botFields = $fields[
'PROPERTIES'];
46 $language = isset($fields[
'LANG'])? $fields[
'LANG']:
null;
49 $class = isset($fields[
'CLASS'])? $fields[
'CLASS']:
'';
50 $methodBotDelete = isset($fields[
'METHOD_BOT_DELETE'])? $fields[
'METHOD_BOT_DELETE']:
'';
51 $methodMessageAdd = isset($fields[
'METHOD_MESSAGE_ADD'])? $fields[
'METHOD_MESSAGE_ADD']:
'';
52 $methodMessageUpdate = isset($fields[
'METHOD_MESSAGE_UPDATE'])? $fields[
'METHOD_MESSAGE_UPDATE']:
'';
53 $methodMessageDelete = isset($fields[
'METHOD_MESSAGE_DELETE'])? $fields[
'METHOD_MESSAGE_DELETE']:
'';
54 $methodWelcomeMessage = isset($fields[
'METHOD_WELCOME_MESSAGE'])? $fields[
'METHOD_WELCOME_MESSAGE']:
'';
55 $textPrivateWelcomeMessage = isset($fields[
'TEXT_PRIVATE_WELCOME_MESSAGE'])? $fields[
'TEXT_PRIVATE_WELCOME_MESSAGE']:
'';
56 $textChatWelcomeMessage = isset($fields[
'TEXT_CHAT_WELCOME_MESSAGE'])? $fields[
'TEXT_CHAT_WELCOME_MESSAGE']:
'';
57 $openline = isset($fields[
'OPENLINE']) && $fields[
'OPENLINE'] ==
'Y'?
'Y':
'N';
58 $isHidden = isset($fields[
'HIDDEN']) && $fields[
'HIDDEN'] ===
'Y' ?
'Y' :
'N';
61 if ($type == self::TYPE_OPENLINE)
64 $installType = self::INSTALL_TYPE_SILENT;
68 $appId = isset($fields[
'APP_ID'])? $fields[
'APP_ID']:
'';
69 $verified = isset($fields[
'VERIFIED']) && $fields[
'VERIFIED'] ==
'Y'?
'Y':
'N';
71 if ($moduleId ==
'rest')
80 if (empty($class) || empty($methodMessageAdd))
84 if (!(!empty($methodWelcomeMessage) || isset($fields[
'TEXT_PRIVATE_WELCOME_MESSAGE'])))
90 $bots = self::getListCache();
91 if ($moduleId && $code)
93 foreach ($bots as $bot)
95 if ($bot[
'MODULE_ID'] == $moduleId && $bot[
'CODE'] == $code)
97 return $bot[
'BOT_ID'];
102 $userCode = $code? $moduleId.
'_'.$code: $moduleId;
105 if (isset($botFields[
'COLOR']))
107 $color = $botFields[
'COLOR'];
108 unset($botFields[
'COLOR']);
112 if ($installType == self::INSTALL_TYPE_USER)
114 if (isset($fields[
'USER_ID']) && intval($fields[
'USER_ID']) > 0)
116 $userId = intval($fields[
'USER_ID']);
121 if (is_object($USER))
123 $userId = $USER->GetID() > 0? $USER->GetID(): 0;
128 $installType = self::INSTALL_TYPE_SYSTEM;
137 if (!(isset($botFields[
'NAME']) || isset($botFields[
'LAST_NAME'])))
142 $botFields[
'LOGIN'] = mb_substr(self::LOGIN_START. mb_substr($userCode, 0, 40).
'_'. randString(5), 0, 50);
143 $botFields[
'PASSWORD'] = md5($botFields[
'LOGIN'].
'|'.rand(1000,9999).
'|'.time());
144 $botFields[
'CONFIRM_PASSWORD'] = $botFields[
'PASSWORD'];
145 $botFields[
'EXTERNAL_AUTH_ID'] = self::EXTERNAL_AUTH_ID;
147 unset($botFields[
'GROUP_ID']);
149 $botFields[
'ACTIVE'] =
'Y';
151 unset($botFields[
'UF_DEPARTMENT']);
153 $botFields[
'WORK_POSITION'] = isset($botFields[
'WORK_POSITION'])? trim($botFields[
'WORK_POSITION']):
'';
154 if (empty($botFields[
'WORK_POSITION']))
156 $botFields[
'WORK_POSITION'] =
Loc::getMessage(
'BOT_DEFAULT_WORK_POSITION');
160 $botId = $user->Add($botFields);
166 $result = \Bitrix\Im\Model\BotTable::add(Array(
168 'CODE' => $code? $code: $botId,
169 'MODULE_ID' => $moduleId,
172 'LANG' => $language? $language:
'',
173 'METHOD_BOT_DELETE' => $methodBotDelete,
174 'METHOD_MESSAGE_ADD' => $methodMessageAdd,
175 'METHOD_MESSAGE_UPDATE' => $methodMessageUpdate,
176 'METHOD_MESSAGE_DELETE' => $methodMessageDelete,
177 'METHOD_WELCOME_MESSAGE' => $methodWelcomeMessage,
178 'TEXT_PRIVATE_WELCOME_MESSAGE' => $textPrivateWelcomeMessage,
179 'TEXT_CHAT_WELCOME_MESSAGE' => $textChatWelcomeMessage,
181 'VERIFIED' => $verified,
182 'OPENLINE' => $openline,
183 'HIDDEN' => $isHidden,
186 $cache = \Bitrix\Main\Data\Cache::createInstance();
187 $cache->cleanDir(self::CACHE_PATH);
189 if ($result->isSuccess())
191 if (\
Bitrix\Main\Loader::includeModule(
'pull'))
195 \CIMStatus::SetColor($botId, $color);
198 self::sendPullNotify($botId,
'botAdd');
200 if ($installType != self::INSTALL_TYPE_SILENT)
205 $userName =
'[USER='.$userId.
'][/USER]';
206 $userGender = \Bitrix\Im\User::getInstance($userId)->getGender();
207 $message =
Loc::getMessage(
'BOT_MESSAGE_INSTALL_USER'.($userGender ==
'F'?
'_F':
''), Array(
'#USER_NAME#' => $userName));
214 $attach = new \CIMMessageParamAttach(
null, $color);
215 $attach->AddBot(Array(
222 \CIMChat::AddGeneralMessage(Array(
223 'MESSAGE' => $message,
225 'SKIP_USER_CHECK' =>
'Y',
230 \Bitrix\Main\Application::getInstance()->getTaggedCache()->clearByTag(
"IM_CONTACT_LIST");
234 $user->Delete($botId);
247 $botId = intval($bot[
'BOT_ID']);
248 $moduleId = isset($bot[
'MODULE_ID'])? $bot[
'MODULE_ID']:
'';
249 $appId = isset($bot[
'APP_ID'])? $bot[
'APP_ID']:
'';
251 if (intval($botId) <= 0)
256 $bots = self::getListCache();
257 if (!isset($bots[$botId]))
262 if ($moduleId <>
'' && $bots[$botId][
'MODULE_ID'] != $moduleId)
267 if ($appId <>
'' && $bots[$botId][
'APP_ID'] != $appId)
272 \Bitrix\Im\Model\BotTable::delete($botId);
274 $orm = \Bitrix\Im\Model\BotChatTable::getList(Array(
275 'filter' => Array(
'=BOT_ID' => $botId)
277 if ($row = $orm->fetch())
279 \Bitrix\Im\Model\BotChatTable::delete($row[
'ID']);
282 $cache = \Bitrix\Main\Data\Cache::createInstance();
283 $cache->cleanDir(self::CACHE_PATH);
286 $user->Delete($botId);
288 if (\
Bitrix\Main\Loader::includeModule($bots[$botId][
'MODULE_ID']) && $bots[$botId][
"METHOD_BOT_DELETE"] && class_exists($bots[$botId][
"CLASS"]) && method_exists($bots[$botId][
"CLASS"], $bots[$botId][
"METHOD_BOT_DELETE"]))
290 call_user_func_array(array($bots[$botId][
"CLASS"], $bots[$botId][
"METHOD_BOT_DELETE"]), Array($botId));
295 \ExecuteModuleEventEx($event, Array($bots[$botId], $botId));
298 $orm = \Bitrix\Im\Model\CommandTable::getList(Array(
299 'filter' => Array(
'=BOT_ID' => $botId)
301 while ($row = $orm->fetch())
303 \Bitrix\Im\Command::unRegister(Array(
'COMMAND_ID' => $row[
'ID'],
'FORCE' =>
'Y'));
306 $orm = \Bitrix\Im\Model\AppTable::getList(Array(
307 'filter' => Array(
'=BOT_ID' => $botId)
309 while ($row = $orm->fetch())
311 \Bitrix\Im\App::unRegister(Array(
'ID' => $row[
'ID'],
'FORCE' =>
'Y'));
314 self::sendPullNotify($botId,
'botDelete');
316 \Bitrix\Main\Application::getInstance()->getTaggedCache()->clearByTag(
"IM_CONTACT_LIST");
326 public static function update(array $bot, array $updateFields)
328 $botId = intval($bot[
'BOT_ID']);
329 $moduleId = isset($bot[
'MODULE_ID'])? $bot[
'MODULE_ID']:
'';
330 $appId = isset($bot[
'APP_ID'])? $bot[
'APP_ID']:
'';
342 $bots = self::getListCache();
343 if (!isset($bots[$botId]))
348 if ($moduleId <>
'' && $bots[$botId][
'MODULE_ID'] != $moduleId)
353 if ($appId <>
'' && $bots[$botId][
'APP_ID'] != $appId)
358 if (isset($updateFields[
'PROPERTIES']))
360 $update = $updateFields[
'PROPERTIES'];
362 $update[
'EXTERNAL_AUTH_ID'] = self::EXTERNAL_AUTH_ID;
364 if (isset($update[
'NAME']) && trim($update[
'NAME']) ==
'')
366 unset($update[
'NAME']);
368 if (isset($update[
'WORK_POSITION']) && trim($update[
'WORK_POSITION']) ==
'')
370 $update[
'WORK_POSITION'] =
Loc::getMessage(
'BOT_DEFAULT_WORK_POSITION');
374 $delBotAvatar =
false;
375 $previousBotAvatar =
false;
377 !empty($update[
'PERSONAL_PHOTO'])
378 && is_numeric($update[
'PERSONAL_PHOTO'])
379 && (
int)$update[
'PERSONAL_PHOTO'] > 0
383 $botAvatar = (int)$update[
'PERSONAL_PHOTO'];
386 !empty($update[
'DELETE_PERSONAL_PHOTO'])
387 && $update[
'DELETE_PERSONAL_PHOTO'] ==
'Y'
391 $delBotAvatar =
true;
399 $update[
'CONFIRM_PASSWORD'],
401 $update[
'UF_DEPARTMENT'],
402 $update[
'PERSONAL_PHOTO'],
403 $update[
'DELETE_PERSONAL_PHOTO']
407 $user->Update($botId, $update);
409 if ($botAvatar > 0 && $botAvatar !== $previousBotAvatar)
411 $connection = Main\Application::getConnection();
412 $connection->query(
"UPDATE b_user SET PERSONAL_PHOTO = ".(
int)$botAvatar.
" WHERE ID = ".(
int)$botId);
414 elseif ($delBotAvatar)
416 $connection = Main\Application::getConnection();
417 $connection->query(
"UPDATE b_user SET PERSONAL_PHOTO = null WHERE ID = ".(
int)$botId);
420 if ($previousBotAvatar > 0)
422 \CFile::Delete($previousBotAvatar);
427 if (isset($updateFields[
'CLASS']) && !empty($updateFields[
'CLASS']))
429 $update[
'CLASS'] = $updateFields[
'CLASS'];
431 if (isset($updateFields[
'TYPE']) && !empty($updateFields[
'TYPE']))
433 $update[
'TYPE'] = $updateFields[
'TYPE'];
435 if (isset($updateFields[
'CODE']) && !empty($updateFields[
'CODE']))
437 $update[
'CODE'] = $updateFields[
'CODE'];
439 if (isset($updateFields[
'APP_ID']) && !empty($updateFields[
'APP_ID']))
441 $update[
'APP_ID'] = $updateFields[
'APP_ID'];
443 if (isset($updateFields[
'LANG']))
445 $update[
'LANG'] = $updateFields[
'LANG']? $updateFields[
'LANG']:
'';
447 if (isset($updateFields[
'METHOD_BOT_DELETE']))
449 $update[
'METHOD_BOT_DELETE'] = $updateFields[
'METHOD_BOT_DELETE'];
451 if (isset($updateFields[
'METHOD_MESSAGE_ADD']))
453 $update[
'METHOD_MESSAGE_ADD'] = $updateFields[
'METHOD_MESSAGE_ADD'];
455 if (isset($updateFields[
'METHOD_MESSAGE_UPDATE']))
457 $update[
'METHOD_MESSAGE_UPDATE'] = $updateFields[
'METHOD_MESSAGE_UPDATE'];
459 if (isset($updateFields[
'METHOD_MESSAGE_DELETE']))
461 $update[
'METHOD_MESSAGE_DELETE'] = $updateFields[
'METHOD_MESSAGE_DELETE'];
463 if (isset($updateFields[
'METHOD_WELCOME_MESSAGE']))
465 $update[
'METHOD_WELCOME_MESSAGE'] = $updateFields[
'METHOD_WELCOME_MESSAGE'];
467 if (isset($updateFields[
'TEXT_PRIVATE_WELCOME_MESSAGE']))
469 $update[
'TEXT_PRIVATE_WELCOME_MESSAGE'] = $updateFields[
'TEXT_PRIVATE_WELCOME_MESSAGE'];
471 if (isset($updateFields[
'TEXT_CHAT_WELCOME_MESSAGE']))
473 $update[
'TEXT_CHAT_WELCOME_MESSAGE'] = $updateFields[
'TEXT_CHAT_WELCOME_MESSAGE'];
475 if (isset($updateFields[
'VERIFIED']))
477 $update[
'VERIFIED'] = $updateFields[
'VERIFIED'] ==
'Y'?
'Y':
'N';
479 if (isset($updateFields[
'HIDDEN']))
481 $update[
'HIDDEN'] = $updateFields[
'HIDDEN'] ===
'Y' ?
'Y' :
'N';
485 \Bitrix\Im\Model\BotTable::update($botId, $update);
487 $cache = \Bitrix\Main\Data\Cache::createInstance();
488 $cache->cleanDir(self::CACHE_PATH);
491 self::sendPullNotify($botId,
'botUpdate');
493 \Bitrix\Main\Application::getInstance()->getTaggedCache()->clearByTag(
"IM_CONTACT_LIST");
592 $botExecModule = self::getBotsForMessage($messageFields);
598 if ($messageFields[
'MESSAGE_TYPE'] != IM_MESSAGE_PRIVATE)
600 $messageFields[
'MESSAGE_ORIGINAL'] = $messageFields[
'MESSAGE'];
601 if (preg_match(
"/\[USER=([0-9]+)( REPLACE)?](.*?)\[\/USER]/i", $messageFields[
'MESSAGE'], $matches))
603 $messageFields[
'TO_USER_ID'] = $matches[1];
607 $messageFields[
'TO_USER_ID'] = 0;
609 $messageFields[
'MESSAGE'] = trim(preg_replace(
'#\[(?P<tag>USER)=\d+\].+?\[/(?P=tag)\],?#',
'', $messageFields[
'MESSAGE']));
612 $messageFields[
'DIALOG_ID'] = self::getDialogId($messageFields);
613 $messageFields = self::removeFieldsToEvent($messageFields);
615 foreach ($botExecModule as $params)
617 if (!$params[
'MODULE_ID'] || !\
Bitrix\Main\Loader::includeModule($params[
'MODULE_ID']))
622 $messageFields[
'BOT_ID'] = $params[
'BOT_ID'];
624 if ($params[
"METHOD_MESSAGE_ADD"] && class_exists($params[
"CLASS"]) && method_exists($params[
"CLASS"], $params[
"METHOD_MESSAGE_ADD"]))
626 \Bitrix\Im\Model\BotTable::update($params[
'BOT_ID'], array(
630 call_user_func_array(array($params[
"CLASS"], $params[
"METHOD_MESSAGE_ADD"]), Array($messageId, $messageFields));
632 else if (class_exists($params[
"CLASS"]) && method_exists($params[
"CLASS"],
"onMessageAdd"))
634 call_user_func_array(array($params[
"CLASS"],
"onMessageAdd"), Array($messageId, $messageFields));
637 unset($messageFields[
'BOT_ID']);
641 \ExecuteModuleEventEx($event, Array($botExecModule, $messageId, $messageFields));
645 $messageFields[
'CHAT_ENTITY_TYPE'] ==
'LINES'
646 && trim($messageFields[
'MESSAGE']) ===
'0'
647 && \
Bitrix\Main\Loader::includeModule(
'imopenlines')
650 $chat = new \Bitrix\Imopenlines\Chat($messageFields[
'TO_CHAT_ID']);
651 $chat->endBotSession();
659 $botExecModule = self::getBotsForMessage($messageFields);
665 if ($messageFields[
'MESSAGE_TYPE'] != IM_MESSAGE_PRIVATE)
667 $messageFields[
'MESSAGE_ORIGINAL'] = $messageFields[
'MESSAGE'];
668 if (preg_match(
"/\[USER=([0-9]+)( REPLACE)?](.*?)\[\/USER]/i", $messageFields[
'MESSAGE'], $matches))
670 $messageFields[
'TO_USER_ID'] = $matches[1];
674 $messageFields[
'TO_USER_ID'] = 0;
676 $messageFields[
'MESSAGE'] = trim(preg_replace(
'#\[(?P<tag>USER)=\d+\].+?\[/(?P=tag)\],?#',
'', $messageFields[
'MESSAGE']));
679 $messageFields[
'DIALOG_ID'] = self::getDialogId($messageFields);
680 $messageFields = self::removeFieldsToEvent($messageFields);
682 foreach ($botExecModule as $params)
684 if (!$params[
'MODULE_ID'] || !\
Bitrix\Main\Loader::includeModule($params[
'MODULE_ID']))
689 $messageFields[
'BOT_ID'] = $params[
'BOT_ID'];
691 if ($params[
"METHOD_MESSAGE_UPDATE"] && class_exists($params[
"CLASS"]) && method_exists($params[
"CLASS"], $params[
"METHOD_MESSAGE_UPDATE"]))
693 call_user_func_array(array($params[
"CLASS"], $params[
"METHOD_MESSAGE_UPDATE"]), Array($messageId, $messageFields));
695 else if (class_exists($params[
"CLASS"]) && method_exists($params[
"CLASS"],
"onMessageUpdate"))
697 call_user_func_array(array($params[
"CLASS"],
"onMessageUpdate"), Array($messageId, $messageFields));
700 unset($messageFields[
'BOT_ID']);
704 \ExecuteModuleEventEx($event, Array($botExecModule, $messageId, $messageFields));
712 $botExecModule = self::getBotsForMessage($messageFields);
718 $messageFields[
'DIALOG_ID'] = self::getDialogId($messageFields);
719 $messageFields = self::removeFieldsToEvent($messageFields);
721 foreach ($botExecModule as $params)
723 if (!$params[
'MODULE_ID'] || !\
Bitrix\Main\Loader::includeModule($params[
'MODULE_ID']))
728 $messageFields[
'BOT_ID'] = $params[
'BOT_ID'];
730 if ($params[
"METHOD_MESSAGE_DELETE"] && class_exists($params[
"CLASS"]) && method_exists($params[
"CLASS"], $params[
"METHOD_MESSAGE_DELETE"]))
732 call_user_func_array(array($params[
"CLASS"], $params[
"METHOD_MESSAGE_DELETE"]), Array($messageId, $messageFields));
734 else if (class_exists($params[
"CLASS"]) && method_exists($params[
"CLASS"],
"onMessageDelete"))
736 call_user_func_array(array($params[
"CLASS"],
"onMessageDelete"), Array($messageId, $messageFields));
739 unset($messageFields[
'BOT_ID']);
743 \ExecuteModuleEventEx($event, Array($botExecModule, $messageId, $messageFields));
751 $bots = self::getListCache();
757 if (!isset($joinFields[
'BOT_ID']) || !$bots[$joinFields[
'BOT_ID']])
762 $bot = $bots[$joinFields[
'BOT_ID']];
764 if (!\
Bitrix\Main\Loader::includeModule($bot[
'MODULE_ID']))
769 if ($joinFields[
'CHAT_TYPE'] == IM_MESSAGE_PRIVATE)
771 $updateCounter = array(
"COUNT_USER" =>
new \
Bitrix\Main\DB\
SqlExpression(
"?# + 1",
"COUNT_USER"));
775 $updateCounter = array(
"COUNT_CHAT" =>
new \
Bitrix\Main\DB\
SqlExpression(
"?# + 1",
"COUNT_CHAT"));
777 \Bitrix\Im\Model\BotTable::update($joinFields[
'BOT_ID'], $updateCounter);
780 $joinFields[
'CHAT_TYPE'] != IM_MESSAGE_PRIVATE
781 && $bot[
'TYPE'] == self::TYPE_SUPERVISOR
782 && (empty($joinFields[
'SILENT_JOIN']) || $joinFields[
'SILENT_JOIN'] !=
'Y')
785 \CIMMessenger::Add(Array(
786 'DIALOG_ID' => $dialogId,
787 'MESSAGE_TYPE' => $joinFields[
'CHAT_TYPE'],
788 'MESSAGE' => str_replace(Array(
'#BOT_NAME#'), Array(
'[USER='.$joinFields[
'BOT_ID'].
'][/USER]'), $joinFields[
'ACCESS_HISTORY']?
Loc::getMessage(
'BOT_SUPERVISOR_NOTICE_ALL_MESSAGES'):
Loc::getMessage(
'BOT_SUPERVISOR_NOTICE_NEW_MESSAGES')),
790 'SKIP_COMMAND' =>
'Y',
792 "CLASS" =>
"bx-messenger-content-item-system"
797 if ($bot[
"METHOD_WELCOME_MESSAGE"] && class_exists($bot[
"CLASS"]) && method_exists($bot[
"CLASS"], $bot[
"METHOD_WELCOME_MESSAGE"]))
799 call_user_func_array(array($bot[
"CLASS"], $bot[
"METHOD_WELCOME_MESSAGE"]), Array($dialogId, $joinFields));
802 $bot[
"TEXT_PRIVATE_WELCOME_MESSAGE"] <>
''
803 && $joinFields[
'CHAT_TYPE'] == IM_MESSAGE_PRIVATE
804 && $joinFields[
'FROM_USER_ID'] != $joinFields[
'BOT_ID']
807 if ($bot[
'TYPE'] == self::TYPE_HUMAN)
809 self::startWriting(Array(
'BOT_ID' => $joinFields[
'BOT_ID']), $dialogId);
812 $userName = \Bitrix\Im\User::getInstance($joinFields[
'USER_ID'])->getName();
813 self::addMessage(Array(
'BOT_ID' => $joinFields[
'BOT_ID']), Array(
814 'DIALOG_ID' => $dialogId,
815 'MESSAGE' => str_replace(Array(
'#USER_NAME#'), Array($userName), $bot[
"TEXT_PRIVATE_WELCOME_MESSAGE"]),
819 $bot[
"TEXT_CHAT_WELCOME_MESSAGE"] <>
''
821 $joinFields[
'CHAT_TYPE'] == IM_MESSAGE_CHAT
822 || $joinFields[
'CHAT_TYPE'] == IM_MESSAGE_OPEN_LINE
823 || $joinFields[
'CHAT_TYPE'] == \
Bitrix\Im\V2\Chat::IM_TYPE_COPILOT
825 && $joinFields[
'FROM_USER_ID'] != $joinFields[
'BOT_ID']
828 if ($bot[
'TYPE'] == self::TYPE_HUMAN)
830 self::startWriting(Array(
'BOT_ID' => $joinFields[
'BOT_ID']), $dialogId);
832 $userName = \Bitrix\Im\User::getInstance($joinFields[
'USER_ID'])->getName();
833 self::addMessage(Array(
'BOT_ID' => $joinFields[
'BOT_ID']), Array(
834 'DIALOG_ID' => $dialogId,
835 'MESSAGE' => str_replace(Array(
'#USER_NAME#'), Array($userName), $bot[
"TEXT_CHAT_WELCOME_MESSAGE"]),
841 \ExecuteModuleEventEx($event, Array($bot, $dialogId, $joinFields));
849 $bots = self::getListCache();
855 if (!isset($leaveFields[
'BOT_ID']) || !$bots[$leaveFields[
'BOT_ID']])
860 $bot = $bots[$leaveFields[
'BOT_ID']];
862 if (!\
Bitrix\Main\Loader::includeModule($bot[
'MODULE_ID']))
867 if ($leaveFields[
'CHAT_TYPE'] == IM_MESSAGE_PRIVATE)
869 $updateCounter = array(
"COUNT_USER" =>
new \
Bitrix\Main\DB\
SqlExpression(
"?# - 1",
"COUNT_USER"));
873 $updateCounter = array(
"COUNT_CHAT" =>
new \
Bitrix\Main\DB\
SqlExpression(
"?# - 1",
"COUNT_CHAT"));
875 \Bitrix\Im\Model\BotTable::update($leaveFields[
'BOT_ID'], $updateCounter);
879 \ExecuteModuleEventEx($event, Array($bot, $dialogId, $leaveFields));
952 public static function addMessage(array $bot, array $messageFields)
954 $botId = $bot[
'BOT_ID'];
955 $moduleId = isset($bot[
'MODULE_ID'])? $bot[
'MODULE_ID']:
'';
956 $appId = isset($bot[
'APP_ID'])? $bot[
'APP_ID']:
'';
958 if (intval($botId) <= 0)
968 $bots = self::getListCache();
969 if (!isset($bots[$botId]))
974 if ($moduleId <>
'' && $bots[$botId][
'MODULE_ID'] != $moduleId)
979 if ($appId <>
'' && $bots[$botId][
'APP_ID'] != $appId)
984 $isPrivateSystem =
false;
985 if (($messageFields[
'FROM_USER_ID'] ??
null) && ($messageFields[
'TO_USER_ID'] ??
null))
987 $messageFields[
'SYSTEM'] =
'Y';
988 $messageFields[
'DIALOG_ID'] = $messageFields[
'TO_USER_ID'];
989 $isPrivateSystem =
true;
991 else if (empty($messageFields[
'DIALOG_ID']))
996 $messageFields[
'MENU'] ??=
null;
997 $messageFields[
'ATTACH'] ??=
null;
998 $messageFields[
'KEYBOARD'] ??=
null;
999 $messageFields[
'PARAMS'] ??= [];
1003 $chatId = \Bitrix\Im\Dialog::getChatId($messageFields[
'DIALOG_ID']);
1009 if (\CIMChat::GetGeneralChatId() == $chatId && !\CIMChat::CanSendMessageToGeneralChat($botId))
1016 "FROM_USER_ID" => $botId,
1017 "TO_CHAT_ID" => $chatId,
1018 "ATTACH" => $messageFields[
'ATTACH'],
1019 "KEYBOARD" => $messageFields[
'KEYBOARD'],
1020 "MENU" => $messageFields[
'MENU'],
1021 "PARAMS" => $messageFields[
'PARAMS'],
1023 if (isset($messageFields[
'MESSAGE']) && (!empty($messageFields[
'MESSAGE']) || $messageFields[
'MESSAGE'] ===
"0"))
1025 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
1027 if (isset($messageFields[
'SYSTEM']) && $messageFields[
'SYSTEM'] ==
'Y')
1029 $ar[
'SYSTEM'] =
'Y';
1030 $ar[
'MESSAGE'] = \Bitrix\Im\User::getInstance($botId)->getFullName().
":[br]".$ar[
'MESSAGE'];
1032 if (isset($messageFields[
'URL_PREVIEW']) && $messageFields[
'URL_PREVIEW'] ==
'N')
1034 $ar[
'URL_PREVIEW'] =
'N';
1036 if (isset($messageFields[
'SKIP_CONNECTOR']) && $messageFields[
'SKIP_CONNECTOR'] ==
'Y')
1038 $ar[
'SKIP_CONNECTOR'] =
'Y';
1039 $ar[
'SILENT_CONNECTOR'] =
'Y';
1041 $ar[
'SKIP_COMMAND'] =
'Y';
1042 $id = \CIMChat::AddMessage($ar);
1047 if ($isPrivateSystem)
1049 $fromUserId = intval($messageFields[
'FROM_USER_ID']);
1052 $messageFields[
'MESSAGE'] =
Loc::getMessage(
"BOT_MESSAGE_FROM", Array(
"#BOT_NAME#" =>
"[USER=".$botId.
"][/USER][BR]")).$messageFields[
'MESSAGE'];
1057 $fromUserId = $botId;
1060 $userId = intval($messageFields[
'DIALOG_ID']);
1062 "FROM_USER_ID" => $fromUserId,
1063 "TO_USER_ID" => $userId,
1064 "ATTACH" => $messageFields[
'ATTACH'],
1065 "KEYBOARD" => $messageFields[
'KEYBOARD'],
1066 "MENU" => $messageFields[
'MENU'],
1067 "PARAMS" => $messageFields[
'PARAMS'],
1069 if (isset($messageFields[
'MESSAGE']) && (!empty($messageFields[
'MESSAGE']) || $messageFields[
'MESSAGE'] ===
"0"))
1071 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
1073 if (isset($messageFields[
'SYSTEM']) && $messageFields[
'SYSTEM'] ==
'Y')
1075 $ar[
'SYSTEM'] =
'Y';
1077 if (isset($messageFields[
'URL_PREVIEW']) && $messageFields[
'URL_PREVIEW'] ==
'N')
1079 $ar[
'URL_PREVIEW'] =
'N';
1081 if (isset($messageFields[
'SKIP_CONNECTOR']) && $messageFields[
'SKIP_CONNECTOR'] ==
'Y')
1083 $ar[
'SKIP_CONNECTOR'] =
'Y';
1084 $ar[
'SILENT_CONNECTOR'] =
'Y';
1086 $ar[
'SKIP_COMMAND'] =
'Y';
1087 $id = \CIMMessage::Add($ar);
1120 $botId = $bot[
'BOT_ID'];
1121 $moduleId = isset($bot[
'MODULE_ID'])? $bot[
'MODULE_ID']:
'';
1122 $appId = isset($bot[
'APP_ID'])? $bot[
'APP_ID']:
'';
1124 if (intval($botId) <= 0)
1130 $bots = self::getListCache();
1131 if (!isset($bots[$botId]))
1134 if ($moduleId <>
'' && $bots[$botId][
'MODULE_ID'] != $moduleId)
1137 if ($appId <>
'' && $bots[$botId][
'APP_ID'] != $appId)
1140 $messageId = intval($messageFields[
'MESSAGE_ID']);
1141 if ($messageId <= 0)
1144 $message = \CIMMessenger::CheckPossibilityUpdateMessage(IM_CHECK_UPDATE, $messageId, $botId);
1148 if (isset($messageFields[
'ATTACH']))
1150 if (empty($messageFields[
'ATTACH']) || $messageFields[
'ATTACH'] ==
'N')
1152 \CIMMessageParam::Set($messageId, Array(
'ATTACH' => Array()));
1154 else if ($messageFields[
'ATTACH'] instanceof \CIMMessageParamAttach)
1156 if ($messageFields[
'ATTACH']->IsAllowSize())
1158 \CIMMessageParam::Set($messageId, Array(
'ATTACH' => $messageFields[
'ATTACH']));
1163 if (isset($messageFields[
'KEYBOARD']))
1165 if (empty($messageFields[
'KEYBOARD']) || $messageFields[
'KEYBOARD'] ==
'N')
1167 \CIMMessageParam::Set($messageId, Array(
'KEYBOARD' =>
'N'));
1169 else if ($messageFields[
'KEYBOARD'] instanceof \
Bitrix\Im\
Bot\Keyboard)
1171 if ($messageFields[
'KEYBOARD']->isAllowSize())
1173 \CIMMessageParam::Set($messageId, Array(
'KEYBOARD' => $messageFields[
'KEYBOARD']));
1178 if (isset($messageFields[
'MENU']))
1180 if (empty($messageFields[
'MENU']) || $messageFields[
'MENU'] ==
'N')
1182 \CIMMessageParam::Set($messageId, Array(
'MENU' =>
'N'));
1184 else if ($messageFields[
'MENU'] instanceof \
Bitrix\Im\
Bot\ContextMenu)
1186 if ($messageFields[
'MENU']->isAllowSize())
1188 \CIMMessageParam::Set($messageId, Array(
'MENU' => $messageFields[
'MENU']));
1193 if (isset($messageFields[
'MESSAGE']))
1195 $urlPreview = isset($messageFields[
'URL_PREVIEW']) && $messageFields[
'URL_PREVIEW'] ==
"N"?
false:
true;
1196 $skipConnector = isset($messageFields[
'SKIP_CONNECTOR']) && $messageFields[
'SKIP_CONNECTOR'] ==
"Y"?
true:
false;
1197 $editFlag = isset($messageFields[
'EDIT_FLAG']) && $messageFields[
'EDIT_FLAG'] ==
"Y"?
true:
false;
1199 $res = \CIMMessenger::Update($messageId, $messageFields[
'MESSAGE'], $urlPreview, $editFlag, $botId, $skipConnector);
1205 \CIMMessageParam::SendPull($messageId, Array(
'KEYBOARD',
'ATTACH',
'MENU'));
1327 $cache = \Bitrix\Main\Data\Cache::createInstance();
1328 if($cache->initCache(self::CACHE_TTL,
'list_r5', self::CACHE_PATH))
1330 $result = $cache->getVars();
1335 $orm = \Bitrix\Im\Model\BotTable::getList();
1336 while ($row = $orm->fetch())
1338 $row[
'LANG'] = $row[
'LANG']? $row[
'LANG']:
null;
1339 $result[$row[
'BOT_ID']] = $row;
1342 $cache->startDataCache();
1343 $cache->endDataCache($result);
1346 if ($type == self::LIST_OPENLINE)
1348 foreach ($result as $botId => $botData)
1350 if ($botData[
'OPENLINE'] !=
'Y' || $botData[
'CODE'] ==
'marta')
1352 unset($result[$botId]);
1473 $chatId = intval($chatId);
1474 $botId = intval($botId);
1476 if ($chatId <= 0 || $botId <= 0)
1479 $chats = self::getChatListCache($botId);
1483 if (isset($chats[$chatId]))
1487 \Bitrix\Im\Model\BotChatTable::add(Array(
1489 'CHAT_ID' => $chatId
1494 if (!isset($chats[$chatId]))
1499 $orm = \Bitrix\Im\Model\BotChatTable::getList(Array(
1500 'filter' => Array(
'=BOT_ID' => $botId,
'=CHAT_ID' => $chatId)
1502 if ($row = $orm->fetch())
1504 \Bitrix\Im\Model\BotChatTable::delete($row[
'ID']);
1508 $cache = \Bitrix\Main\Data\Cache::createInstance();
1509 $cache->clean(
'chat'.$botId, self::CACHE_PATH);
1516 $cache = \Bitrix\Main\Data\Cache::createInstance();
1517 if($cache->initCache(self::CACHE_TTL,
'language_v2',
'/bx/im/'))
1519 $languageId = $cache->getVars();
1525 $siteIterator = \Bitrix\Main\SiteTable::getList(array(
1526 'select' => array(
'LANGUAGE_ID'),
1527 'filter' => array(
'=DEF' =>
'Y',
'=ACTIVE' =>
'Y')
1529 if ($site = $siteIterator->fetch())
1530 $languageId = (string)$site[
'LANGUAGE_ID'];
1532 if ($languageId ==
'')
1534 if (\
Bitrix\Main\Loader::includeModule(
'bitrix24'))
1536 $languageId = \CBitrix24::getLicensePrefix();
1540 $languageId = LANGUAGE_ID;
1543 if ($languageId ==
'')
1548 $languageId = mb_strtolower($languageId);
1550 $cache->startDataCache();
1551 $cache->endDataCache($languageId);