接入tgbot
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import {
|
||||
createTelegramBindSession,
|
||||
getTelegramBinding,
|
||||
getTelegramConfig,
|
||||
getTelegramDeepLink,
|
||||
} from '@/lib/telegram';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo?.username) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const config = await getTelegramConfig();
|
||||
const binding = await getTelegramBinding(authInfo.username);
|
||||
return NextResponse.json({
|
||||
enabled: config.enabled && config.bindingEnabled && Boolean(config.botToken),
|
||||
botUsername: config.botUsername,
|
||||
binding,
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo?.username) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const config = await getTelegramConfig();
|
||||
if (!config.enabled || !config.bindingEnabled || !config.botToken) {
|
||||
return NextResponse.json({ error: 'Telegram Bot 未启用' }, { status: 400 });
|
||||
}
|
||||
|
||||
const session = await createTelegramBindSession(authInfo.username);
|
||||
return NextResponse.json({
|
||||
code: session.code,
|
||||
expiresAt: session.expiresAt,
|
||||
botUsername: config.botUsername,
|
||||
deepLink: config.botUsername ? getTelegramDeepLink(config.botUsername, `bind_${session.code}`) : '',
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
import { getTelegramConfig } from '@/lib/telegram';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET() {
|
||||
const config = await getTelegramConfig();
|
||||
return NextResponse.json({
|
||||
enabled: config.enabled && Boolean(config.botToken),
|
||||
botUsername: config.botUsername,
|
||||
loginEnabled: config.loginEnabled,
|
||||
bindingEnabled: config.bindingEnabled,
|
||||
notificationsEnabled: config.notificationsEnabled,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
import {
|
||||
createTelegramLoginSession,
|
||||
getTelegramConfig,
|
||||
getTelegramDeepLink,
|
||||
} from '@/lib/telegram';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function POST() {
|
||||
const config = await getTelegramConfig();
|
||||
if (!config.enabled || !config.loginEnabled || !config.botToken || !config.botUsername) {
|
||||
return NextResponse.json({ error: 'Telegram 登录未启用' }, { status: 400 });
|
||||
}
|
||||
|
||||
const session = await createTelegramLoginSession();
|
||||
return NextResponse.json({
|
||||
token: session.token,
|
||||
expiresAt: session.expiresAt,
|
||||
botUsername: config.botUsername,
|
||||
deepLink: getTelegramDeepLink(config.botUsername, `login_${session.token}`),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import {
|
||||
consumeConfirmedTelegramLogin,
|
||||
getTelegramLoginSession,
|
||||
} from '@/lib/telegram';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const token = new URL(request.url).searchParams.get('token');
|
||||
const session = await getTelegramLoginSession(token);
|
||||
if (!session) return NextResponse.json({ status: 'expired' });
|
||||
|
||||
if (session.status === 'confirmed' && session.authToken) {
|
||||
const consumed = await consumeConfirmedTelegramLogin(session.token);
|
||||
const response = NextResponse.json({ status: 'confirmed', username: consumed?.username });
|
||||
const expires = new Date();
|
||||
expires.setDate(expires.getDate() + 60);
|
||||
response.cookies.set('auth', session.authToken, {
|
||||
path: '/',
|
||||
expires,
|
||||
sameSite: 'lax',
|
||||
httpOnly: false,
|
||||
secure: false,
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
status: session.status,
|
||||
expiresAt: session.expiresAt,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import {
|
||||
handleTelegramWebhookUpdate,
|
||||
validateTelegramWebhookRequest,
|
||||
} from '@/lib/telegram';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { secret: string } }
|
||||
) {
|
||||
if (!(await validateTelegramWebhookRequest(request, params.secret))) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const update = await request.json();
|
||||
await handleTelegramWebhookUpdate(update);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
Reference in New Issue
Block a user