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
+9 -25
View File
@@ -2,41 +2,28 @@
namespace App\Telegram;
use App\Models\User;
use App\Telegram\Handlers\ChannelCommand;
use App\Telegram\Handlers\DataCommand;
use App\Telegram\Handlers\HelpCommand;
use App\Telegram\Handlers\JoinedChatHandler;
use App\Telegram\Handlers\PingCommand;
use App\Telegram\Handlers\TransferCommand;
use App\Telegram\Middleware\AuthorizedChat;
use App\Telegram\Middleware\GroupAdminOnly;
use App\Telegram\Middleware\OfficialOnly;
use SergiX44\Nutgram\Nutgram;
class TelegramRegistrar
{
public function __construct(
private readonly TelegramBotContext $context,
) {}
/** Official system bot — includes /transfer. Does not mutate bot context. */
/** Single site bot. Official vs agent is resolved from chat_id. */
public function registerOfficial(Nutgram $bot): void
{
$this->registerShared($bot, allowTransfer: true);
}
// Announce chat_id when invited — must not use AuthorizedChat
// so unconfigured groups can still copy the id into admin settings.
$bot->onMyChatMember(JoinedChatHandler::class);
/** Agent bot — no /transfer; binds request context to this agent. */
public function registerAgent(Nutgram $bot, User $agent): void
{
$this->context->setAgent($agent);
$this->registerShared($bot, allowTransfer: false);
}
private function registerShared(Nutgram $bot, bool $allowTransfer): void
{
// HandlerGroup::middleware() unshifts — register GroupAdminOnly first so
// AuthorizedChat ends up outermost (chat allowlist before admin check).
$bot->group(function (Nutgram $bot) use ($allowTransfer) {
$bot->group(function (Nutgram $bot) {
$bot->onCommand('help', HelpCommand::class)
->description('List available commands');
@@ -53,12 +40,9 @@ class TelegramRegistrar
$bot->onCommand('channel', ChannelCommand::class)
->description('List channels; copy promo iframe (new) or support link (old)');
if ($allowTransfer) {
$bot->onCommand('transfer {args}', TransferCommand::class)
->where('args', '.+')
->middleware(OfficialOnly::class)
->description('Transfer TRX or USDT from a device address (omit amount = all)');
}
$bot->onCommand('transfer {args}', TransferCommand::class)
->where('args', '.+')
->description('Transfer TRX or USDT from a device address (omit amount = all)');
})
->middleware(GroupAdminOnly::class)
->middleware(AuthorizedChat::class);