• Модуль: imbot
  • Путь к файлу: ~/bitrix/modules/imbot/lib/bot/supportbox.php
  • Класс: BitrixImBotBotSupportBox
  • Вызов: SupportBox::refreshAgent
static function refreshAgent(bool $regular = true): string
{
	do
	{
		$prevBotCode = self::getBotCode();
		if (!self::isEnabled() || empty($prevBotCode))
		{
			return '';
		}

		if (self::checkPublicUrl() !== true)
		{
			break;
		}

		$settings = self::getBotSettings([
			'BOT_ID' => self::getBotId()
		]);
		if (empty($settings))
		{
			break;
		}

		$botCode = $settings[self::OPTION_BOT_CODE];
		if (empty($botCode))
		{
			//err
			break;
		}

		$botParams = [];

		if ($prevBotCode !== $botCode)
		{
			$botParams['CODE'] = parent::BOT_CODE. '_'. $botCode;
			$botParams['APP_ID'] = $botCode;
		}

		if (!self::saveSettings($settings))
		{
			break;
		}

		$botId = self::getBotId();

		$botParams['PROPERTIES'] = [
			'NAME' => self::getBotName(),
			'WORK_POSITION' => self::getBotDesc(),
		];

		$botData = ImUser::getInstance($botId);
		$userAvatar = (int)ImUser::uploadAvatar(self::getBotAvatar(), $botId);
		if ($userAvatar > 0 && (int)$botData->getAvatarId() != $userAvatar)
		{
			$botParams['PROPERTIES']['PERSONAL_PHOTO'] = $userAvatar;
		}

		ImBot::clearCache();
		ImBot::update(['BOT_ID' => $botId], $botParams);

		self::registerCommands();
		self::registerApps();
	}
	while (false);

	$error = self::getError();
	if ($error->error === true && $error->msg !== '')
	{
		$helpDeskUrl = '';
		if (MainLoader::includeModule('ui'))
		{
			$helpDeskUrl = BitrixUIUtil::getArticleUrlByCode(self::HELP_DESK_CODE);
		}

		$message = Loc::getMessage('SUPPORT_BOX_REFRESH_ERROR', [
			'#ERROR#' => $error->msg,
			'#HELP_DESK#' => $helpDeskUrl,
		]);

		if (MainLoader::includeModule('im'))
		{
			CIMNotify::deleteBySubTag("IMBOT|SUPPORT|ERR");

			$notifyUsers = self::getAdministrators();
			foreach ($notifyUsers as $userId)
			{
				CIMNotify::add([
					"TO_USER_ID" => $userId,
					"NOTIFY_TYPE" => IM_NOTIFY_SYSTEM,
					"NOTIFY_MODULE" => self::MODULE_ID,
					"NOTIFY_EVENT" => 'refresh_error',
					"NOTIFY_SUB_TAG" => "IMBOT|SUPPORT|ERR",
					"NOTIFY_MESSAGE" => $message,
					"NOTIFY_MESSAGE_OUT" => $message,
					"RECENT_ADD" => 'Y',
				]);
			}
		}
	}
	else
	{
		if (MainLoader::includeModule('im'))
		{
			CIMNotify::deleteBySubTag("IMBOT|SUPPORT|ERR");
		}
	}

	return $regular ? __METHOD__. '();' : '';
}