Cl_QuizMaster / bot.py
H3y-sam's picture
Upload 12 files
d9bbfb9 verified
Raw History Blame Contribute Delete
2.55 kB
"""
bot.py β€” Local polling entry point for CLQuizMaster-Bot.
Run this for local development and testing:
python bot.py
For production on Hugging Face Spaces, run app.py instead.
"""
import logging
import os
from dotenv import load_dotenv
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from telegram.ext import Application
import db
from handlers import register_handlers
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
handlers=[
logging.StreamHandler(),
logging.FileHandler("quizbot.log", encoding="utf-8"),
],
)
logger = logging.getLogger(__name__)
def main() -> None:
token = os.getenv("TELEGRAM_TOKEN")
admin_id = int(os.getenv("ADMIN_ID", "0"))
if not token:
raise RuntimeError("TELEGRAM_TOKEN is not set. Add it to your .env file.")
if not admin_id:
raise RuntimeError("ADMIN_ID is not set. Add it to your .env file.")
# ── DB init ──────────────────────────────────────────────────────────────
db.init_db()
# ── Scheduler ────────────────────────────────────────────────────────────
scheduler = AsyncIOScheduler(timezone="Asia/Kolkata")
# ── Build Application ────────────────────────────────────────────────────
app = (
Application.builder()
.token(token)
.build()
)
# Inject shared data
app.bot_data["ADMIN_ID"] = admin_id
app.bot_data["scheduler"] = scheduler
register_handlers(app)
# ── Start scheduler alongside polling ────────────────────────────────────
async def on_startup(application: Application) -> None:
scheduler.start()
logger.info("APScheduler started (IST timezone).")
async def on_shutdown(application: Application) -> None:
scheduler.shutdown(wait=False)
logger.info("APScheduler stopped.")
app.post_init = on_startup
app.post_shutdown = on_shutdown
logger.info("Starting CLQuizMaster in POLLING mode…")
app.run_polling(drop_pending_updates=True)
if __name__ == "__main__":
main()