diff --git a/telegram-bot-proxy.js b/telegram-bot-proxy.js new file mode 100644 index 0000000..6ba9f1b --- /dev/null +++ b/telegram-bot-proxy.js @@ -0,0 +1,174 @@ +// 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) { + // Clone the request to modify it + const requestClone = new Request(request); + const url = new URL(request.url); + + // If accessing the root path, show documentation + 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 = new URL( + `${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 + const telegramRequest = new Request(telegramUrl, { + method: requestClone.method, + headers: headers, + body: requestClone.method !== 'GET' ? await request.clone().arrayBuffer() : undefined, + redirect: 'follow', + }); + + try { + const response = await fetch(telegramRequest); + + // Create a new response with the Telegram API response + const newResponse = new Response(response.body, { + status: response.status, + statusText: response.statusText, + headers: response.headers, + }); + + // Add CORS headers to allow requests from any origin + newResponse.headers.set('Access-Control-Allow-Origin', '*'); + newResponse.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); + newResponse.headers.set('Access-Control-Allow-Headers', 'Content-Type'); + + return newResponse; + } catch (error) { + return new Response(`Error proxying request: ${error.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)); + } +}); \ No newline at end of file