Skip to content

Latest commit

Β 

History

30 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎯 Quizbot

A production-grade Telegram quiz platform β€” create, manage, and run interactive quizzes at scale.

Python Pyrogram PTB MongoDB License Docker


Originally developed by devgagan Β β€’Β  Sponsored by Qzio


πŸ€– Try the Live Bot β†’ @advance_quiz_bot


πŸ“‹ Table of Contents


✨ Features

Category Capability
Quiz Creation Text input, forwarded Telegram quiz polls, file/PDF import, AI-generated quizzes
Quiz Formats Standard, sectional (per-section timers), practice & exam modes
Smart Filtering Strips [1/100]-style progress tags, usernames, links, and custom word lists from imported polls
Editing Shuffle questions, retitle, adjust timers, add/remove questions
Access Control Free and paid quiz tiers, batch access, auth-chat lists, optional premium gate
Analytics Per-user performance, leaderboards, sectional score breakdowns
HTML Reports Self-contained interactive HTML scorecards β€” question navigator, KaTeX/Markdown rendering, dark/light theme
Mini App Visual in-Telegram quiz player (practice + exam mode) as a Telegram WebApp
Inline Sharing Share any quiz by ID via inline query, with a working Play button
Payments Razorpay-backed premium plans
Broadcast Send announcements to all users (owner only)

πŸ— Architecture

