13 private const CACHE_PATH =
'/bx/im/bot/new_cache_v1/';
14 private const CACHE_TTL = 31536000;
15 private const CACHE_KEY =
'bot_data_';
17 private const BOT_TYPE = [
19 'TYPE_NETWORK' =>
'N',
20 'TYPE_OPENLINE' =>
'O',
21 'TYPE_SUPERVISOR' =>
'S',
24 private static array $staticCache = [];
27 private array $botData = [];
28 private array $commands = [];
30 private function __construct()
41 if (isset(self::$staticCache[$id]))
43 return self::$staticCache[$id];
46 self::$staticCache[$id] =
new self;
47 self::$staticCache[$id]->id = $id;
48 self::$staticCache[$id]->fillBotData();
50 return self::$staticCache[$id];
53 private function fillBotData(): void
55 $botId = $this->
getId();
57 $cache = $this->getSavedCache($botId);
58 $cachedBot = $cache->getVars();
60 if ($cachedBot !==
false)
62 $this->botData = $cachedBot;
67 $botData = $this->getBotDataFromDb($botId);
68 if ($botData ===
null)
75 $this->saveInCache($cache, $botData);
77 $this->botData = $botData;
87 return $this->botData[
'CODE'] ??
'';
92 return $this->botData;
102 if (empty($this->botData))
108 $code = $this->botData[
'CODE'];
110 if ($this->botData[
'TYPE'] === self::BOT_TYPE[
'TYPE_HUMAN'])
114 else if ($this->botData[
'TYPE'] === self::BOT_TYPE[
'TYPE_NETWORK'])
118 if ($this->botData[
'CLASS'] ===
'Bitrix\ImBot\Bot\Support24')
121 $code =
'network_cloud';
123 else if ($this->botData[
'CLASS'] ===
'Bitrix\ImBot\Bot\Partner24')
126 $code =
'network_partner';
128 else if ($this->botData[
'CLASS'] ===
'Bitrix\ImBot\Bot\SupportBox')
131 $code =
'network_box';
134 else if ($this->botData[
'TYPE'] === self::BOT_TYPE[
'TYPE_OPENLINE'])
138 else if ($this->botData[
'TYPE'] === self::BOT_TYPE[
'TYPE_SUPERVISOR'])
140 $type =
'supervisor';
146 'appId' => $this->botData[
'APP_ID'],
147 'isHidden' => $this->botData[
'HIDDEN'] ===
'Y',
148 'isSupportOpenline' => $this->botData[
'OPENLINE'] ===
'Y',
154 if (empty($this->commands))
159 return $this->commands;
162 private function getSavedCache(
int $id):
Cache
166 $cacheDir = self::getCacheDir($id);
167 $cache->initCache(self::CACHE_TTL, self::getCacheKey($id), $cacheDir);
172 private function getBotDataFromDb(
int $id): ?array
174 $query = BotTable::query()
177 ->where(
'BOT_ID', $id)
180 $result = $query->fetch();
182 return $result ?:
null;
185 private function saveInCache(
Cache $cache, array $userData): void
191 private static function getCacheDir(
int $id): string
193 $cacheSubDir = substr(md5(self::getCacheKey($id)),2,2);
195 return self::CACHE_PATH .
"{$cacheSubDir}/" . self::getCacheKey($id) .
"/";
198 private static function getCacheKey($id): string
200 return self::CACHE_KEY . $id;
endDataCache($vars=false)
startDataCache($TTL=false, $uniqueString=false, $initDir=false, $vars=array(), $baseDir='cache')