• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/rest.php
  • Класс: BitrixImOpenLinesRest
  • Вызов: Rest::networkMessageAdd
static function networkMessageAdd($arParams, $n, CRestServer $server)
{
	if ($server->getAuthType() == SessionAuthAuth::AUTH_TYPE)
	{
		throw new RestException('Access for this method not allowed by session authorization.', 'WRONG_AUTH_TYPE', CRestServer::STATUS_FORBIDDEN);
	}
	$arParams = array_change_key_case($arParams, CASE_UPPER);
	if (!isset($arParams['CODE']) || mb_strlen($arParams['CODE']) != 32)
	{
		throw new RestException('You entered an invalid code', 'CODE', CRestServer::STATUS_WRONG_REQUEST);
	}

	if (!Loader::includeModule('imbot'))
	{
		throw new RestException('Module IMBOT is not installed', 'IMBOT_ERROR', CRestServer::STATUS_WRONG_REQUEST);
	}

	if (Network::isFdcCode($arParams['CODE']))
	{
		throw new RestException('Line not found', 'NOT_FOUND', CRestServer::STATUS_WRONG_REQUEST);
	}

	$networkBot = null;

	$bots = ImBot::getListCache();
	foreach ($bots as $bot)
	{
		if ($bot['APP_ID'] == $arParams['CODE'])
		{
			$networkBot = $bot;
			break;
		}
	}
	if (!$networkBot)
	{
		throw new RestException('Line not found', 'NOT_FOUND', CRestServer::STATUS_WRONG_REQUEST);
	}

	$arMessageFields = Array();

	$arMessageFields['DIALOG_ID'] = (int)$arParams['USER_ID'];
	if (empty($arMessageFields['DIALOG_ID']))
	{
		throw new RestException('User ID can't be empty', 'USER_ID_EMPTY', CRestServer::STATUS_WRONG_REQUEST);
	}

	$isBitrix24 = Loader::includeModule('bitrix24');
	if (
		$isBitrix24
		&& !CBitrix24::IsNfrLicense()
		|| !$isBitrix24
		&& !defined('IMOPENLINES_NETWORK_LIMIT')
	)
	{
		$dateLimit = new DateTime();
		$dateLimit->add('-1 WEEK');

		$check = ModelRestNetworkLimitTable::getList([
			'filter' => [
				'=BOT_ID' => $networkBot['BOT_ID'],
				'=USER_ID' => $arMessageFields['DIALOG_ID'],
				'>DATE_CREATE' => $dateLimit
			]
		])->fetch();
		if ($check)
		{
			throw new RestException('You cant send more than one message per week to each user.', 'USER_MESSAGE_LIMIT', CRestServer::STATUS_WRONG_REQUEST);
		}
	}

	$arMessageFields['MESSAGE'] = trim($arParams['MESSAGE']);
	if ($arMessageFields['MESSAGE'] == '')
	{
		throw new RestException('Message can't be empty', 'MESSAGE_EMPTY', CRestServer::STATUS_WRONG_REQUEST);
	}

	if (isset($arParams['ATTACH']) && !empty($arParams['ATTACH']))
	{
		$attach = CIMMessageParamAttach::GetAttachByJson($arParams['ATTACH']);
		if ($attach)
		{
			if ($attach->IsAllowSize())
			{
				$arMessageFields['ATTACH'] = $attach;
			}
			else
			{
				throw new RestException('You have exceeded the maximum allowable size of attach', 'ATTACH_OVERSIZE', CRestServer::STATUS_WRONG_REQUEST);
			}
		}
		else if ($arParams['ATTACH'])
		{
			throw new RestException('Incorrect attach params', 'ATTACH_ERROR', CRestServer::STATUS_WRONG_REQUEST);
		}
	}

	if (isset($arParams['KEYBOARD']) && !empty($arParams['KEYBOARD']))
	{
		$keyboard = Array();
		if (!isset($arParams['KEYBOARD']['BUTTONS']))
		{
			$keyboard['BUTTONS'] = $arParams['KEYBOARD'];
		}
		else
		{
			$keyboard = $arParams['KEYBOARD'];
		}
		$keyboard['BOT_ID'] = $arParams['BOT_ID'];

		$keyboard = ImBotKeyboard::getKeyboardByJson($keyboard);
		if ($keyboard)
		{
			$arMessageFields['KEYBOARD'] = $keyboard;
		}
		else
		{
			throw new RestException('Incorrect keyboard params', 'KEYBOARD_ERROR', CRestServer::STATUS_WRONG_REQUEST);
		}
	}

	if (isset($arParams['URL_PREVIEW']) && $arParams['URL_PREVIEW'] == 'N')
	{
		$arMessageFields['URL_PREVIEW'] = 'N';
	}
	$arMessageFields['PARAMS']['IMOL_QUOTE_MSG'] = 'Y';

	$id = ImBot::addMessage(array('BOT_ID' => $networkBot['BOT_ID']), $arMessageFields);
	if (!$id)
	{
		throw new RestException('Message isn't added', 'WRONG_REQUEST', CRestServer::STATUS_WRONG_REQUEST);
	}

	ModelRestNetworkLimitTable::add(Array('BOT_ID' => $networkBot['BOT_ID'], 'USER_ID' => $arMessageFields['DIALOG_ID']));

	return true;
}