quizbot/
β”œβ”€β”€ database/            Shared async MongoDB layer
β”‚   β”œβ”€β”€ db.py             Motor connection manager + automatic index setup
β”‚   └── repositories.py   One repository class per domain (users, quizzes, payments, ...)
β”‚
β”œβ”€β”€ shared/               Code shared by both bots
β”‚   β”œβ”€β”€ config.py          All configuration & secrets, loaded from .env
β”‚   β”œβ”€β”€ utils/             Text cleanup, premium checks, async file I/O
β”‚   └── html/              Quiz-report HTML generator (exam UI + analysis)
β”‚
β”œβ”€β”€ creator_bot/          Pyrogram bot β€” quiz creation, editing, batches, payments
β”‚   β”œβ”€β”€ bot.py             Client setup + run_creator_bot()
β”‚   └── handlers/          One module per feature area
β”‚
β”œβ”€β”€ runner_bot/           python-telegram-bot bot β€” playing quizzes, AI generation
β”‚   β”œβ”€β”€ bot.py             Application setup + run_runner_bot()
β”‚   └── handlers/          One module per feature area
β”‚
└── mini_app/             FastAPI Mini App β€” the visual "Play" quiz player
    β”œβ”€β”€ telegram_auth.py   Verifies Telegram WebApp initData (HMAC-SHA256)
    β”œβ”€β”€ player_service.py  Play-session state, scoring, DB persistence
    β”œβ”€β”€ routes.py          FastAPI app + /api/* endpoints
    └── static/index.html  Single-file frontend (practice + exam mode UI)

run.py                  Combined launcher β€” starts both bots (+ Mini App, if configured)
requirements.txt
Procfile                 Heroku process declaration (single web dyno)
Dockerfile / docker-compose.yml
.env.example             Environment variable template

Everything runs from one process (run.py) by default, sharing a single async MongoDB database:

  • Creator Bot (Pyrogram) handles quiz creation, editing, imports, batches, and payments.
  • Runner Bot (python-telegram-bot) handles quiz sessions β€” sending polls, tracking answers, building leaderboards.
  • Mini App (FastAPI, optional) serves a visual in-Telegram quiz player when a public domain is configured.

πŸ”§ Prerequisites

Requirement Minimum Version Notes
Python 3.11+
MongoDB Atlas free tier (M0) or self-hosted MONGODB_URI in .env
Telegram API credentials β€” From my.telegram.org
Two Telegram bot tokens β€” same token for both runner and creator u can keep seperate too

⚑ Quick Start

python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt

cp .env.example .env
nano .env          # fill in your values β€” see Configuration Reference below

python run.py

The database and all its indexes are created automatically on first connect β€” no manual schema step needed.


βš™οΈ Configuration Reference

All values live in .env (copy from .env.example). Note: you must not expose these values in the repo you fork directly otherwise you may loose your bot, data (securely fill these vars and secret in the environment of the platform you are using)

Variable Required Description
API_ID / API_HASH βœ… Telegram API credentials from my.telegram.org
CREATOR_BOT_TOKEN βœ… Token for the Pyrogram bot (creation, editing, payments)
RUNNER_BOT_TOKEN βœ… Token for the PTB bot (playing, scheduling, AI generation)
MONGODB_URI βœ… MongoDB Atlas connection string
MONGODB_DB_NAME βœ… Database name (default: quizbot)
OWNER_ID βœ… Your Telegram user ID
ADMIN_IDS βž– Space-separated additional admin user IDs
LOG_GROUP βž– Negative chat ID for error/log channel
BOT_GROUP βž– Main community group ID
CHANNEL_ID βž– Announcement channel ID
REQUIRED_SUB_CHANNEL βž– Channel users must join to use /start, /create, /myquizzes, /add
FREE_BOT βž– true to treat every user as premium
RAZORPAY_KEY_ID / RAZORPAY_KEY_SECRET βž– Leave blank to disable the /pay premium-purchase flow
PDF_API_BASE βž– Optional external PDF-generation microservice for /testseries
MINI_APP_DOMAIN βž– Public HTTPS URL for the Mini App β€” leave blank to disable it entirely
MINI_APP_HOST / MINI_APP_PORT βž– Local bind address behind your reverse proxy (default 0.0.0.0:8080)
OPENROUTER_DEFAULT_KEYS βž– Comma-separated fallback AI provider keys

Rate limits, session timeouts, and other tuning knobs have sensible defaults β€” see the comments in .env.example for the full list.


πŸš€ Running the Platform

Run Everything

python run.py

Starts both bots, and the Mini App server too if MINI_APP_DOMAIN is set.

Run One Component Only

python run.py --only creator   # Creator Bot only
python run.py --only runner    # Runner Bot only
python run.py --only miniapp   # Mini App server only

systemd (VPS, always-on)

sudo nano /etc/systemd/system/quizbot.service
[Unit]
Description=Quizbot Platform
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/opt/quizbot
ExecStart=/opt/quizbot/.venv/bin/python run.py
Restart=always
RestartSec=10
EnvironmentFile=/opt/quizbot/.env
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now quizbot
sudo journalctl -u quizbot -f      # live logs

🐳 Docker Deployment

docker compose up -d --build

This starts the Creator Bot, Runner Bot, and (if MINI_APP_DOMAIN is set) the Mini App as separate containers, each connecting out to the same MongoDB Atlas cluster via MONGODB_URI β€” no local volume needed, since nothing is stored on the container's own filesystem.

To skip the Mini App entirely:

docker compose up -d --build creator-bot runner-bot

Heroku / other PaaS: a single Procfile (web: python run.py) runs the whole platform from one dyno/process β€” set the same .env variables as Config Vars.


πŸ“± Mini App β€” the Visual Quiz Player

A "Play" button (opened as a Telegram WebApp) appears after quiz creation and on inline-share cards, offering two modes:

  • Practice mode β€” instant correct/incorrect feedback with the explanation shown right after each answer, then auto-advance.
  • Exam mode β€” no answers revealed until the end, followed by a full top-to-bottom review of every question, your answer, the correct answer, and the explanation.

It's strictly a player β€” no creation or editing happens here, and it enforces the same access rules as both bots (free/paid quizzes, batch access, auth-chat lists, optional premium gate).

Telegram requires a public HTTPS URL for WebApp buttons, so put a reverse proxy or tunnel (nginx, Caddy, Cloudflare Tunnel, etc.) in front of the FastAPI server and set MINI_APP_DOMAIN accordingly. Leave it blank to disable the feature entirely β€” no Play buttons are shown, and the server doesn't start.

Identity comes solely from Telegram's own initData, verified server-side via HMAC-SHA256 on every request. Quiz content in every API response is AES-256-GCM encrypted with a per-session key, and the correct answer is never present in a question's payload before it's answered.


πŸ—„ Database

Data lives in MongoDB Atlas β€” a free M0 cluster is enough to get started (see Quick Start). quizbot/database/db.py connects via Motor and creates every required index automatically on first connect, so there's no manual schema step. Both bots read and write through repository classes in quizbot/database/repositories.py β€” there's no separate API layer to keep in sync.


πŸ™ Credits

Role
Originally developed by devgagan
Sponsored by Qzio β€” The Smart Quiz Platform
Telegram Libraries Pyrogram & python-telegram-bot
Database MongoDB Atlas

Built for educators, exam aspirants, and quiz creators.

About

Feature rich Quizbot for telegram, an alternative to official telegram quizbot.

Topics

Resources

Stars

21 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages