Skip to content

Instantly share code, notes, and snippets.

@ihciah ihciah/README.MD
Last active Jun 9, 2019

Embed
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.

Usage

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 flask.py. If you use Cloudflare Workers, use the cf.js.

const whitelist = ["/bot563441998:"];
const tg_host = "api.telegram.org";
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
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);
u.host = 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
try:
from urllib.parse import urlparse
except:
from urlparse import urlparse
app = Flask(__name__)
base_path = ''
key_prefix = "/bot"
telegram_api = "https://api.telegram.org"
@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
TG_TOKEN = ""
#TG_URL = "https://api.telegram.org/bot%s/sendMessage" % TG_TOKEN
TG_URL = " https://xxxxxx.cn-hongkong.fc.aliyuncs.com/xxxxx/proxy/xxxx/xxxx/bot%s/sendMessage" % TG_TOKEN
CHAT_ID = 0
data = {"chat_id":CHAT_ID, "text":"TEST"}
print(requests.post(TG_URL,data).content)
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"chat_id": "0000000", "text": "This is a test from curl"}' \
https://xxx.xxxx.workers.dev/bot233333:AAAAAAAAAAAAAAAAAAA/sendMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.