-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
31 lines (24 loc) · 922 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import httpx
import uvicorn
app = FastAPI()
@app.post("/{path:path}")
async def forward_request(request: Request, path: str):
# 获取原始请求的方法、头部和数据
headers = dict(request.headers)
headers['content-type'] = 'application/json'
headers['host'] = 'api.telegram.org'
headers.pop('content-length', None)
body = await request.body()
data = body.decode('utf-8')
# 创建新的请求URL
new_url = 'https://api.telegram.org/' + path
# 使用httpx发送新的请求
if data:
response = httpx.post(new_url, headers=headers, params=data, timeout=1000)
else:
response = httpx.post(new_url, headers=headers, timeout=1000)
return JSONResponse(response.json())
if __name__ == '__main__':
uvicorn.run(app='main:app', port=8000, host="0.0.0.0", workers=5)