• Модуль: imbot
  • Путь к файлу: ~/bitrix/modules/imbot/lib/dialogsession.php
  • Класс: BitrixImBotDialogSession
  • Вызов: DialogSession::start
public function start(array $params = []): bool
{
	$this->init($params);
	if (empty($this->botId) || empty($this->dialogId))
	{
		return false;
	}

	$newData = [
		'DATE_LAST_ACTIVITY' => new DateTime,
		'DATE_FINISH' => null,
	];

	if (!empty($params['GREETING_SHOWN']))
	{
		$newData['GREETING_SHOWN'] = $params['GREETING_SHOWN'];
	}
	if (array_key_exists('MENU_STATE', $params))
	{
		if (is_array($params['MENU_STATE']))
		{
			$newData['MENU_STATE'] = MainWebJson::encode($params['MENU_STATE']);
		}
		else
		{
			$newData['MENU_STATE'] = $params['MENU_STATE'];
		}
	}

	if (!empty($params['SESSION_ID']))
	{
		$newData['SESSION_ID'] = (int)$params['SESSION_ID'];
	}
	elseif (!empty($this->sessionId))
	{
		$newData['SESSION_ID'] = $this->sessionId;
	}

	if (!empty($params['CLOSE_TERM']))
	{
		$newData['CLOSE_TERM'] = (int)$params['CLOSE_TERM'];
	}
	elseif (!empty($this->closeTerm))
	{
		$newData['CLOSE_TERM'] = $this->closeTerm;
	}

	if ($this->primaryId)
	{
		$filter = ['ID' => $this->primaryId];
	}
	else
	{
		$filter = $this->initFilter();
	}
	$res = NetworkSessionTable::getList([
		'select' => [
			'ID',
			'BOT_ID',
			'DIALOG_ID',
			'SESSION_ID',
			'GREETING_SHOWN',
			'MENU_STATE',
			'DATE_CREATE',
			'DATE_FINISH',
			'DATE_LAST_ACTIVITY',
			'CLOSE_TERM',
			'CLOSED',
			'TELEMETRY_SENT',
		],
		'filter' => $filter
	]);
	if ($sessData = $res->fetch())
	{
		$this->primaryId = (int)$sessData['ID'];
		foreach ($newData as $field => $value)
		{
			if ($sessData[$field] === $newData[$field])
			{
				unset($newData[$field]);
			}
		}
		$this->update($newData);
	}
	else
	{
		$newData['BOT_ID'] = $this->botId;
		$newData['DIALOG_ID'] = $this->dialogId;

		$res = NetworkSessionTable::add($newData);
		if ($res->isSuccess())
		{
			$this->primaryId = (int)$res->getId();
		}
	}

	return true;
}