FVG-Delta-Crypto-Core / telegram_proxy_worker.js
abyXsmart's picture
Upload 19 files
687f23f verified
Raw History Blame
5.16 kB
// ===========================================================================
// FVG-Delta β€” Telegram reverse proxy for Deno Deploy (free, no card needed)
// ===========================================================================
//
// WHY THIS EXISTS
// ---------------
// Hugging Face Spaces blackhole outbound traffic to api.telegram.org (anti-spam
// / anti-bot policy), so the engine's alerts silently never arrive. This tiny
// worker runs on Deno Deploy (a free, globally-distributed edge runtime) and
// simply forwards every request it receives to the real Telegram Bot API. The
// engine points TELEGRAM_API_BASE at this worker's URL, and delivery works
// again β€” your bot token only ever transits your own worker.
//
// WHAT IT DOES
// ------------
// Whatever path the engine calls, e.g. /bot<token>/sendMessage
// is forwarded verbatim to https://api.telegram.org/bot<token>/sendMessage
// Method, headers, query string and body (GET form-encoded OR multipart photo
// uploads) are all passed straight through, and Telegram's JSON reply is
// returned unchanged. No data is stored or logged.
//
// DEPLOY (5 minutes, browser only β€” no install, no folders, phone-friendly)
// ------------------------------------------------------------------------
// 1. Go to https://dash.deno.com and sign in (GitHub / Google β€” free, no card).
// 2. Click "New Playground".
// 3. Delete the sample code, paste THIS ENTIRE FILE in, click "Save & Deploy".
// 4. Copy the project URL shown at the top, e.g. https://your-name-123.deno.dev
// 5. In the engine's Settings page β†’ "Telegram API Base URL", paste that URL
// (no trailing slash) and Save. Click "Test Telegram" β€” you should get the
// test message. Done.
//
// Deno Deploy's free tier allows ~100k requests/day β€” far more than a 24/7
// alert bot needs. If you ever want to rotate it, just redeploy and update the
// URL in Settings; no engine restart required.
// ===========================================================================
const TELEGRAM_ORIGIN = "https://api.telegram.org";
Deno.serve(async (req) => {
const incoming = new URL(req.url);
// Health check: a plain GET / returns a small OK so you can eyeball it in a
// browser without leaking anything.
if (incoming.pathname === "/" || incoming.pathname === "") {
return new Response("FVG-Delta Telegram proxy is live.", {
status: 200,
headers: { "content-type": "text/plain; charset=utf-8" },
});
}
// Rebuild the target URL on the Telegram origin, preserving the full path
// (/bot<token>/<method>) and any query string.
const target = TELEGRAM_ORIGIN + incoming.pathname + incoming.search;
// Forward the request. We rebuild headers so the inbound Host (….deno.dev) is NOT
// leaked upstream β€” Telegram must see its own Host or it can reject the request β€” and
// we drop hop-by-hop headers that must not be proxied. Both
// application/x-www-form-urlencoded (sendMessage) and multipart/form-data (sendPhoto)
// pass straight through.
const fwd = new Headers(req.headers);
fwd.delete("host"); // never leak ….deno.dev upstream
fwd.delete("connection");
fwd.delete("content-length"); // fetch recomputes it from the buffered body below
// NOTE: do NOT set a "host" header here β€” fetch derives Host from the target URL, and
// a manually-forced host is a forbidden header on edge runtimes (ignored or rejected).
const hasBody = !(req.method === "GET" || req.method === "HEAD");
// CRITICAL FIX (v7.4.1): BUFFER the body instead of streaming it.
// The previous version forwarded `req.body` as a live ReadableStream with
// `duplex:'half'`. On Deno Deploy that streaming path could truncate the small
// urlencoded sendMessage body before it reached Telegram β€” Telegram then read a
// mangled chat_id and replied 400 "chat not found", so NO text (and intermittently
// no photo) was delivered. Reading the whole body into an ArrayBuffer first lets
// fetch send it with a correct Content-Length, byte-for-byte. This preserves both
// application/x-www-form-urlencoded (sendMessage) and multipart/form-data
// (sendPhoto) exactly, and removes the need for duplex streaming entirely.
const bodyBuf = hasBody ? await req.arrayBuffer() : undefined;
const init = {
method: req.method,
headers: fwd,
body: bodyBuf,
};
try {
const upstream = await fetch(target, init);
// Pass Telegram's status + body straight back to the engine, stripping hop-by-hop
// headers so the edge runtime doesn't choke on a re-declared transfer-encoding.
const outHeaders = new Headers(upstream.headers);
outHeaders.delete("transfer-encoding");
outHeaders.delete("connection");
outHeaders.delete("content-encoding");
return new Response(upstream.body, {
status: upstream.status,
headers: outHeaders,
});
} catch (err) {
return new Response(
JSON.stringify({ ok: false, error_code: 502, description: "proxy_fetch_failed: " + String(err) }),
{ status: 502, headers: { "content-type": "application/json" } },
);
}
});