Skip to content

Instantly share code, notes, and snippets.



Forked from ihciah/README.MD
Created Oct 9, 2019
What would you like to do?
A reverse proxy for Telegram Bot API on Aliyun Function Compute / Cloudflare Workers

A reverse proxy for Telegram Bot API on Aliyun Function Compute / Cloudflare Workers

To help users in China mainland access telegram api stably and conveniently with low cost, this script maybe the one you need.

The server-less means you don't have to run a server to proxy the requests, just pay as you go.


Edit key_prefix, set it to the prefix of you bot address(like /bot563441998:) can avoid abusing.

Tip: You should create the function in regions outside China mainland.

If you use Aliyun FC, create a function with Flask template and replace it with the If you use Cloudflare Workers, use the cf.js.

const whitelist = ["/bot563441998:"];
const tg_host = "";
addEventListener('fetch', event => {
function validate(path) {
for (var i = 0; i < whitelist.length; i++) {
if (path.startsWith(whitelist[i]))
return true;
return false;
async function handleRequest(request) {
var u = new URL(request.url); = tg_host;
if (!validate(u.pathname))
return new Response('Unauthorized', {
status: 403
var req = new Request(u, {
method: request.method,
headers: request.headers,
body: request.body
const result = await fetch(req);
return result;
#!/usr/bin/env python
# coding=utf-8
from flask import Flask
from flask import request
from flask import make_response
import requests
from urllib.parse import urlparse
from urlparse import urlparse
app = Flask(__name__)
base_path = ''
key_prefix = "/bot"
telegram_api = ""
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST'])
@app.route('/<path:path>', methods=['GET', 'POST'])
def home(path):
if not request.full_path.startswith(key_prefix):
return make_response("401 Unauthorized.", 401)
method = request.method
full_path = request.full_path
url = telegram_api + full_path
data = None if method.upper() == "GET" else request.form.to_dict()
req = requests.request(method, url, data=data)
return make_response(req.content, req.status_code)
def handler(environ, start_response):
parsed_tuple = urlparse(environ['fc.request_uri'])
li = parsed_tuple.path.split('/')
global base_path
if not base_path:
base_path = "/".join(li[0:5])
return app(environ, start_response)
import requests
data = {"chat_id":CHAT_ID, "text":"TEST"}
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"chat_id": "0000000", "text": "This is a test from curl"}' \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment