Skip to content

python-telegram-bot: build an echo bot with commands

python-telegram-bot is a Python wrapper around the Telegram Bot API. It handles long polling or webhooks, parses incoming updates, and routes them to handler functions you write. This post builds a small bot that answers /start and /help and echoes any other text message back.

The current release (22.8 at the time of writing) requires Python 3.10 or newer:

Terminal window
pip install "python-telegram-bot==22.*"

Bots are created through Telegram’s own BotFather bot:

  1. In Telegram, open a chat with @BotFather.
  2. Send /newbot.
  3. Choose a display name, then a username. The username must end in bot (for example my_test_bot).
  4. BotFather replies with a token. Anyone holding it can control your bot, so keep it out of source control.

The example reads the token from an environment variable:

Terminal window
export TELEGRAM_BOT_TOKEN="123456:ABC..."

Handlers are async functions that receive an Update and a context object. You register them on an Application, then start polling:

import logging
import os
from telegram import Update
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
MessageHandler,
filters,
)
logging.basicConfig(level=logging.INFO)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Hello! I am your Telegram bot.")
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Send me a message and I will echo it back to you.")
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text(update.message.text)
def main() -> None:
application = Application.builder().token(os.environ["TELEGRAM_BOT_TOKEN"]).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("help", help_command))
# Any text message that isn't a /command
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
# Blocks until Ctrl-C; handles startup and shutdown of the bot for you.
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()

Run it with python bot.py, open a chat with your bot, and send /start, /help, then any text.

If you are porting an older bot, these are the renames you will hit most:

v13v20+
Updater(token) + updater.dispatcherApplication.builder().token(token).build()
dp.add_handler(...)application.add_handler(...)
updater.start_polling() + updater.idle()application.run_polling()
Filters.text, Filters.commandfilters.TEXT, filters.COMMAND
CallbackContextContextTypes.DEFAULT_TYPE
def handler(...): update.message.reply_text(...)async def handler(...): await update.message.reply_text(...)

Every Bot API call is now a coroutine, so forgetting await is the usual mistake: the call silently does nothing and Python warns that a coroutine was never awaited.

  • Group chats and privacy mode. By default a bot in a group only sees commands and replies to its own messages. Turn privacy mode off with BotFather’s /setprivacy if the echo handler should see every message.
  • Blocking work. A slow synchronous call (a requests.get, a heavy computation) inside a handler stalls the whole bot. Use an async HTTP client or asyncio.to_thread.
  • Polling vs webhooks. run_polling() is fine for development and small bots. For a deployed bot behind HTTPS, run_webhook() avoids the constant polling connection.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)