Spaces:
Running

axeronis's picture
Feat: Add Telegram Proxy API route to bypass HF firewall
e154c63
Raw History Blame Contribute Delete
1.64 kB
import { NextRequest, NextResponse } from "next/server";
export const runtime = 'edge'; // Use Edge Runtime for speed
export async function POST(req: NextRequest) {
try {
const searchParams = req.nextUrl.searchParams;
const token = searchParams.get("token");
const method = searchParams.get("method");
if (!token || !method) {
return NextResponse.json(
{ error: "Missing 'token' or 'method' query parameters" },
{ status: 400 }
);
}
const telegramUrl = `https://api.telegram.org/bot${token}/${method}`;
// Forward the request to Telegram
const response = await fetch(telegramUrl, {
method: "POST",
headers: req.headers, // Pass original headers (Content-Type etc.)
body: req.body, // Stream the original body (JSON or FormData)
// @ts-ignore - duplplex needed for streaming uploads in some environments, harmless if ignored
duplex: 'half'
});
// Stream the response back to the client
return new NextResponse(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
} catch (error: any) {
console.error("Telegram Proxy Error:", error);
return NextResponse.json(
{ error: "Proxy failed", details: error.message },
{ status: 500 }
);
}
}
// Allow GET for simple health checks or simple methods like getMe
export async function GET(req: NextRequest) {
return POST(req);
}