// Telegram Bot API base URL const TELEGRAM_API_BASE = 'https://api.telegram.org'; // HTML template for documentation const DOC_HTML = ` Telegram Bot API Proxy Documentation

Telegram Bot API Proxy

This service acts as a transparent proxy for the Telegram Bot API. It allows you to bypass network restrictions and create middleware for your Telegram bot applications.

How to Use

Replace api.telegram.org with this worker's URL in your API calls.

Example Usage:

Original Telegram API URL:

https://api.telegram.org/bot{YOUR_BOT_TOKEN}/sendMessage

Using this proxy:

https://{YOUR_WORKER_URL}/bot{YOUR_BOT_TOKEN}/sendMessage

Features

Note: This proxy does not store or modify your bot tokens. All requests are forwarded directly to Telegram's API servers.

Example Code

// JavaScript Example fetch('https://{YOUR_WORKER_URL}/bot{YOUR_BOT_TOKEN}/sendMessage', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ chat_id: "123456789", text: "Hello from Telegram Bot API Proxy!" }) }) .then(response => response.json()) .then(data => console.log(data));
`; async function handleRequest(request) { const url = new URL(request.url); if (url.pathname === '/' || url.pathname === '') { return new Response(DOC_HTML, { headers: { 'Content-Type': 'text/html;charset=UTF-8', 'Cache-Control': 'public, max-age=3600', }, }); } // Extract the bot token and method from the URL path // Expected format: /bot{token}/{method} const pathParts = url.pathname.split('/').filter(Boolean); if (pathParts.length < 2 || !pathParts[0].startsWith('bot')) { return new Response('Invalid bot request format', { status: 400 }); } // Reconstruct the Telegram API URL const telegramUrl = `${TELEGRAM_API_BASE}${url.pathname}${url.search}`; // Create headers for the new request const headers = new Headers(request.headers); // Forward the request to Telegram API let body = undefined; if (request.method !== 'GET' && request.method !== 'HEAD') { try { body = await request.arrayBuffer(); } catch (err) { return new Response(`Failed to read request body: ${err.message}`, { status: 400 }); } } const proxyReq = new Request(telegramUrl, { method: request.method, headers: request.headers, body, redirect: 'follow', }); try { const tgRes = await fetch(proxyReq); const res = new Response(tgRes.body, tgRes); // Copy response as-is res.headers.set('Access-Control-Allow-Origin', '*'); res.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); res.headers.set('Access-Control-Allow-Headers', 'Content-Type'); return res; } catch (err) { return new Response(`Error proxying request: ${err.message}`, { status: 500 }); } } // Handle OPTIONS requests for CORS function handleOptions(request) { const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Max-Age': '86400', }; return new Response(null, { status: 204, headers: corsHeaders, }); } // Main event listener for the worker addEventListener('fetch', event => { const request = event.request; // Handle CORS preflight requests if (request.method === 'OPTIONS') { event.respondWith(handleOptions(request)); } else { event.respondWith(handleRequest(request)); } });