DEV Community

Alex Spinov
Alex Spinov

Posted on

Build a Telegram Bot in Python in 5 Minutes (Template Included)

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
Enter fullscreen mode Exit fullscreen mode

How to Get a Bot Token

  1. Open Telegram, find @botfather
  2. Send /newbot
  3. Choose a name and username
  4. 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}")
Enter fullscreen mode Exit fullscreen mode

Register in bot.py:

app.add_handler(CommandHandler("weather", weather_command))
Enter fullscreen mode Exit fullscreen mode

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)
    )
Enter fullscreen mode Exit fullscreen mode

Deployment

# Docker
docker build -t my-bot .
docker run -d --env-file .env my-bot

# Or Railway/Render — just push to GitHub and deploy
Enter fullscreen mode Exit fullscreen mode

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)