• Модуль: imbot
  • Путь к файлу: ~/bitrix/modules/imbot/lib/bot/supportbox.php
  • Класс: BitrixImBotBotSupportBox
  • Вызов: SupportBox::register
static function register(array $params = []): int
{
	if (!MainLoader::includeModule('im'))
	{
		return -1;
	}

	if (self::isInstalled() && self::getBotId() > 0)
	{
		return self::getBotId();// do nothing
	}

	ImBot::clearCache();

	$showActivateMessage = true;

	$botParams = [
		'MODULE_ID' => self::MODULE_ID,
		'CLASS' => __CLASS__,
		'METHOD_WELCOME_MESSAGE' => 'onChatStart',/** @see SupportBox::onChatStart */
		'METHOD_MESSAGE_ADD' => 'onMessageAdd',/** @see SupportBox::onMessageAdd */
		'METHOD_BOT_DELETE' => 'onBotDelete',/** @see SupportBox::onBotDelete */
		'PROPERTIES' => [
			'NAME' => self::getBotName(),
			'WORK_POSITION' => self::getBotDesc(),
		]
	];
	$botAvatar = self::uploadAvatar(self::getBotAvatar());
	if (!empty($botAvatar))
	{
		$botParams['PROPERTIES']['PERSONAL_PHOTO'] = $botAvatar;
	}

	$botId = self::getPreviousBotId();
	if ($botId === null)
	{
		// Stage I - register as local bot
		$botParams['CODE'] = self::BOT_CODE;
		$botParams['TYPE'] = ImBot::TYPE_BOT;
		$botParams['INSTALL_TYPE'] =  ImBot::INSTALL_TYPE_SILENT;

		$botId = (int)ImBot::register($botParams);

		self::setBotId($botId);
	}
	elseif ($botId)
	{
		$botCache = ImBot::getCache($botId);

		self::setBotId($botId);

		// upgrade previous one
		ImBot::update(['BOT_ID' => $botId], $botParams);

		// perform activation
		if (self::updateBotProperties())
		{
			$showActivateMessage = false;

			self::setActive(true);

			if ($botCache['APP_ID'] !== '' && $botCache['APP_ID'] !== self::getBotCode())
			{
				self::sendRequestFinalizeSession([
					'BOT_CODE' => $botCache['APP_ID'],
					'MESSAGE' => Loc::getMessage('SUPPORT_BOX_CHANGE_LINE'),
				]);
			}
		}
	}

	if ($botId)
	{
		self::registerCommands($botId);
		self::registerApps($botId);

		// set recent contact for admins
		self::setAsRecent();

		self::restoreQuestionHistory();
	}

	// must be activated before use
	if ($showActivateMessage)
	{
		self::setActive(false);

		$keyboard = new Keyboard(self::getBotId());
		$keyboard->addButton([
			'DISPLAY' => 'LINE',
			'TEXT' =>  Loc::getMessage('SUPPORT_BOX_ACTIVATE'),
			'BG_COLOR' => '#29619b',
			'TEXT_COLOR' => '#fff',
			'BLOCK' => 'Y',
			'COMMAND' => self::COMMAND_ACTIVATE,
		]);

		parent::sendMessage([
			'DIALOG_ID' => self::USER_LEVEL_ADMIN,
			'MESSAGE' => Loc::getMessage('SUPPORT_BOX_WELCOME_MESSAGE'),
			'KEYBOARD' => $keyboard,
			'SYSTEM' => 'N',
			'URL_PREVIEW' => 'N',
		]);
	}
	else
	{
		self::notifyAdministrators(self::getMessage('ACTIVATION_SUCCESS', Loc::getMessage('SUPPORT_BOX_ACTIVATION_SUCCESS')));
	}

	return $botId;
}