feat: bot

This commit is contained in:
hashbro
2026-08-15 23:05:51 +08:00
parent 9049fdccad
commit 376b442880
26 changed files with 944 additions and 352 deletions
+4 -14
View File
@@ -8,8 +8,8 @@ use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Properties\ChatType;
/**
* Allow only the configured owner group/supergroup for the current bot context
* (official system chat or the active agent's chat_id).
* Allow only the official owner chat or an enabled agent's chat_id.
* Binds TelegramBotContext so later handlers know official vs agent.
*/
class AuthorizedChat
{
@@ -19,21 +19,11 @@ class AuthorizedChat
public function __invoke(Nutgram $bot, callable $next): mixed
{
$expected = $this->context->expectedChatId();
if ($expected === '') {
Log::warning('telegram AuthorizedChat: chat id empty', [
'agent_id' => $this->context->agent()?->id,
]);
return null;
}
$chatId = $bot->chatId();
if ($chatId === null || (string) $chatId !== $expected) {
if (! $this->context->bindFromChatId($chatId)) {
Log::info('telegram AuthorizedChat: chat rejected', [
'chat_id' => $chatId,
'expected' => $expected,
'agent_id' => $this->context->agent()?->id,
'expected_official' => trim((string) config('coruna.telegram.owner_chat_id', '')),
]);
return null;
@@ -1,21 +0,0 @@
<?php
namespace App\Telegram\Middleware;
use App\Telegram\TelegramBotContext;
use SergiX44\Nutgram\Nutgram;
/** Blocks /transfer (and similar) on agent bots. */
class OfficialBotOnly
{
public function __invoke(Nutgram $bot, callable $next): mixed
{
if (! app(TelegramBotContext::class)->isOfficial()) {
$bot->sendMessage('⛔ /transfer 仅限官方系统群使用');
return null;
}
return $next($bot);
}
}
-25
View File
@@ -1,25 +0,0 @@
<?php
namespace App\Telegram\Middleware;
use App\Telegram\TelegramBotContext;
use SergiX44\Nutgram\Nutgram;
/** Blocks /transfer (and similar) on agent bots. */
class OfficialOnly
{
public function __construct(
private readonly TelegramBotContext $context,
) {}
public function __invoke(Nutgram $bot, callable $next): mixed
{
if (! $this->context->isOfficial()) {
$bot->sendMessage('⛔ /transfer 仅限官方机器人使用');
return null;
}
return $next($bot);
}
}