21 public static function register(array $fields)
23 $moduleId = $fields[
'MODULE_ID'];
24 $command = mb_substr($fields[
'COMMAND'], 0, 1) ==
'/'? mb_substr($fields[
'COMMAND'], 1) : $fields[
'COMMAND'];
26 $botId = isset($fields[
'BOT_ID'])? intval($fields[
'BOT_ID']): 0;
32 $common = isset($fields[
'COMMON']) && $fields[
'COMMON'] ==
'Y'?
'Y':
'N';
38 $hidden = isset($fields[
'HIDDEN']) && $fields[
'HIDDEN'] ==
'Y'?
'Y':
'N';
39 $sonetSupport = isset($fields[
'SONET_SUPPORT']) && $fields[
'SONET_SUPPORT'] ==
'Y'?
'Y':
'N';
40 $extranetSupport = isset($fields[
'EXTRANET_SUPPORT']) && $fields[
'EXTRANET_SUPPORT'] ==
'Y'?
'Y':
'N';
43 $class = isset($fields[
'CLASS'])? $fields[
'CLASS']:
'';
44 $methodCommandAdd = isset($fields[
'METHOD_COMMAND_ADD'])? $fields[
'METHOD_COMMAND_ADD']:
'';
45 $methodLangGet = isset($fields[
'METHOD_LANG_GET'])? $fields[
'METHOD_LANG_GET']:
'';
48 $appId = isset($fields[
'APP_ID'])? $fields[
'APP_ID']:
'';
49 $langSet = isset($fields[
'LANG'])? $fields[
'LANG']: Array();
55 if ($moduleId ==
'rest')
57 if (empty($appId) || empty($langSet) && $hidden ==
'N')
64 if (empty($class) || empty($methodCommandAdd))
68 if (empty($methodLangGet))
75 foreach ($commands as $cmd)
79 if ($botId == $cmd[
'BOT_ID'] && $command == $cmd[
'COMMAND'])
86 if ($appId == $cmd[
'APP_ID'] && $command == $cmd[
'COMMAND'])
91 else if ($moduleId == $cmd[
'MODULE_ID'] && $command == $cmd[
'COMMAND'])
97 $result = \Bitrix\Im\Model\CommandTable::add(Array(
99 'MODULE_ID' => $moduleId,
100 'COMMAND' => $command,
103 'SONET_SUPPORT' => $sonetSupport,
104 'EXTRANET_SUPPORT' => $extranetSupport,
106 'METHOD_COMMAND_ADD' => $methodCommandAdd,
107 'METHOD_LANG_GET' => $methodLangGet,
111 if (!$result->isSuccess())
114 $cache = \Bitrix\Main\Data\Cache::createInstance();
115 $cache->cleanDir(self::CACHE_PATH);
117 $commandId = $result->getId();
119 if ($moduleId ==
'rest')
121 foreach ($langSet as $lang)
123 if (!isset($lang[
'LANGUAGE_ID']) || empty($lang[
'LANGUAGE_ID']))
126 if (!isset($lang[
'TITLE']) && empty($lang[
'TITLE']))
131 \Bitrix\Im\Model\CommandLangTable::add(array(
132 'COMMAND_ID' => $commandId,
133 'LANGUAGE_ID' => mb_strtolower($lang[
'LANGUAGE_ID']),
134 'TITLE' => $lang[
'TITLE'],
135 'PARAMS' => isset($lang[
'PARAMS'])? $lang[
'PARAMS']:
''
149 $commandId = intval($command[
'COMMAND_ID']);
150 $moduleId = isset($command[
'MODULE_ID'])? $command[
'MODULE_ID']:
'';
151 $appId = isset($command[
'APP_ID'])? $command[
'APP_ID']:
'';
153 if (intval($commandId) <= 0)
156 if (!isset($command[
'FORCE']) || $command[
'FORCE'] ==
'N')
159 if (!isset($commands[$commandId]))
162 if ($moduleId <>
'' && $commands[$commandId][
'MODULE_ID'] != $moduleId)
165 if ($appId <>
'' && $commands[$commandId][
'APP_ID'] != $appId)
169 \Bitrix\Im\Model\CommandTable::delete($commandId);
171 $orm = \Bitrix\Im\Model\CommandLangTable::getList(Array(
172 'filter' => Array(
'=COMMAND_ID' => $commandId)
174 while ($row = $orm->fetch())
176 \Bitrix\Im\Model\CommandLangTable::delete($row[
'ID']);
179 $cache = \Bitrix\Main\Data\Cache::createInstance();
180 $cache->cleanDir(self::CACHE_PATH);
182 if (\
Bitrix\Main\Loader::includeModule(
'pull'))
184 \CPullStack::AddShared(Array(
186 'command' =>
'commandDelete',
188 'commandId' => $commandId
197 public static function update(array $command, array $updateFields)
199 $commandId = $command[
'COMMAND_ID'];
200 $moduleId = isset($command[
'MODULE_ID'])? $command[
'MODULE_ID']:
'';
201 $appId = isset($command[
'APP_ID'])? $command[
'APP_ID']:
'';
203 if (intval($commandId) <= 0)
207 if (!isset($commands[$commandId]))
210 if ($moduleId <>
'' && $commands[$commandId][
'MODULE_ID'] != $moduleId)
213 if ($appId <>
'' && $commands[$commandId][
'APP_ID'] != $appId)
216 if (isset($updateFields[
'LANG']) && $commands[$commandId][
'MODULE_ID'] ==
'rest')
218 $orm = \Bitrix\Im\Model\CommandLangTable::getList(Array(
219 'filter' => Array(
'=COMMAND_ID' => $commandId)
221 while ($row = $orm->fetch())
223 \Bitrix\Im\Model\CommandLangTable::delete($row[
'ID']);
225 foreach ($updateFields[
'LANG'] as $lang)
227 if (!isset($lang[
'LANGUAGE_ID']) || empty($lang[
'LANGUAGE_ID']))
230 if (!isset($lang[
'TITLE']) && empty($lang[
'TITLE']))
235 \Bitrix\Im\Model\CommandLangTable::add(array(
236 'COMMAND_ID' => $commandId,
237 'LANGUAGE_ID' => mb_strtolower($lang[
'LANGUAGE_ID']),
238 'TITLE' => $lang[
'TITLE'],
239 'PARAMS' => isset($lang[
'PARAMS'])? $lang[
'PARAMS']:
''
249 if (isset($updateFields[
'CLASS']) && !empty($updateFields[
'CLASS']))
251 $update[
'CLASS'] = $updateFields[
'CLASS'];
253 if (isset($updateFields[
'METHOD_COMMAND_ADD']))
255 $update[
'METHOD_COMMAND_ADD'] = $updateFields[
'METHOD_COMMAND_ADD'];
257 if (isset($updateFields[
'METHOD_LANG_GET']))
259 $update[
'METHOD_LANG_GET'] = $updateFields[
'METHOD_LANG_GET'];
261 if (isset($updateFields[
'COMMON']))
263 if ($commands[$commandId][
'BOT_ID'] <= 0)
265 $update[
'COMMON'] =
'Y';
269 $update[
'COMMON'] = $updateFields[
'COMMON'] ==
'Y'?
'Y':
'N';
272 if (isset($updateFields[
'HIDDEN']))
274 $update[
'HIDDEN'] = $updateFields[
'HIDDEN'] ==
'Y'?
'Y':
'N';
276 if (isset($updateFields[
'EXTRANET_SUPPORT']))
278 $update[
'EXTRANET_SUPPORT'] = $updateFields[
'EXTRANET_SUPPORT'] ==
'Y'?
'Y':
'N';
280 if (isset($updateFields[
'SONET_SUPPORT']))
282 $update[
'SONET_SUPPORT'] = $updateFields[
'SONET_SUPPORT'] ==
'Y'?
'Y':
'N';
286 \Bitrix\Im\Model\CommandTable::update($commandId, $update);
288 $cache = \Bitrix\Main\Data\Cache::createInstance();
289 $cache->cleanDir(self::CACHE_PATH);
297 $skipCommand = $messageFields[
'SKIP_COMMAND'] ??
null;
298 $isSystem = $messageFields[
'SYSTEM'] ??
null;
300 if ($skipCommand ===
'Y' || $isSystem ===
'Y')
306 if (empty($commands))
311 $commandList = Array();
312 $message = $messageFields[
'MESSAGE'] ??
null;
313 if (preg_match_all(
"/^\\/(?P<COMMAND>[^\\040\\n]*)(\\040?)(?P<PARAMS>.*)$/m", $message, $matches))
315 foreach($matches[
'COMMAND'] as $idx => $cmd)
317 $commandData = self::findCommands(Array(
'COMMAND' => $cmd,
'EXEC_PARAMS' => $matches[
'PARAMS'][$idx],
'MESSAGE_FIELDS' => $messageFields));
323 $commandList = array_merge($commandList, $commandData);
326 if (empty($commandList))
333 $messageFields[
'MESSAGE_OUT'],
334 $messageFields[
'NOTIFY_EVENT'],
335 $messageFields[
'NOTIFY_MODULE'],
336 $messageFields[
'URL_PREVIEW']
341 foreach ($commandList as $params)
343 if (!$params[
'MODULE_ID'] || !\
Bitrix\Main\Loader::includeModule($params[
'MODULE_ID']))
347 $hash = md5($params[
'EXEC_PARAMS'].$params[
'COMMAND_ID']);
348 if (isset($executed[$hash]) && $executed[$hash])
352 $executed[$hash] =
true;
360 if ($params[
'BOT_ID'] > 0)
362 Bot\Token::add($params[
'BOT_ID'], $messageFields[
'DIALOG_ID']);
363 if ($messageFields[
'MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
365 Bot\Token::add($params[
'BOT_ID'], $messageFields[
'TO_USER_ID']);
369 $messageFields[
'COMMAND'] = $params[
'COMMAND'];
370 $messageFields[
'COMMAND_ID'] = $params[
'COMMAND_ID'];
371 $messageFields[
'COMMAND_PARAMS'] = $params[
'EXEC_PARAMS'];
373 if ($params[
"METHOD_COMMAND_ADD"] && class_exists($params[
"CLASS"]) && method_exists($params[
"CLASS"], $params[
"METHOD_COMMAND_ADD"]))
375 if ($params[
'BOT_ID'] > 0)
377 \Bitrix\Im\Model\BotTable::update($params[
'BOT_ID'], array(
382 call_user_func_array(array($params[
"CLASS"], $params[
"METHOD_COMMAND_ADD"]), Array($messageId, $messageFields));
386 $messageFields[
'COMMAND'],
387 $messageFields[
'COMMAND_ID'],
388 $messageFields[
'COMMAND_PARAMS'],
389 $messageFields[
'COMMAND_CONTEXT']
394 ExecuteModuleEventEx($event, Array($commandList, $messageId, $messageFields));
400 public static function addMessage(array $access, array $messageFields)
402 $messageId = intval($access[
'MESSAGE_ID']);
403 $commandId = intval($access[
'COMMAND_ID']);
404 $moduleId = isset($access[
'MODULE_ID'])? $access[
'MODULE_ID']:
'';
405 $appId = isset($access[
'APP_ID'])? $access[
'APP_ID']:
'';
407 if ($messageId <= 0 || $commandId <= 0)
411 if (!isset($commands[$commandId]))
414 if ($moduleId <>
'' && $commands[$commandId][
'MODULE_ID'] != $moduleId)
417 if ($appId <>
'' && $commands[$commandId][
'APP_ID'] != $appId)
420 $botId = intval($commands[$commandId][
'BOT_ID']);
422 $orm = \Bitrix\Im\Model\MessageTable::getById($messageId);
423 if (!($message = $orm->fetch()))
426 $orm = \Bitrix\Im\Model\ChatTable::getById($message[
'CHAT_ID']);
427 if (!($chat = $orm->fetch()))
430 $relations = \CIMChat::GetRelationById($message[
'CHAT_ID'],
false,
true,
false);
432 $chatWithBot =
false;
433 foreach ($relations as $userId => $relation)
435 if ($relation[
'MESSAGE_TYPE'] != IM_MESSAGE_PRIVATE)
437 $messageFields[
'DIALOG_ID'] =
'chat'.$message[
'CHAT_ID'];
440 if ($message[
'AUTHOR_ID'] == $botId)
443 if ($botId != $relation[
'USER_ID'])
445 $messageFields[
'DIALOG_ID'] = $relation[
'USER_ID'];
449 else if ($message[
'AUTHOR_ID'] != $relation[
'USER_ID'])
451 $messageFields[
'DIALOG_ID'] = $relation[
'USER_ID'];
458 $grantAccess =
false;
459 if (
Bot\Token::isActive($botId, $messageFields[
'DIALOG_ID']))
466 $grantAccess =
false;
467 if (preg_match_all(
"/^\\/(?P<COMMAND>[^\\040\\n]*)(\\040?)(?P<PARAMS>.*)$/m", $message[
'MESSAGE'], $matches))
469 foreach($matches[
'COMMAND'] as $idx => $cmd)
471 if ($commands[$commandId][
'COMMAND'] == $cmd)
482 $messageFields[
'ATTACH'] = $messageFields[
'ATTACH']? $messageFields[
'ATTACH']:
null;
483 $messageFields[
'KEYBOARD'] = $messageFields[
'KEYBOARD']? $messageFields[
'KEYBOARD']:
null;
484 $messageFields[
'MENU'] = $messageFields[
'MENU']? $messageFields[
'MENU']:
null;
488 $chatId = \Bitrix\Im\Dialog::getChatId($messageFields[
'DIALOG_ID']);
492 if (isset($relations[$botId]) && $messageFields[
'SYSTEM'] !=
'Y')
495 "FROM_USER_ID" => $botId,
496 "TO_CHAT_ID" => $chatId,
497 "ATTACH" => $messageFields[
'ATTACH'],
498 "KEYBOARD" => $messageFields[
'KEYBOARD'],
499 "MENU" => $messageFields[
'MENU'],
501 if (isset($messageFields[
'MESSAGE']))
503 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
509 "FROM_USER_ID" => $botId,
510 "TO_CHAT_ID" => $chatId,
511 "ATTACH" => $messageFields[
'ATTACH'],
512 "KEYBOARD" => $messageFields[
'KEYBOARD'],
513 "MENU" => $messageFields[
'MENU'],
516 if (isset($messageFields[
'MESSAGE']))
518 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
522 $ar[
'MESSAGE'] =
Loc::getMessage(
"COMMAND_BOT_ANSWER", Array(
"#BOT_NAME#" =>
"[USER=".$botId.
"][/USER]\n ")).$ar[
'MESSAGE'];
526 $ar[
'MESSAGE'] =
"[B]".Loc::getMessage(
"COMMAND_SYSTEM_ANSWER", Array(
"#COMMAND#" =>
"/".$commands[$commandId][
'COMMAND'])).
"[/B]\n ".$ar[
'MESSAGE'];
530 if (isset($messageFields[
'URL_PREVIEW']) && $messageFields[
'URL_PREVIEW'] ==
'N')
532 $ar[
'URL_PREVIEW'] =
'N';
535 $ar[
'SKIP_USER_CHECK'] =
'Y';
536 $ar[
'SKIP_COMMAND'] =
'Y';
538 $id = \CIMChat::AddMessage($ar);
544 $message[
'AUTHOR_ID'] = intval($messageFields[
'DIALOG_ID']);
549 $userId = intval($messageFields[
'DIALOG_ID']);
551 \CModule::IncludeModule(
'imbot');
552 if ($botId == $userId && $messageFields[
'SYSTEM'] !=
'Y')
555 "FROM_USER_ID" => $userId,
556 "TO_USER_ID" => $message[
'AUTHOR_ID'],
557 "ATTACH" => $messageFields[
'ATTACH'],
558 "KEYBOARD" => $messageFields[
'KEYBOARD'],
560 if (isset($messageFields[
'MESSAGE']))
562 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
568 "FROM_USER_ID" => $message[
'AUTHOR_ID'],
569 "TO_USER_ID" => $userId,
570 "ATTACH" => $messageFields[
'ATTACH'],
571 "KEYBOARD" => $messageFields[
'KEYBOARD'],
574 if (isset($messageFields[
'MESSAGE']))
576 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
580 $ar[
'MESSAGE'] =
Loc::getMessage(
"COMMAND_BOT_ANSWER", Array(
"#BOT_NAME#" =>
"[USER=".$botId.
"][/USER]\n ")).$ar[
'MESSAGE'];
584 $ar[
'MESSAGE'] =
"[B]".Loc::getMessage(
"COMMAND_SYSTEM_ANSWER", Array(
"#COMMAND#" =>
"/".$commands[$commandId][
'COMMAND'])).
"[/B]\n ".$ar[
'MESSAGE'];
588 if (isset($messageFields[
'URL_PREVIEW']) && $messageFields[
'URL_PREVIEW'] ==
'N')
590 $ar[
'URL_PREVIEW'] =
'N';
593 $ar[
'SKIP_COMMAND'] =
'Y';
594 $id = \CIMMessage::Add($ar);
729 $cache = \Bitrix\Main\Data\Cache::createInstance();
730 if($cache->initCache(self::CACHE_TTL,
'list_v5_'.$lang, self::CACHE_PATH))
732 $result = $cache->getVars();
736 $loadRestLang =
false;
738 $orm = \Bitrix\Im\Model\CommandTable::getList();
739 while ($row = $orm->fetch())
741 $row[
'COMMAND_ID'] = $row[
'ID'];
742 $row[
'CONTEXT'] =
'';
744 if ($row[
'BOT_ID'] > 0)
746 $row[
'CATEGORY'] = \Bitrix\Im\User::getInstance($row[
'BOT_ID'])->getFullName();
748 else if ($row[
'MODULE_ID'] ==
'im')
754 $moduleClass = new \CModule();
755 $module = $moduleClass->createModuleObject($row[
'MODULE_ID']);
756 $row[
'CATEGORY'] = $module->MODULE_NAME;
759 if (!empty($row[
'CLASS']) && !empty($row[
'METHOD_LANG_GET']))
761 if (\
Bitrix\Main\Loader::includeModule($row[
'MODULE_ID']) && class_exists($row[
"CLASS"]) && method_exists($row[
"CLASS"], $row[
"METHOD_LANG_GET"]))
763 $localize = call_user_func_array(array($row[
"CLASS"], $row[
"METHOD_LANG_GET"]), Array($row[
'COMMAND'], $lang));
766 $row[
'TITLE'] = $localize[
'TITLE'];
767 $row[
'PARAMS'] = $localize[
'PARAMS'];
771 $row[
'HIDDEN'] =
'Y';
772 $row[
'METHOD_LANG_GET'] =
'';
777 $row[
'HIDDEN'] =
'Y';
778 $row[
'METHOD_LANG_GET'] =
'';
785 if ($row[
'MODULE_ID'] ==
'rest')
787 $loadRestLang =
true;
788 if ($row[
'BOT_ID'] <= 0 && $row[
'APP_ID'])
790 $res = \Bitrix\Rest\AppTable::getList([
791 'filter' => array(
'=CLIENT_ID' => $row[
'APP_ID']),
793 if ($app = $res->fetch())
795 $row[
'CATEGORY'] = !empty($app[
'APP_NAME'])
797 : (!empty($app[
'APP_NAME_DEFAULT'])
798 ? $app[
'APP_NAME_DEFAULT']
806 $result[$row[
'COMMAND_ID']] = $row;
812 $orm = \Bitrix\Im\Model\CommandLangTable::getList();
813 while ($row = $orm->fetch())
815 if (!isset($result[$row[
'COMMAND_ID']]))
818 $langSet[$row[
'COMMAND_ID']][$row[
'LANGUAGE_ID']][
'TITLE'] = $row[
'TITLE'];
819 $langSet[$row[
'COMMAND_ID']][$row[
'LANGUAGE_ID']][
'PARAMS'] = $row[
'PARAMS'];
822 $langAlter = \Bitrix\Im\Bot::getDefaultLanguage();
823 foreach ($result as $commandId => $commandData)
825 if (isset($langSet[$commandId][$lang]))
827 $result[$commandId][
'TITLE'] = $langSet[$commandId][$lang][
'TITLE'];
828 $result[$commandId][
'PARAMS'] = $langSet[$commandId][$lang][
'PARAMS'];
830 else if (isset($langSet[$commandId][$langAlter]))
832 $result[$commandId][
'TITLE'] = $langSet[$commandId][$langAlter][
'TITLE'];
833 $result[$commandId][
'PARAMS'] = $langSet[$commandId][$langAlter][
'PARAMS'];
835 else if (isset($langSet[$commandId]))
837 $langSetCommand = array_values($langSet[$commandId]);
838 $result[$commandId][
'TITLE'] = $langSetCommand[0][
'TITLE'];
839 $result[$commandId][
'PARAMS'] = $langSetCommand[0][
'PARAMS'];
843 foreach ($result as $key => $value)
845 if (empty($value[
'TITLE']))
847 $result[$key][
'HIDDEN'] =
'Y';
848 $row[
'METHOD_LANG_GET'] =
'';
855 \Bitrix\Main\Type\Collection::sortByColumn(
857 Array(
'MODULE_ID' => SORT_ASC),
864 $result = self::mergeWithDefaultCommands($result);
866 $cache->startDataCache();
867 $cache->endDataCache($result);