• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/integration/im/chat.php
  • Класс: Bitrix\Crm\Integration\Im\Chat
  • Вызов: Chat::createChat
static function createChat($params = [])
{
	if (!\Bitrix\Main\Loader::includeModule('im'))
	{
		return false;
	}

	$entityType = $params['ENTITY_TYPE'];
	$entityTypeId = \CCrmOwnerType::ResolveID($entityType);
	$entityId = (int)$params['ENTITY_ID'];
	$userId = (int)$params['USER_ID'];

	if (empty($entityType) || empty($entityId) || empty($userId))
	{
		return false;
	}

	$enablePermissionCheck = isset($params['ENABLE_PERMISSION_CHECK'])
		? (bool)$params['ENABLE_PERMISSION_CHECK'] : true;
	if ($enablePermissionCheck && !self::checkPermission($entityTypeId, $entityId, $userId))
	{
		return false;
	}

	$crmEntityTitle = '';
	$crmEntityAvatarId = 0;

	$entityData = self::getEntityData($entityType, $entityId, true);
	if ($entityType == \CCrmOwnerType::CompanyName)
	{
		if (isset($entityData['TITLE']))
		{
			$crmEntityTitle = $entityData['TITLE'];
		}
		if (isset($entityData['LOGO']))
		{
			$crmEntityAvatarId = intval($entityData['LOGO']);
		}
	}
	else if ($entityType == \CCrmOwnerType::ContactName)
	{
		if (isset($entityData['FULL_NAME']))
		{
			$crmEntityTitle = $entityData['FULL_NAME'];
		}
		if (isset($entityData['PHOTO']))
		{
			$crmEntityAvatarId = intval($entityData['PHOTO']);
		}
	}
	else
	{
		if (isset($entityData['TITLE']))
		{
			$crmEntityTitle = $entityData['TITLE'];
		}
	}

	if (!$crmEntityTitle)
	{
		$crmEntityTitle = '#'.$entityId;
	}

	$authorId = (int)$entityData['ASSIGNED_BY_ID'];
	if($authorId <= 0)
	{
		$authorId = $userId;
	}
	$joinUserList = array_unique(array_merge([ $userId ], self::getEntityUserIDs($entityTypeId, $entityId)));

	$chatFields = array(
		'TITLE' => self::buildChatName([
			'ENTITY_TYPE' => $entityType,
			'ENTITY_TITLE' => $crmEntityTitle,
		]),
		'TYPE' => IM_MESSAGE_CHAT,
		'ENTITY_TYPE' => self::CHAT_ENTITY_TYPE,
		'ENTITY_ID' => $entityType.'|'.$entityId,
		'SKIP_ADD_MESSAGE' => 'Y',
		'AUTHOR_ID' => $authorId,
		'USERS' => $joinUserList
	);
	if ($crmEntityAvatarId)
	{
		$chatFields['AVATAR_ID'] = $crmEntityAvatarId;
	}

	$chat = new \CIMChat(0);
	$chatId = $chat->add($chatFields);

	$users = [];
	foreach ($joinUserList as $uid)
	{
		$users[$uid] = \Bitrix\Im\User::getInstance($uid)->getArray(['JSON' => 'Y']);
	}

	if (Main\Loader::includeModule('pull'))
	{
		$tag = Crm\Timeline\TimelineEntry::prepareEntityPushTag($entityTypeId, $entityId);
		\CPullWatch::AddToStack(
			$tag,
			array(
				'module_id' => 'crm',
				'command' => 'timeline_chat_create',
				'params' => array(
					'CHAT_DATA' => array('CHAT_ID' => $chatId, 'USER_INFOS' => $users),
					'TAG' => $tag
				),
			)
		);
	}

	// first message in chat, if you delete this message, need set SKIP_ADD_MESSAGE = N in creating chat props
	\CIMChat::AddMessage([
		"TO_CHAT_ID" => $chatId,
		"USER_ID" => $userId,
		"MESSAGE" => static::getChatCardTitle($entityType),
		"SYSTEM" => 'Y',
		"ATTACH" => self::getEntityCard($entityType, $entityId, $entityData)
	]);

	return $chatId;
}