1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Chat.php
См. документацию.
1<?php
2declare(strict_types=1);
3
4namespace Bitrix\Landing\Copilot\Connector\Chat;
5
6use Bitrix\Main\Loader;
7use Bitrix\Main\Security;
8use Bitrix\Landing;
9use Bitrix\Landing\Copilot\Model\SiteToChatTable;
10
11class Chat
12{
13 private int $userId;
14
15 public function __construct()
16 {
17 $this->userId = Landing\Manager::getUserId();
18 }
19
24 public function setUserId(int $userId): self
25 {
26 if ($userId > 0)
27 {
28 $this->userId = $userId;
29 }
30
31 return $this;
32 }
33
34 public static function getCreateSiteChatBot(): ?ICopilotChatBot
35 {
36 return self::getChatBot(CreateSiteChatBot::class);
37 }
38
39 public static function getChangeBlockChatBot(): ?ICopilotChatBot
40 {
41 return self::getChatBot(ChangeBlockChatBot::class);
42 }
43
49 private static function getChatBot(string $botClass): ?ICopilotChatBot
50 {
51 if (!Loader::includeModule('ai'))
52 {
53 return null;
54 }
55
56 static $chatBots = [];
57 if (!isset($chatBots[$botClass]))
58 {
62 $chatBot = new $botClass();
63 $chatBot?->register();
64 $chatBots[$botClass] = $chatBot;
65 }
66
67 return $chatBots[$botClass];
68 }
69
74 public static function createChatEntityId(): string
75 {
76 return 'site_copilot_' . Security\Random::getString(6);
77 }
78
79 public function setChatForSite(int $siteId, int $chatId): bool
80 {
81 $exists = SiteToChatTable::query()
82 ->where('SITE_ID', $siteId)
83 ->where('CHAT_ID', $chatId)
84 ->where('USER_ID', $this->userId)
85 ->fetch()
86 ;
87 if ($exists)
88 {
89 return false;
90 }
91
92 $add = SiteToChatTable::add([
93 'SITE_ID' => $siteId,
94 'CHAT_ID' => $chatId,
95 'USER_ID' => $this->userId,
96 ]);
97
98 return $add->isSuccess();
99 }
100
101 public function getChatForSite(int $siteId): ?int
102 {
103 $exists = SiteToChatTable::query()
104 ->setSelect(['CHAT_ID'])
105 ->where('SITE_ID', $siteId)
106 ->where('USER_ID', $this->userId)
107 ->fetch()
108 ;
109 if ($exists)
110 {
111 return (int)$exists['CHAT_ID'];
112 }
113
114 return null;
115 }
116}
static getChangeBlockChatBot()
Определения Chat.php:39
getChatForSite(int $siteId)
Определения Chat.php:101
static createChatEntityId()
Определения Chat.php:74
static getCreateSiteChatBot()
Определения Chat.php:34
setChatForSite(int $siteId, int $chatId)
Определения Chat.php:79
setUserId(int $userId)
Определения Chat.php:24
static getUserId()
Определения manager.php:107
static getString($length, $caseSensitive=false)
Определения random.php:76
$siteId
Определения ajax.php:8
Определения binding.php:2