I build Telegram bots regularly. Every time, I rebuild the same boilerplate: command handlers, inline keyboards, database, rate limiting.
So I made a template. Now I clone it and start adding features in 5 minutes.
python-telegram-bot-template — open source, MIT.
What's Inside
- Async bot built on python-telegram-bot v21+
- Command handlers (/start, /help, custom)
- Inline keyboard with callbacks
- SQLite database for user tracking
- Rate limiting middleware
- Docker support
- Environment-based config
Quick Start
git clone https://github.com/spinov001-art/python-telegram-bot-template.git
cd python-telegram-bot-template
pip install -r requirements.txt
cp .env.example .env
# Add your BOT_TOKEN from @BotFather
python bot.py
How to Get a Bot Token
- Open Telegram, find @botfather
- Send
/newbot - Choose a name and username
- Copy the token into
.env
Adding a Custom Command
Edit handlers/commands.py:
async def weather_command(update, context):
city = " ".join(context.args) if context.args else "London"
weather = await fetch_weather(city) # your API call
await update.message.reply_text(f"Weather in {city}: {weather}")
Register in bot.py:
app.add_handler(CommandHandler("weather", weather_command))
Done. Users can now type /weather London and get results.
Adding Inline Keyboards
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
async def menu(update, context):
keyboard = [
[InlineKeyboardButton("Option A", callback_data="a")],
[InlineKeyboardButton("Option B", callback_data="b")],
]
await update.message.reply_text(
"Choose:", reply_markup=InlineKeyboardMarkup(keyboard)
)
Deployment
# Docker
docker build -t my-bot .
docker run -d --env-file .env my-bot
# Or Railway/Render — just push to GitHub and deploy
The Full Template
Clone it, customize it, ship it:
github.com/spinov001-art/python-telegram-bot-template
What bots have you built? Share in the comments — I love seeing creative bot ideas.
More templates: Python Web Scraper | GitHub Actions
Top comments (0)