- Модуль: im
- Путь к файлу: ~/bitrix/modules/im/classes/general/im_rest.php
- Класс: CIMRestService
- Вызов: CIMRestService::botMessageAdd
static function botMessageAdd($arParams, $n, CRestServer $server)
{
$arParams = array_change_key_case($arParams, CASE_UPPER);
$clientId = $server->getClientId();
if (!$clientId)
{
if (!empty($arParams['CLIENT_ID']))
{
$clientId = 'custom'.$arParams['CLIENT_ID'];
}
else
{
throw new BitrixRestAccessException("Client ID not specified");
}
}
$bots = BitrixImBot::getListCache();
if (isset($arParams['BOT_ID']))
{
if (!isset($bots[$arParams['BOT_ID']]))
{
throw new BitrixRestRestException("Bot not found", "BOT_ID_ERROR", CRestServer::STATUS_WRONG_REQUEST);
}
if ($clientId && $bots[$arParams['BOT_ID']]['APP_ID'] != $clientId)
{
throw new BitrixRestRestException("Bot was installed by another rest application", "APP_ID_ERROR", CRestServer::STATUS_WRONG_REQUEST);
}
}
else
{
$botFound = false;
foreach ($bots as $bot)
{
if ($bot['APP_ID'] == $clientId)
{
$botFound = true;
$arParams['BOT_ID'] = $bot['BOT_ID'];
break;
}
}
if (!$botFound)
{
throw new BitrixRestRestException("Bot not found", "BOT_ID_ERROR", CRestServer::STATUS_WRONG_REQUEST);
}
}
$arMessageFields = Array();
if (intval($arParams['FROM_USER_ID']) && intval($arParams['TO_USER_ID']))
{
$arParams['SYSTEM'] = 'Y';
$arMessageFields['FROM_USER_ID'] = intval($arParams['FROM_USER_ID']);
$arMessageFields['TO_USER_ID'] = intval($arParams['TO_USER_ID']);
}
else
{
$arMessageFields['DIALOG_ID'] = $arParams['DIALOG_ID'];
if (!BitrixImCommon::isDialogId($arMessageFields['DIALOG_ID']))
{
throw new BitrixRestRestException("Dialog ID can't be empty", "DIALOG_ID_EMPTY", CRestServer::STATUS_WRONG_REQUEST);
}
}
$arMessageFields['MESSAGE'] = trim($arParams['MESSAGE']);
if ($arMessageFields['MESSAGE'] == '')
{
throw new BitrixRestRestException("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 BitrixRestRestException("You have exceeded the maximum allowable size of attach", "ATTACH_OVERSIZE", CRestServer::STATUS_WRONG_REQUEST);
}
}
else if ($arParams['ATTACH'])
{
throw new BitrixRestRestException("Incorrect attach params", "ATTACH_ERROR", CRestServer::STATUS_WRONG_REQUEST);
}
}
if (isset($arParams['KEYBOARD']) && !empty($arParams['KEYBOARD']))
{
$keyboard = Array();
if (is_string($arParams['KEYBOARD']))
{
$arParams['KEYBOARD'] = CUtil::JsObjectToPhp($arParams['KEYBOARD']);
}
if (!isset($arParams['KEYBOARD']['BUTTONS']))
{
$keyboard['BUTTONS'] = $arParams['KEYBOARD'];
}
else
{
$keyboard = $arParams['KEYBOARD'];
}
$keyboard['BOT_ID'] = $arParams['BOT_ID'];
$keyboard = BitrixImBotKeyboard::getKeyboardByJson($keyboard);
if ($keyboard)
{
$arMessageFields['KEYBOARD'] = $keyboard;
}
else
{
throw new BitrixRestRestException("Incorrect keyboard params", "KEYBOARD_ERROR", CRestServer::STATUS_WRONG_REQUEST);
}
}
if (isset($arParams['MENU']) && !empty($arParams['MENU']))
{
$menu = Array();
if (is_string($arParams['MENU']))
{
$arParams['MENU'] = CUtil::JsObjectToPhp($arParams['MENU']);
}
if (!isset($arParams['MENU']['ITEMS']))
{
$menu['ITEMS'] = $arParams['MENU'];
}
else
{
$menu = $arParams['MENU'];
}
$menu['BOT_ID'] = $arParams['BOT_ID'];
$menu = BitrixImBotContextMenu::getByJson($menu);
if ($menu)
{
$arMessageFields['MENU'] = $menu;
}
else
{
throw new BitrixRestRestException("Incorrect menu params", "MENU_ERROR", CRestServer::STATUS_WRONG_REQUEST);
}
}
if (isset($arParams['SYSTEM']) && $arParams['SYSTEM'] == 'Y')
{
$arMessageFields['SYSTEM'] = 'Y';
}
if (isset($arParams['URL_PREVIEW']) && $arParams['URL_PREVIEW'] == 'N')
{
$arMessageFields['URL_PREVIEW'] = 'N';
}
if (isset($arParams['SKIP_CONNECTOR']) && mb_strtoupper($arParams['SKIP_CONNECTOR']) == 'Y')
{
$arMessageFields['SKIP_CONNECTOR'] = 'Y';
$arMessageFields['SILENT_CONNECTOR'] = 'Y';
}
$id = BitrixImBot::addMessage(array('BOT_ID' => $arParams['BOT_ID']), $arMessageFields);
if (!$id)
{
throw new BitrixRestRestException("Message isn't added", "WRONG_REQUEST", CRestServer::STATUS_WRONG_REQUEST);
}
return $id;
}