Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CopilotChat.php
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
5use Bitrix\Im;
12use Bitrix\ImBot\Bot;
15
17{
18 private const COUNTER_CHAT_CODE = 'copilot_chat_counter';
19
20 protected function getDefaultType(): string
21 {
22 return self::IM_TYPE_COPILOT;
23 }
24
25 public function getDefaultManageUsersAdd(): string
26 {
27 return self::MANAGE_RIGHTS_NONE;
28 }
29
30 public function getDefaultManageUsersDelete(): string
31 {
32 return self::MANAGE_RIGHTS_NONE;
33 }
34
35 public function getDefaultManageSettings(): string
36 {
37 return self::MANAGE_RIGHTS_NONE;
38 }
39
40 public function setCanPost(string $canPost): Chat
41 {
42 return $this;
43 }
44
45 public function setManageSettings(string $manageSettings): Chat
46 {
47 return $this;
48 }
49
50 public function setManageUI(string $manageUI): Chat
51 {
52 return $this;
53 }
54
55 public function setManageUsersAdd(string $manageUsersAdd): Chat
56 {
57 return $this;
58 }
59
60 public function setManageUsersDelete(string $manageUsersDelete): Chat
61 {
62 return $this;
63 }
64
65 public function add(array $params, ?Context $context = null): Result
66 {
67 $result = new Result();
68
69 if (!Loader::includeModule('imbot'))
70 {
71 return $result->addError(new ChatError(ChatError::IMBOT_NOT_INSTALLED));
72 }
73
75 {
76 return $result->addError(new Error(Restriction::AI_TEXT_ERROR));
77 }
78
79 $copilotBotId = static::getBotIdOrRegister();
80
81 if (!$copilotBotId)
82 {
83 return $result->addError(new Error(ChatError::COPILOT_NOT_INSTALLED));
84 }
85
86 $context ??= new Context();
87 $params['USERS'] = [$context->getUserId(), $copilotBotId];
88
89 return parent::add($params, $context);
90 }
91
92 protected function sendGreetingMessage(?int $authorId = null)
93 {
94 return;
95 }
96
97 protected function sendMessageUsersAdd(array $usersToAdd, bool $skipRecent = false): void
98 {
99 return;
100 }
101
102 protected function sendBanner(?int $authorId = null): void
103 {
104 \CIMMessage::Add([
105 'MESSAGE_TYPE' => $this->getType(),
106 'TO_CHAT_ID' => $this->getChatId(),
107 'FROM_USER_ID' => Bot\CopilotChatBot::getBotId(),
108 'MESSAGE' => Loc::getMessage('IM_CHAT_CREATE_COPILOT_WELCOME'),
109 'SKIP_USER_CHECK' => 'Y',
110 'PUSH' => 'N',
111 'SKIP_COUNTER_INCREMENTS' => 'Y',
112 'PARAMS' => [
113 Params::COMPONENT_ID => Bot\CopilotChatBot::MESSAGE_COMPONENT_START,
114 Params::NOTIFY => 'N',
115 ]
116 ]);
117 }
118
119 protected function sendDescriptionMessage(?int $authorId = null): void
120 {
121 return;
122 }
123
124 public function getDescription(): ?string
125 {
126 return null;
127 }
128
129 protected function prepareParams(array $params = []): Result
130 {
131 unset($params['TITLE']);
132
133 return parent::prepareParams($params);
134 }
135
136 public static function getTitleTemplate(): ?string
137 {
138 return Loc::getMessage('IM_CHAT_COPILOT_CHAT_TITLE');
139 }
140
141 protected function generateTitle(): string
142 {
143 $copilotChatCounter = \CUserOptions::GetOption('im', self::COUNTER_CHAT_CODE, 1, $this->getContext()->getUserId());
144 $title = Loc::getMessage('IM_CHAT_COPILOT_CHAT_TITLE', ['#NUMBER#' => $copilotChatCounter]);
145 \CUserOptions::SetOption('im', self::COUNTER_CHAT_CODE, $copilotChatCounter + 1);
146
147 return $title;
148 }
149
150 protected function addIndex(): Chat
151 {
152 return $this;
153 }
154
155 protected function updateIndex(): Chat
156 {
157 return $this;
158 }
159
160
161 protected function sendEventUsersAdd(array $usersToAdd): void
162 {
163 if (empty($usersToAdd))
164 {
165 return;
166 }
167
168 foreach ($usersToAdd as $userId)
169 {
170 $relation = $this->getRelations()->getByUserId($userId, $this->getId());
171 if ($relation === null)
172 {
173 continue;
174 }
175 if ($relation->getUser()->isBot())
176 {
177 Im\Bot::changeChatMembers($this->getId(), $userId);
178 Im\Bot::onJoinChat('chat'.$this->getId(), [
179 'CHAT_TYPE' => $this->getType(),
180 'MESSAGE_TYPE' => $this->getType(),
181 'BOT_ID' => $userId,
182 'USER_ID' => $this->getContext()->getUserId(),
183 'CHAT_ID' => $this->getId(),
184 'CHAT_AUTHOR_ID' => $this->getAuthorId(),
185 'CHAT_ENTITY_TYPE' => $this->getEntityType(),
186 'CHAT_ENTITY_ID' => $this->getEntityId(),
187 'ACCESS_HISTORY' => (int)$relation->getStartCounter() === 0,
188 'SILENT_JOIN' => 'Y', // suppress system message
189 ]);
190 }
191 }
192 }
193
194 private static function getBotIdOrRegister(): int
195 {
196 return Bot\CopilotChatBot::getBotId() ?: Bot\CopilotChatBot::register();
197 }
198
199 public static function isAvailable(): bool
200 {
201 return Loader::includeModule('imbot')
203 && static::getBotIdOrRegister();
204 }
205
206 public function toRestFormat(array $option = []): array
207 {
208 $chatData = parent::toRestFormat($option);
209
210 $providerName = Im\V2\Integration\AI\AIHelper::getProviderName();
211 $chatData['aiProvider'] = $providerName;
212
213 return $chatData;
214 }
215}
static getType($chatData)
Definition chat.php:41
sendMessageUsersAdd(array $usersToAdd, bool $skipRecent=false)
toRestFormat(array $option=[])
setManageUsersDelete(string $manageUsersDelete)
add(array $params, ?Context $context=null)
sendBanner(?int $authorId=null)
sendEventUsersAdd(array $usersToAdd)
setManageUI(string $manageUI)
setManageSettings(string $manageSettings)
prepareParams(array $params=[])
setManageUsersAdd(string $manageUsersAdd)
sendDescriptionMessage(?int $authorId=null)
sendGreetingMessage(?int $authorId=null)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29