init
This commit is contained in:
46
main.py
Normal file
46
main.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
from fastapi import FastAPI, Request, HTTPException
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
# 配置日志
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
# 发送消息到Telegram群组的函数
|
||||||
|
def send_tg(message: str):
|
||||||
|
base_url = "https://api.telegram.org/bot"
|
||||||
|
token = "558659722:AAENA3v8gKq7R7_nsr8V58Iu-_Z6BWx2SCw"
|
||||||
|
chat_id = "-861577609"
|
||||||
|
text = message
|
||||||
|
url = f"{base_url}{token}/sendMessage?chat_id={chat_id}&text={text}"
|
||||||
|
try:
|
||||||
|
r = requests.get(url)
|
||||||
|
if r.status_code != 200:
|
||||||
|
logging.error(f"Error: {r.status_code}, {r.text}")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"请检查网络代理: {e}")
|
||||||
|
|
||||||
|
# POST请求接口
|
||||||
|
@app.post("/send_message")
|
||||||
|
async def send_message(request: Request):
|
||||||
|
try:
|
||||||
|
data = await request.json()
|
||||||
|
logging.info(f"Received data: {data}")
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logging.error("Invalid JSON")
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid JSON")
|
||||||
|
|
||||||
|
message = data.get("message")
|
||||||
|
if not message:
|
||||||
|
logging.error("Message not provided")
|
||||||
|
raise HTTPException(status_code=400, detail="Message not provided")
|
||||||
|
|
||||||
|
send_tg(message)
|
||||||
|
return {"status": "success", "message": "Message sent"}
|
||||||
|
|
||||||
|
# 运行应用程序
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import uvicorn
|
||||||
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||||
Reference in New Issue
Block a user