• Модуль: imbot
  • Путь к файлу: ~/bitrix/modules/imbot/lib/bot/support24.php
  • Класс: BitrixImBotBotSupport24
  • Вызов: Support24::execScheduleAction
static function execScheduleAction($target, $action, $code = ''): bool
{
	if (!MainLoader::includeModule('im'))
	{
		return false;
	}

	if ($action === self::SCHEDULE_ACTION_WELCOME)
	{
		if (!is_numeric($target))
		{
			// only for user
			return false;
		}
		if (self::getSupportLevel() != self::SUPPORT_LEVEL_FREE)
		{
			return true;
		}
		if (!self::isActiveFreeSupport() || !self::isActiveFreeSupportForUser($target))
		{
			return true;
		}

		CIMMessage::getChatId($target, self::getBotId());
	}
	elseif ($action === self::SCHEDULE_ACTION_INVOLVEMENT)
	{
		if (!is_numeric($target))
		{
			// only for user
			return false;
		}
		if (self::getSupportLevel() != self::SUPPORT_LEVEL_FREE)
		{
			return true;
		}
		if (!self::isActiveFreeSupport() || !self::isActiveFreeSupportForUser($target))
		{
			return true;
		}

		$generationDate = (int)Option::get('imbot', self::OPTION_BOT_FREE_START_DATE, 0);
		$currentDay = (int)floor((time() - $generationDate) / 86400) + 1;

		self::scheduleAction($target, self::SCHEDULE_ACTION_INVOLVEMENT, '', 24*60);

		$message = self::getMessage((string)$currentDay);
		if ($message == '')
		{
			return false;
		}

		$lastMessageMinTime = self::INVOLVEMENT_LAST_MESSAGE_BLOCK_TIME * 60 * 60; // hour to second

		$query = "
			SELECT
				RU.USER_ID,
				RU.CHAT_ID,
				IF(UNIX_TIMESTAMP(MB.DATE_CREATE) > UNIX_TIMESTAMP()-".$lastMessageMinTime.", 'Y', 'N') BOT_RECENTLY_TALK,
				IF(UNIX_TIMESTAMP(MU.DATE_CREATE) > UNIX_TIMESTAMP()-".$lastMessageMinTime.", 'Y', 'N') USER_RECENTLY_TALK
			FROM
				b_im_relation RB LEFT JOIN b_im_message MB ON RB.LAST_ID = MB.ID,
				b_im_relation RU LEFT JOIN b_im_message MU ON RU.LAST_ID = MU.ID
			WHERE
				RB.USER_ID = ".self::getBotId()."
			and RU.USER_ID = ".$target."
			and RB.MESSAGE_TYPE = '".IM_MESSAGE_PRIVATE."'
			and RU.MESSAGE_TYPE = '".IM_MESSAGE_PRIVATE."'
			and RB.CHAT_ID = RU.CHAT_ID
		";
		$dialog = MainApplication::getInstance()->getConnection()->query($query)->fetch();

		if (
			$dialog['BOT_RECENTLY_TALK'] == 'Y'
			|| $dialog['USER_RECENTLY_TALK'] == 'Y'
		)
		{
			return false;
		}

		self::sendMessage([
			'DIALOG_ID' => $target,
			'MESSAGE' => $message,
			'SYSTEM' => 'N',
			'URL_PREVIEW' => 'N'
		]);

		return true;
	}
	elseif ($action === self::SCHEDULE_ACTION_MESSAGE)
	{
		$code = trim($code);
		if ($code == '')
		{
			return false;
		}

		$message = self::getMessage($code);
		if ($message == '')
		{
			return false;
		}

		self::sendMessage([
			'DIALOG_ID' => $target,
			'MESSAGE' => $message,
			'SYSTEM' => 'N',
			'URL_PREVIEW' => 'N'
		]);
	}
	elseif ($action === self::SCHEDULE_ACTION_PARTNER_JOIN)
	{
		$keyboard = new Keyboard(self::getBotId());
		$keyboard->addButton([
			"DISPLAY" => "LINE",
			"TEXT" => self::getMessage('PARTNER_BUTTON_YES'),
			"BG_COLOR" => "#29619b",
			"TEXT_COLOR" => "#fff",
			"BLOCK" => "Y",
			"COMMAND" => self::COMMAND_SUPPORT24,
			"COMMAND_PARAMS" => self::COMMAND_ACTIVATE_PARTNER,
		]);
		$keyboard->addButton([
			"DISPLAY" => "LINE",
			"TEXT" => self::getMessage('PARTNER_BUTTON_NO'),
			"BG_COLOR" => "#990000",
			"TEXT_COLOR" => "#fff",
			"BLOCK" => "Y",
			"COMMAND" => self::COMMAND_SUPPORT24,
			"COMMAND_PARAMS" => self::COMMAND_DECLINE_PARTNER_REQUEST,
		]);

		self::sendMessage([
			'DIALOG_ID' => $target,
			'MESSAGE' => self::getMessage('PARTNER_REQUEST'),
			'KEYBOARD' => $keyboard,
			'SYSTEM' => 'N',
			'URL_PREVIEW' => 'N'
		]);

		return true;
	}
	elseif ($action === self::SCHEDULE_ACTION_HIDE_DIALOG)
	{
		$session = self::instanceDialogSession((int)self::getBotId(), $target);

		$sessionActive = (
			$session->getSessionId() > 0
			&& $session->getParam('DATE_FINISH') === null
		);

		if (!$sessionActive)// don't hide active session
		{
			if (BitrixImCommon::isChatId($target))
			{
				$chatId = self::getChatId($target);
				if ($chatId > 0)
				{
					$relations = ImChat::getRelation($chatId, ['SELECT' => ['ID', 'USER_ID']]);
					foreach ($relations as $relation)
					{
						if ((int)$relation['USER_ID'] != static::getBotId())
						{
							CIMContactList::dialogHide($target, (int)$relation['USER_ID']);
						}
					}
				}
			}
			else
			{
				CIMContactList::dialogHide(self::getBotId(), $target);
			}
		}
	}
	elseif ($action === self::SCHEDULE_ACTION_CHECK_STAGE)
	{
		if ($code === 'START')
		{
			self::sendNotifyPortalStageMode([
				'IS_STAGE_STARTED' => true
			]);
		}
		elseif ($code === 'STOP')
		{
			self::sendNotifyPortalStageMode([
				'IS_STAGE_STOPPED' => true
			]);
		}
	}

	return true;
}