function sendMessage(int $fromUserId, int $toUserId, string $message)
{
if (! \Bitrix\Main\Loader::includeModule('im')) {
throw new \Bitrix\Main\LoaderException('Unable to load IM module');
}
$fields = [
"TO_USER_ID" => $fromUserId, // ID пользователя
"FROM_USER_ID" => $toUserId, // От кого (0 - системное)
"MESSAGE_TYPE" => "S",
"NOTIFY_MODULE" => "im",
"NOTIFY_MESSAGE" => $message, // Текст сообщения
];
$msg = new \CIMMessenger();
if (! $msg->Add($fields)) {
$e = $GLOBALS['APPLICATION']->GetException();
throw new \Bitrix\Main\SystemException($e->GetString()); // $e->GetString() - тут находится сообщение об ошибке
}
}
// Приветствуем пользователя с ID 1
sendMessage(1, 0, 'Здарова!'); |