A modular, production-ready Telegram bot template built with Python. Designed to be a solid starting point for bots that need structured logging, access control, time-based restrictions, and clean project organization.
- 🔐 User authorization — only whitelisted Telegram IDs can access protected commands
- 🕐 Time-based restrictions — commands can be automatically blocked outside business hours
- 📋 Structured logging — separate log files for debug, info, warnings, and errors
- 📦 Modular architecture — each concern (auth, logging, alerts) is isolated in its own module
- 🔒 Secure by default — sensitive data loaded via
.env, never hardcoded
├── main.py # Entry point — bot setup and command registration
├── BlockBot/
│ └── main.py # Time-based access control logic
├── Logs/
│ └── main.py # Logger factory (debug, info, warning, error)
├── Utils/
│ ├── alert.py # Console alert utility (info, warning, critical)
│ └── errorRaiser.py # Centralized error handling helpers
├── .env.example # Environment variable template
├── .gitignore
├── requirements.txt
└── README.md
- Python 3.11+
- A Telegram bot token (see Creating a bot below)
git clone https://github.com/chEfInHO0/Telegram-Bot-Example.git
cd Telegram-Bot-Examplepython -m venv venvWindows:
.\venv\Scripts\activateLinux/macOS:
source venv/bin/activatepip install -r requirements.txtcp .env.example .envEdit .env with your values:
TOKEN=your_telegram_bot_token_here
BOT_USERNAME=@your_bot_username
AUTH_GRANTED=[123456789, 987654321]
⚠️ Never commit your.envfile. It is already listed in.gitignore.
python main.py| Command | Description | Authorization required |
|---|---|---|
/start |
Greets the user | No |
/ping |
Health check — responds with Pong |
No |
/test |
Basic test command | No |
User authorization is handled by checking the sender's Telegram ID against the AUTH_GRANTED list defined in .env.
To find your Telegram user ID, you can use @userinfobot.
The BlockBot module allows commands to be automatically disabled after a configured time. By default, protected commands are blocked after 17:30.
# BlockBot/main.py
def blockbot():
limit = datetime(YEAR, MONTH, DAY, hour=17, minute=30)
return datetime.today() > limitTo change the cutoff time, edit the HOUR and MINUTE constants in BlockBot/main.py.
The Logs module provides a logger factory that creates isolated loggers, each writing to its own file:
| Logger | File | Level |
|---|---|---|
debugger.logger |
debug.log |
DEBUG |
command_logger |
commands.log |
INFO |
warning_logger |
warnings.log |
WARNING |
error_logger |
errors.log |
ERROR |
Log files are stored inside the Logs/ directory and are excluded from version control via .gitignore.
The Alert utility provides colored console output for startup warnings:
from Utils.alert import Alert
Alert("Check your .env file before running", "warning").display()
Alert("TOKEN is missing!", "critical").display()To add a new command:
# 1. Define the handler in main.py
async def hello_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hello!")
# 2. Register it in the main() function
app.add_handler(CommandHandler('hello', hello_command))| Package | Version | Purpose |
|---|---|---|
python-telegram-bot |
21.5 | Telegram Bot API wrapper |
python-dotenv |
1.0.1 | .env file loading |
httpx |
0.27.2 | Async HTTP client |
- Open Telegram and search for @BotFather
- Send
/newbotand follow the instructions - Copy the token provided and paste it into your
.envfile asTOKEN
This project is licensed under the MIT License. See LICENSE for details.