EnergyVenom's picture
Patchwise Agent Studio: Oracle AI Agent Studio cycles
af1bee9 verified
Raw History Blame Contribute Delete
2.17 kB
// Patchwise Telegram relay (Cloudflare Worker).
//
// Some hosts (Hugging Face Spaces among them) block connections to api.telegram.org. This Worker forwards Patchwise's
// Telegram Bot API calls from Cloudflare instead: set FPT_TELEGRAM_API_BASE to this Worker's https address.
//
// It forwards only the bot methods Patchwise uses, only for the one bot whose id is configured, logs nothing, and keeps
// nothing. The bot token still travels in the path (as Telegram requires), so treat the Worker's address like any HTTPS
// endpoint: it sees what it forwards.
//
// Settings (Worker → Settings → Variables):
// BOT_ID the number before the colon in the bot token (e.g. 9172594614): calls for any other bot are refused.
const ALLOWED_METHODS = new Set(['getMe', 'getUpdates', 'sendMessage', 'editMessageText', 'editMessageReplyMarkup', 'answerCallbackQuery', 'deleteWebhook']);
export default {
async fetch(request, env) {
const url = new URL(request.url);
// /bot<id>:<secret>/<method>
const m = /^\/bot(\d+):([A-Za-z0-9_-]+)\/([A-Za-z]+)$/.exec(url.pathname);
if (!m) return new Response('Not found', { status: 404 });
const [, botId, , method] = m;
if (!env.BOT_ID || botId !== String(env.BOT_ID)) return new Response('Forbidden', { status: 403 });
if (!ALLOWED_METHODS.has(method)) return new Response('Method not allowed', { status: 405 });
if (request.method !== 'GET' && request.method !== 'POST') return new Response('Method not allowed', { status: 405 });
const upstream = new URL(`https://api.telegram.org${url.pathname}`);
upstream.search = url.search;
const init = { method: request.method, headers: { 'Content-Type': request.headers.get('Content-Type') || 'application/json' } };
if (request.method === 'POST') init.body = await request.arrayBuffer();
const r = await fetch(upstream.toString(), init);
// pass Telegram's answer through unchanged (status and JSON body); no caching
return new Response(r.body, { status: r.status, headers: { 'Content-Type': r.headers.get('Content-Type') || 'application/json', 'Cache-Control': 'no-store' } });
},
};