Instantly share code, notes, and snippets.

@ihciah /README.MD
Last active Jan 29, 2019

Embed
What would you like to do?
A reverse proxy for Telegram Bot API on Aliyun Function Compute

A reverse proxy for Telegram Bot API on Aliyun Function Compute

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 can avoid abusing.

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

#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment