GoToChat::sendAction

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. GoToChat
  4. sendAction
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/controller/activity/gotochat.php
  • Класс: Bitrix\Crm\Controller\Activity\GoToChat
  • Вызов: GoToChat::sendAction
public function sendAction(int $ownerTypeId, int $ownerId, array $params): Result
{
	$result = new Result();

	if (!\Bitrix\Crm\Integration\ImOpenLines\GoToChat::isActive())
	{
		$result->addError(new Error('Feature is not available'));

		return $result;
	}

	$result = $this->validateEntity($ownerTypeId, $ownerId);
	if (!$result->isSuccess())
	{
		return $result;
	}

	$senderType = (string)($params['senderType'] ?? '');
	$senderChannelId = (string)($params['senderId'] ?? '');
	$from = (string)($params['from'] ?? '');
	$to = (int)($params['to'] ?? '');
	$lineId = (string)($params['lineId'] ?? '');

	if (!\Bitrix\Crm\Integration\ImOpenLines\GoToChat::isValidLineId($lineId))
	{
		$this->addError(new Error(Loc::getMessage('CRM_INVITATION_WRONG_LINE')));

		return $result;
	}

	$channel =
		\Bitrix\Crm\MessageSender\Channel\ChannelRepository::create(new ItemIdentifier($ownerTypeId, $ownerId))
			->getById($senderType, $senderChannelId)
	;
	if (!$channel)
	{
		$result->addError(new Error(Loc::getMessage('CRM_INVITATION_CHANNEL_NOT_FOUND')));

		return $result;
	}

	$checkChannelResult = $this->checkChannel($channel);
	if (!$checkChannelResult->isSuccess())
	{
		$result->addErrors($checkChannelResult->getErrors());

		return $result;
	}

	$fromCorrespondent = null;
	$toCorrespondent = null;

	foreach ($channel->getFromList() as $fromListItem)
	{
		if ($fromListItem->getId() === $from)
		{
			$fromCorrespondent = $fromListItem;
			break;
		}
	}

	if (!$fromCorrespondent)
	{
		$result->addError(new Error(Loc::getMessage('CRM_INVITATION_WRONG_FROM')));

		return $result;
	}

	foreach ($channel->getToList() as $toListItem)
	{
		if ($toListItem->getAddress()->getId() === $to)
		{
			$toCorrespondent = $toListItem;
			break;
		}
	}

	if (!$toCorrespondent)
	{
		$result->addError(new Error(Loc::getMessage('CRM_INVITATION_WRONG_TO')));

		return $result;
	}

	$result = (new \Bitrix\Crm\Integration\ImOpenLines\GoToChat())->send(
		$channel,
		$lineId,
		$fromCorrespondent,
		$toCorrespondent
	);

	if (!$result->isSuccess())
	{
		$this->addErrors($result->getErrors());
	}

	return $result;
}

Добавить комментарий