import os import threading import uvicorn from fastapi import FastAPI from fastapi.responses import HTMLResponse from bot import main as start_bot # Define lifespan to manage startup/shutdown from contextlib import asynccontextmanager @asynccontextmanager async def lifespan(app: FastAPI): # Startup: Run bot in background thread print("Starting Telegram Bot in background thread...") bot_thread = threading.Thread(target=start_bot, daemon=True) bot_thread.start() yield # Shutdown logic (if any) app = FastAPI(lifespan=lifespan) @app.get("/", response_class=HTMLResponse) def home(): return """ AI Quiz Bot Status

✅ AI Quiz Bot is Running

The bot is active and polling for updates.

""" if __name__ == "__main__": port = int(os.getenv("PORT", 7860)) uvicorn.run(app, host="0.0.0.0", port=port)