From 94f4c385752f06c6aa5fcb17f1b7887619ec5d23 Mon Sep 17 00:00:00 2001 From: mtvpls Date: Tue, 30 Jun 2026 11:10:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4tgbot=E8=A7=A3=E9=99=A4?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=80=99=E9=80=89?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/admin/telegram/route.ts | 3 +- src/lib/telegram.ts | 44 ++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/app/api/admin/telegram/route.ts b/src/app/api/admin/telegram/route.ts index b70a806..cd939e6 100644 --- a/src/app/api/admin/telegram/route.ts +++ b/src/app/api/admin/telegram/route.ts @@ -4,7 +4,7 @@ import type { AdminConfig } from '@/lib/admin.types'; import { getAuthInfoFromCookie } from '@/lib/auth'; import { getConfig } from '@/lib/config'; import { getStorage } from '@/lib/db'; -import { getTelegramConfig, sendTelegramMessage, setTelegramWebhook, TelegramApiError } from '@/lib/telegram'; +import { getTelegramConfig, sendTelegramMessage, setTelegramBotCommands, setTelegramWebhook, TelegramApiError } from '@/lib/telegram'; export const runtime = 'nodejs'; @@ -80,6 +80,7 @@ export async function POST(request: NextRequest) { apiProxy, apiBaseUrl, }); + await setTelegramBotCommands(botToken, { apiProxy, apiBaseUrl }); return NextResponse.json({ success: true, message: 'Webhook 设置成功', result }); } catch (error) { if (error instanceof TelegramApiError) { diff --git a/src/lib/telegram.ts b/src/lib/telegram.ts index 5bed390..ccb7726 100644 --- a/src/lib/telegram.ts +++ b/src/lib/telegram.ts @@ -493,6 +493,30 @@ export async function setTelegramWebhook( return data?.result ?? true; } +export async function setTelegramBotCommands( + botToken: string, + config?: Partial +): Promise { + const commands: { command: string; description: string }[] = [ + { command: 'bind', description: '绑定账号' }, + { command: 'status', description: '查看绑定状态' }, + { command: 'help', description: '显示帮助' }, + { command: 'register', description: '注册并绑定账号' }, + ]; + + const response = await fetchTelegramApi( + 'setMyCommands', + botToken, + { commands }, + config + ); + + if (!response.ok) { + const errorText = await response.text().catch(() => ''); + throw new Error(`Telegram 命令设置失败: ${response.status} ${errorText}`); + } +} + export async function sendTelegramMessage( chatId: string, text: string, @@ -600,15 +624,16 @@ function parseMessageText(update: any) { } function buildTelegramHelpText(config: TelegramConfig) { - return [ + const commands: string[] = [ 'MoonTVPlus Telegram Bot 已连接。', '', '可用命令:', config.registrationEnabled ? '/register 用户名 密码 - 注册并绑定账号' : '', '/bind 绑定码 - 绑定账号', '/status - 查看状态', - '/unbind - 解除绑定', - ].filter(Boolean).join('\n'); + '/help - 显示帮助', + ]; + return commands.filter(Boolean).join('\n'); } export async function handleTelegramWebhookUpdate(update: any): Promise { @@ -665,18 +690,17 @@ export async function handleTelegramWebhookUpdate(update: any): Promise { return; } - if (/^\/unbind$/i.test(parsed.text)) { - const ok = await unbindTelegramUser(parsed.telegramUserId); - await sendTelegramMessage(parsed.chatId, ok ? '已解除 Telegram 绑定。' : '当前 Telegram 账号尚未绑定。'); - return; - } - if (/^\/status$/i.test(parsed.text)) { const binding = await getTelegramBindingByTelegramUser(parsed.telegramUserId); await sendTelegramMessage(parsed.chatId, binding ? `已绑定账号:${binding.username}\n通知:${binding.notificationsEnabled ? '开启' : '关闭'}` : '当前 Telegram 账号尚未绑定。'); return; } + if (/^\/help$/i.test(parsed.text)) { + await sendTelegramMessage(parsed.chatId, buildTelegramHelpText(await getTelegramConfig())); + return; + } + await sendTelegramMessage(parsed.chatId, buildTelegramHelpText(await getTelegramConfig())); return; } @@ -687,7 +711,7 @@ export async function handleTelegramWebhookUpdate(update: any): Promise { const data = String(callback.data); const confirmMatch = data.match(/^tg_login_confirm:(.+)$/); const denyMatch = data.match(/^tg_login_deny:(.+)$/); - + if (confirmMatch) { try { await confirmTelegramLogin(confirmMatch[1], telegramUserId);