Spaces:
Running
Running
Feat: Add Telegram Proxy API route to bypass HF firewall
Browse files
frontend/app/api/proxy/telegram/route.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
+
|
| 3 |
+
export const runtime = 'edge'; // Use Edge Runtime for speed
|
| 4 |
+
|
| 5 |
+
export async function POST(req: NextRequest) {
|
| 6 |
+
try {
|
| 7 |
+
const searchParams = req.nextUrl.searchParams;
|
| 8 |
+
const token = searchParams.get("token");
|
| 9 |
+
const method = searchParams.get("method");
|
| 10 |
+
|
| 11 |
+
if (!token || !method) {
|
| 12 |
+
return NextResponse.json(
|
| 13 |
+
{ error: "Missing 'token' or 'method' query parameters" },
|
| 14 |
+
{ status: 400 }
|
| 15 |
+
);
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
const telegramUrl = `https://api.telegram.org/bot${token}/${method}`;
|
| 19 |
+
|
| 20 |
+
// Forward the request to Telegram
|
| 21 |
+
const response = await fetch(telegramUrl, {
|
| 22 |
+
method: "POST",
|
| 23 |
+
headers: req.headers, // Pass original headers (Content-Type etc.)
|
| 24 |
+
body: req.body, // Stream the original body (JSON or FormData)
|
| 25 |
+
// @ts-ignore - duplplex needed for streaming uploads in some environments, harmless if ignored
|
| 26 |
+
duplex: 'half'
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
// Stream the response back to the client
|
| 30 |
+
return new NextResponse(response.body, {
|
| 31 |
+
status: response.status,
|
| 32 |
+
statusText: response.statusText,
|
| 33 |
+
headers: response.headers,
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
} catch (error: any) {
|
| 37 |
+
console.error("Telegram Proxy Error:", error);
|
| 38 |
+
return NextResponse.json(
|
| 39 |
+
{ error: "Proxy failed", details: error.message },
|
| 40 |
+
{ status: 500 }
|
| 41 |
+
);
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// Allow GET for simple health checks or simple methods like getMe
|
| 46 |
+
export async function GET(req: NextRequest) {
|
| 47 |
+
return POST(req);
|
| 48 |
+
}
|