41 if ($sendingConfig ===
null)
45 $this->sendingConfig = $sendingConfig;
65 $uuidAddResult = $this->uuidService->add();
69 $messageIdByUuid = $this->uuidService->getMessageId();
72 if (!is_null($messageIdByUuid))
74 return $result->setResult([
'messageId' => $messageIdByUuid]);
79 if (!$this->uuidService->updateIfExpired())
95 if (isset($this->uuidService))
97 $this->uuidService->updateMessageId($message->
getMessageId());
117 $event =
new Event(
'im', static::EVENT_BEFORE_MESSAGE_ADD, [
118 'message' => $message->
toArray(),
119 'parameters' => $this->sendingConfig->toArray(),
123 foreach ($event->getResults() as $eventResult)
125 $eventParams = $eventResult->getParameters();
126 if ($eventResult->getType() === EventResult::SUCCESS)
130 if (isset($eventParams[
'message']) && is_array($eventParams[
'message']))
133 $eventParams[
'message'][
'MESSAGE_ID'],
134 $eventParams[
'message'][
'CHAT_ID'],
135 $eventParams[
'message'][
'AUTHOR_ID'],
136 $eventParams[
'message'][
'FROM_USER_ID']
138 $message->
fill($eventParams[
'message']);
140 if (isset($eventParams[
'parameters']) && is_array($eventParams[
'parameters']))
142 $this->sendingConfig->fill($eventParams[
'parameters']);
146 elseif ($eventResult->getType() === EventResult::ERROR)
148 if ($eventParams && isset($eventParams[
'error']))
150 if ($eventParams[
'error'] instanceof Main\
Error)
152 $result->addError($eventParams[
'error']);
154 elseif (is_string($eventParams[
'error']))
181 $compatibleFields = array_merge(
183 $this->sendingConfig->toArray(),
185 $compatibleChatFields = $chat->toArray();
187 foreach (\GetModuleEvents(
'im', self::EVENT_BEFORE_CHAT_MESSAGE_ADD,
true) as $event)
189 $eventResult = \ExecuteModuleEventEx($event, [$compatibleFields, $compatibleChatFields]);
190 if ($eventResult ===
false || isset($eventResult[
'result']) && $eventResult[
'result'] ===
false)
192 $reason = $this->detectReasonSendError($chat->
getType(), $eventResult[
'reason'] ??
'');
196 if (isset($eventResult[
'fields']) && is_array($eventResult[
'fields']))
199 $eventResult[
'fields'][
'MESSAGE_ID'],
200 $eventResult[
'fields'][
'CHAT_ID'],
201 $eventResult[
'fields'][
'AUTHOR_ID'],
202 $eventResult[
'fields'][
'FROM_USER_ID']
204 $message->
fill($eventResult[
'fields']);
205 $this->sendingConfig->fill($eventResult[
'fields']);
224 $compatibleFields = array_merge(
229 'EXTRA_PARAMS' => [],
233 $this->sendingConfig->toArray(),
236 foreach (\GetModuleEvents(
'im', static::EVENT_AFTER_MESSAGE_ADD,
true) as $event)
238 \ExecuteModuleEventEx($event, [$message->
getMessageId(), $compatibleFields]);
256 $compatibleFields = array_merge(
259 $this->sendingConfig->toArray(),
262 foreach (\GetModuleEvents(
'im', self::EVENT_BEFORE_NOTIFY_ADD,
true) as $arEvent)
264 $eventResult = \ExecuteModuleEventEx($arEvent, [&$compatibleFields]);
265 if ($eventResult ===
false || isset($eventResult[
'result']) && $eventResult[
'result'] ===
false)
267 $reason = $this->detectReasonSendError($chat->
getType(), $eventResult[
'reason'] ??
'');
287 $compatibleFields = array_merge(
290 $this->sendingConfig->toArray(),
293 foreach(\GetModuleEvents(
'im', self::EVENT_AFTER_NOTIFY_ADD,
true) as $event)
295 \ExecuteModuleEventEx($event, [(
int)$message->
getMessageId(), $compatibleFields]);
301 private function detectReasonSendError($type, $reason =
''): string
305 $sanitizer = new \CBXSanitizer;
306 $sanitizer->addTags([
307 'a' => [
'href',
'style',
'target'],
314 $reason = $sanitizer->sanitizeHtml($reason);
340 ?\CRestServer $server
343 if (isset($forwardMessages))
345 if (!isset($fieldsToSend[
'MESSAGE']) && !isset($fieldsToSend[
'ATTACH']))
351 $result = $this->checkMessage($fieldsToSend);
352 if(!$result->isSuccess())
356 $fieldsToSend = $result->getResult();
358 $chatData = $this->getChatData($chat, $fieldsToSend, $server);
359 $fieldsToSend = array_merge($fieldsToSend, $chatData);
361 if (isset($fieldsToSend[
'ATTACH']))
363 $result = $this->checkAttach($fieldsToSend);
364 if (!$result->isSuccess())
369 $fieldsToSend = $result->getResult();
372 if (!empty($fieldsToSend[
'KEYBOARD']))
374 $result = $this->checkKeyboard($fieldsToSend);
375 if (!$result->isSuccess())
380 $fieldsToSend = $result->getResult();
383 if (!empty($fieldsToSend[
'MENU']))
385 $result = $this->checkMenu($fieldsToSend);
386 if (!$result->isSuccess())
391 $fieldsToSend = $result->getResult();
394 if (isset($fieldsToSend[
'REPLY_ID']) && (
int)$fieldsToSend[
'REPLY_ID'] > 0)
396 $result = $this->checkReply($fieldsToSend, $chat);
397 if (!$result->isSuccess())
402 $fieldsToSend = $result->getResult();
405 $result = $this->checkParams($fieldsToSend, $server);
407 return $result->setResult($fieldsToSend);
410 private function checkMessage(array $fieldsToSend):
Result
413 if(isset($fieldsToSend[
'MESSAGE']))
415 if (!is_string($fieldsToSend[
'MESSAGE']))
420 $fieldsToSend[
'MESSAGE'] = trim($fieldsToSend[
'MESSAGE']);
422 if ($fieldsToSend[
'MESSAGE'] ===
'' && empty($arguments[
'ATTACH']))
426 "Message can't be empty"
430 elseif (!isset($fieldsToSend[
'ATTACH']))
435 return $result->setResult($fieldsToSend);
438 private function getChatData(Chat $chat, array $fieldsToSend, ?\CRestServer $server): ?array
440 $userId = $chat->getContext()->getUserId();
445 "MESSAGE_TYPE" => IM_MESSAGE_PRIVATE,
446 "FROM_USER_ID" => $userId,
447 "DIALOG_ID" => $chat->getDialogId(),
451 if (isset($fieldsToSend[
'SYSTEM'], $server) && $fieldsToSend[
'SYSTEM'] ===
'Y')
453 $fieldsToSend[
'MESSAGE'] = $this->prepareSystemMessage($server, $fieldsToSend[
'MESSAGE']);
457 'MESSAGE' => $fieldsToSend[
'MESSAGE'],
458 "MESSAGE_TYPE" => IM_MESSAGE_CHAT,
460 "DIALOG_ID" => $chat->getDialogId(),
464 private function prepareSystemMessage(\CRestServer $server,
string $message): string
466 $clientId = $server->getClientId();
473 $result = \Bitrix\Rest\AppTable::getList(
476 '=CLIENT_ID' => $clientId
481 'APP_NAME_DEFAULT' =>
'LANG_DEFAULT.MENU_NAME',
485 $result = $result->fetch();
486 $moduleName = !empty($result[
'APP_NAME'])
487 ? $result[
'APP_NAME']
488 : (!empty($result[
'APP_NAME_DEFAULT'])
489 ? $result[
'APP_NAME_DEFAULT']
494 return "[b]" . $moduleName .
"[/b]\n" . $message;
497 private function checkAttach(array $fieldsToSend): Result
499 $result =
new Result();
501 $attach = CIMMessageParamAttach::GetAttachByJson($fieldsToSend[
'ATTACH']);
504 if ($attach->IsAllowSize())
506 $fieldsToSend[
'ATTACH'] = $attach;
508 return $result->setResult($fieldsToSend);
511 return $result->addError(
new ParamError(
513 'You have exceeded the maximum allowable size of attach'
520 private function checkKeyboard(array $fieldsToSend): Result
522 $result =
new Result();
525 $keyboardField = $fieldsToSend[
'KEYBOARD'];
527 if (is_string($keyboardField))
529 $keyboardField = \CUtil::JsObjectToPhp($keyboardField);
531 if (!isset($keyboardField[
'BUTTONS']))
533 $keyboard[
'BUTTONS'] = $keyboardField;
537 $keyboard = $keyboardField;
540 $keyboard[
'BOT_ID'] = $fieldsToSend[
'BOT_ID'];
541 $keyboard = \Bitrix\Im\Bot\Keyboard::getKeyboardByJson($keyboard);
545 $fieldsToSend[
'KEYBOARD'] = $keyboard;
547 return $result->setResult($fieldsToSend);
553 private function checkMenu(array $fieldsToSend): Result
555 $result =
new Result();
558 $menuField = $fieldsToSend[
'MENU'];
560 if (is_string($menuField))
562 $menuField = \CUtil::JsObjectToPhp($menuField);
565 if (!isset($menuField[
'ITEMS']))
567 $menu[
'ITEMS'] = $menuField;
574 $menu[
'BOT_ID'] = $fieldsToSend[
'BOT_ID'];
575 $menu = \Bitrix\Im\Bot\ContextMenu::getByJson($menu);
579 $fieldsToSend[
'MENU'] = $menu;
581 return $result->setResult($fieldsToSend);
587 private function checkReply(array $fieldsToSend, Chat $chat): Result
589 $result =
new Result();
591 $message = new \Bitrix\Im\V2\Message((
int)$fieldsToSend[
'REPLY_ID']);
592 if (!$message->hasAccess())
597 if ($message->getChat()->getId() !== $chat->getId())
599 return $result->addError(
new MessageError(
601 'You can only reply to a message within the same chat')
605 $fieldsToSend[
'PARAMS'][
'REPLY_ID'] = $message->getId();
607 return $result->setResult($fieldsToSend);
610 private function checkParams(array $fieldsToSend, ?\CRestServer $server): Result
612 $result =
new Result();
613 $checkAuth = isset($server) ? $server->getAuthType() !== \Bitrix\Rest\SessionAuth\Auth::AUTH_TYPE :
true;
616 isset($fieldsToSend[
'SYSTEM']) && $fieldsToSend[
'SYSTEM'] ===
'Y'
621 $fieldsToSend[
'SYSTEM'] =
'Y';
624 if (isset($fieldsToSend[
'URL_PREVIEW']) && $fieldsToSend[
'URL_PREVIEW'] ===
'N')
626 $fieldsToSend[
'URL_PREVIEW'] =
'N';
629 if (isset($fieldsToSend[
'SKIP_CONNECTOR']) && mb_strtoupper($fieldsToSend[
'SKIP_CONNECTOR']) ===
'Y')
631 $fieldsToSend[
'SKIP_CONNECTOR'] =
'Y';
632 $fieldsToSend[
'SILENT_CONNECTOR'] =
'Y';
635 if (!empty($fieldsToSend[
'TEMPLATE_ID']))
637 $fieldsToSend[
'TEMPLATE_ID'] = mb_substr((
string)$fieldsToSend[
'TEMPLATE_ID'], 0, 255);
640 return $result->setResult($fieldsToSend);
static getMessage($code, $replace=null, $language=null)