| 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 | |
| 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) | |
| def home(): | |
| return """ | |
| <html> | |
| <head> | |
| <title>AI Quiz Bot Status</title> | |
| <style> | |
| body { font-family: sans-serif; text-align: center; padding-top: 50px; background-color: #f0f2f5; } | |
| .card { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); display: inline-block; } | |
| h1 { color: #2e7d32; } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="card"> | |
| <h1>✅ AI Quiz Bot is Running</h1> | |
| <p>The bot is active and polling for updates.</p> | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| if __name__ == "__main__": | |
| port = int(os.getenv("PORT", 7860)) | |
| uvicorn.run(app, host="0.0.0.0", port=port) | |