移除tgbot解除绑定,增加候选指令功能
This commit is contained in:
@@ -4,7 +4,7 @@ import type { AdminConfig } from '@/lib/admin.types';
|
|||||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||||
import { getConfig } from '@/lib/config';
|
import { getConfig } from '@/lib/config';
|
||||||
import { getStorage } from '@/lib/db';
|
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';
|
export const runtime = 'nodejs';
|
||||||
|
|
||||||
@@ -80,6 +80,7 @@ export async function POST(request: NextRequest) {
|
|||||||
apiProxy,
|
apiProxy,
|
||||||
apiBaseUrl,
|
apiBaseUrl,
|
||||||
});
|
});
|
||||||
|
await setTelegramBotCommands(botToken, { apiProxy, apiBaseUrl });
|
||||||
return NextResponse.json({ success: true, message: 'Webhook 设置成功', result });
|
return NextResponse.json({ success: true, message: 'Webhook 设置成功', result });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof TelegramApiError) {
|
if (error instanceof TelegramApiError) {
|
||||||
|
|||||||
+33
-9
@@ -493,6 +493,30 @@ export async function setTelegramWebhook(
|
|||||||
return data?.result ?? true;
|
return data?.result ?? true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function setTelegramBotCommands(
|
||||||
|
botToken: string,
|
||||||
|
config?: Partial<TelegramConfig>
|
||||||
|
): Promise<void> {
|
||||||
|
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(
|
export async function sendTelegramMessage(
|
||||||
chatId: string,
|
chatId: string,
|
||||||
text: string,
|
text: string,
|
||||||
@@ -600,15 +624,16 @@ function parseMessageText(update: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function buildTelegramHelpText(config: TelegramConfig) {
|
function buildTelegramHelpText(config: TelegramConfig) {
|
||||||
return [
|
const commands: string[] = [
|
||||||
'MoonTVPlus Telegram Bot 已连接。',
|
'MoonTVPlus Telegram Bot 已连接。',
|
||||||
'',
|
'',
|
||||||
'可用命令:',
|
'可用命令:',
|
||||||
config.registrationEnabled ? '/register 用户名 密码 - 注册并绑定账号' : '',
|
config.registrationEnabled ? '/register 用户名 密码 - 注册并绑定账号' : '',
|
||||||
'/bind 绑定码 - 绑定账号',
|
'/bind 绑定码 - 绑定账号',
|
||||||
'/status - 查看状态',
|
'/status - 查看状态',
|
||||||
'/unbind - 解除绑定',
|
'/help - 显示帮助',
|
||||||
].filter(Boolean).join('\n');
|
];
|
||||||
|
return commands.filter(Boolean).join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handleTelegramWebhookUpdate(update: any): Promise<void> {
|
export async function handleTelegramWebhookUpdate(update: any): Promise<void> {
|
||||||
@@ -665,18 +690,17 @@ export async function handleTelegramWebhookUpdate(update: any): Promise<void> {
|
|||||||
return;
|
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)) {
|
if (/^\/status$/i.test(parsed.text)) {
|
||||||
const binding = await getTelegramBindingByTelegramUser(parsed.telegramUserId);
|
const binding = await getTelegramBindingByTelegramUser(parsed.telegramUserId);
|
||||||
await sendTelegramMessage(parsed.chatId, binding ? `已绑定账号:${binding.username}\n通知:${binding.notificationsEnabled ? '开启' : '关闭'}` : '当前 Telegram 账号尚未绑定。');
|
await sendTelegramMessage(parsed.chatId, binding ? `已绑定账号:${binding.username}\n通知:${binding.notificationsEnabled ? '开启' : '关闭'}` : '当前 Telegram 账号尚未绑定。');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (/^\/help$/i.test(parsed.text)) {
|
||||||
|
await sendTelegramMessage(parsed.chatId, buildTelegramHelpText(await getTelegramConfig()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await sendTelegramMessage(parsed.chatId, buildTelegramHelpText(await getTelegramConfig()));
|
await sendTelegramMessage(parsed.chatId, buildTelegramHelpText(await getTelegramConfig()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user