main.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from fastapi import FastAPI, HTTPException
  2. import subprocess
  3. app = FastAPI()
  4. # Gotify 服务器的 URL
  5. GOTIFY_URL = "https://gotify.erhe.top"
  6. @app.get("/webhook")
  7. async def receive_checkchan_webhook(key: str, title: str, message: str):
  8. if not key:
  9. raise HTTPException(status_code=400, detail="Missing required parameter 'key'")
  10. if key not in ['A_SgWPLrgFAXm0d', 'A9KF--mx_12PjSu', 'A0Xg6ZE5946iBYg']:
  11. raise HTTPException(status_code=401, detail="Invalid API key")
  12. try:
  13. # 构建命令行参数
  14. command = [
  15. 'curl',
  16. '-k', # 允许不安全的 HTTPS 请求
  17. f'{GOTIFY_URL}/message?token={key}',
  18. '-F', f'title={title}',
  19. '-F', f'message={message}',
  20. '-F', 'priority=5'
  21. ]
  22. # 执行命令
  23. result = subprocess.run(command, check=True, text=True, capture_output=True)
  24. print('命令执行成功,输出:')
  25. print(result.stdout)
  26. if result.returncode == 0:
  27. return {"status": "success", "message": "Message sent to Gotify"}
  28. else:
  29. raise HTTPException(
  30. status_code=500, detail="Failed to send message to Gotify")
  31. except subprocess.CalledProcessError as e:
  32. print('命令执行失败:')
  33. print(e.stderr)
  34. raise HTTPException(
  35. status_code=500, detail="Failed to send message to Gotify")
  36. except Exception as e:
  37. raise HTTPException(status_code=500, detail=str(e))
  38. # 运行 Uvicorn 服务器
  39. if __name__ == "__main__":
  40. import uvicorn
  41. uvicorn.run(app, host="0.0.0.0", port=29981)