Новые роуты API для статистики и мониторинга

- /api/version
- /api/health
- /api/stats
This commit is contained in:
2025-11-01 00:58:25 +08:00
parent c75da39b87
commit b5d3b60356
4 changed files with 162 additions and 84 deletions

View File

@@ -33,37 +33,35 @@ class Bot
/**
* @var BotApi Объект Telegram Bot API
*/
protected BotApi $bot;
public readonly BotApi $api;
/**
* @var Update Объект обновления бота
*/
protected Update $update;
/**
* @var ServerRequestInterface Пришедший от Telegram запрос
*/
protected ServerRequestInterface $request;
/**
* Конструктор
*
* @param ServerRequestInterface $request
* @param ServerRequestInterface|null $request
* @throws InvalidTelegramSecretException
* @throws JsonException
* @throws InvalidArgumentException
* @throws Exception
*/
public function __construct(ServerRequestInterface $request)
public function __construct(?ServerRequestInterface $request = null)
{
$this->checkSecret($request);
$body = json_decode((string)$request->getBody(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new JsonException(json_last_error_msg());
$this->api = new BotApi(config('bot.token'));
if ($request) {
$this->checkSecret($request);
$this->request = $request;
}
$this->bot = new BotApi(config('bot.token'));
$this->update = Update::fromResponse($body);
}
/**
* Запсукает обработку команды
* Запускает обработку команды
*
* @return bool
* @throws InvalidArgumentException
@@ -72,7 +70,9 @@ class Bot
*/
public function process(): bool
{
$this->parseRequestBody();
$commandText = $this->getBotCommandText();
return match (true) {
str_starts_with($commandText, '/start') => $this->processHelpCommand(),
str_starts_with($commandText, '/list') => $this->processListCommand(),
@@ -84,6 +84,22 @@ class Bot
};
}
/**
* Подготавливает объект события бота
*
* @return void
* @throws InvalidArgumentException
* @throws JsonException
*/
protected function parseRequestBody(): void
{
$body = json_decode((string)$this->request->getBody(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new JsonException(json_last_error_msg());
}
$this->update = Update::fromResponse($body);
}
/**
* Обрабатывает команду /list
*
@@ -94,7 +110,7 @@ class Bot
*/
protected function processListCommand(): bool
{
$this->bot->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$this->api->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$playlists = ini()->getPlaylists();
if (empty($playlists)) {
@@ -139,7 +155,7 @@ class Bot
*/
protected function processInfoCommand(): bool
{
$this->bot->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$this->api->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$message = $this->update->getMessage();
$text = $message->getText();
@@ -235,7 +251,7 @@ class Bot
*/
protected function processHelpCommand(): bool
{
$this->bot->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$this->api->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$replyText[] = 'Бот предоставляет короткую сводку о плейлистах, которые видны на сайте ' .
$this->escape(base_url()) . '\.';
@@ -265,7 +281,7 @@ class Bot
*/
protected function processLinksCommand(): bool
{
$this->bot->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$this->api->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$replyText[] = '*Ресурсы и страницы*';
$replyText[] = '';
@@ -288,69 +304,7 @@ class Bot
*/
protected function processStatsCommand(): bool
{
$this->bot->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$allChannels = [];
foreach (ini()->getPlaylists() as $pls) {
$allChannels = array_merge($allChannels, $pls['channels'] ?? []);
}
$onlinePls = array_filter(
ini()->getPlaylists(),
static fn (array $pls) => $pls['isOnline'] === true,
);
$offlinePls = array_filter(
ini()->getPlaylists(),
static fn (array $pls) => $pls['isOnline'] === false,
);
$unknownPls = array_filter(
ini()->getPlaylists(),
static fn (array $pls) => $pls['isOnline'] === null,
);
$adultPls = array_filter(
$onlinePls,
static fn (array $pls) => in_array('adult', $pls['tags']),
);
$catchupPls = array_filter(
$onlinePls,
static fn (array $pls) => $pls['hasCatchup'] === true,
);
$tvgPls = array_filter(
$onlinePls,
static fn (array $pls) => $pls['hasTvg'] === true,
);
$grouppedPls = array_filter(
$onlinePls,
static fn (array $pls) => count($pls['groups'] ?? []) > 0
);
$onlineCh = $offlineCh = $adultCh = [];
foreach ($onlinePls as $pls) {
$tmpOnline = array_filter(
$pls['channels'] ?? [],
static fn (array $ch) => $ch['isOnline'] === true,
);
$tmpOffline = array_filter(
$pls['channels'] ?? [],
static fn (array $ch) => $ch['isOnline'] === false,
);
$tmpAdult = array_filter(
$pls['channels'] ?? [],
static fn (array $ch) => in_array('adult', $ch['tags']),
);
$onlineCh = array_merge($onlineCh, $tmpOnline);
$offlineCh = array_merge($offlineCh, $tmpOffline);
$adultCh = array_merge($adultCh, $tmpAdult);
}
$this->api->sendChatAction($this->update->getMessage()->getChat()->getId(), 'typing');
$stats = new StatisticsService()->get();
$replyText[] = '📊 *Статистика*';
@@ -437,7 +391,7 @@ class Bot
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $keyboard = null,
): bool {
try {
$this->bot->sendMessage(
$this->api->sendMessage(
chatId: $this->update->getMessage()->getChat()->getId(),
text: $text,
parseMode: 'MarkdownV2',