File size: 1,619 Bytes
9d382e5 | 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import os
import sys
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from db import init_db
from bot.handlers import handle_update
from bot.bale_api import send_message
from web.routes import router as web_router
app = FastAPI(title="MultiBot")
app.include_router(web_router)
@app.on_event("startup")
async def startup():
await init_db()
print("✅ Bot database initialized")
@app.get("/health")
async def health():
return {"status": "ok"}
@app.post("/webhook/bot")
async def bale_webhook(update: dict):
try:
await handle_update(update)
except Exception as e:
print(f"❌ Error handling update: {e}")
return JSONResponse({"ok": True})
@app.get("/set-webhook")
async def set_webhook():
from bot.bale_api import BASE
import httpx
wh_url = os.getenv("WEBHOOK_URL", "")
if not wh_url:
return JSONResponse({"ok": False, "error": "WEBHOOK_URL not set"})
webhook_endpoint = f"{wh_url.rstrip('/')}/webhook/bot"
async with httpx.AsyncClient() as client:
r = await client.post(f"{BASE}/setWebhook", json={"url": webhook_endpoint})
return r.json()
@app.get("/delete-webhook")
async def delete_webhook():
from bot.bale_api import BASE
import httpx
async with httpx.AsyncClient() as client:
r = await client.post(f"{BASE}/deleteWebhook")
return r.json()
if __name__ == "__main__":
port = int(os.getenv("PORT", 8000))
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=False)
|