Beginner 2 min read

Create a Telegram Bot with Python

A Telegram bot is a program that runs inside Telegram and responds to messages automatically. You send /start, it replies. You send a question, it answers.

In this tutorial you'll build a working bot from scratch in under 10 minutes.


What You Need

  • Python installed (python --version to check)
  • Telegram app on your phone or desktop
  • Basic Python knowledge (just functions)

Step 1: Create Your Bot on Telegram

Every bot needs a token from BotFather — Telegram's official bot manager.

  1. Open Telegram and search @BotFather
  2. Send /newbot
  3. Enter a nameMy Study Bot
  4. Enter a username → must end in bot, e.g. mystudyhelper_bot

BotFather gives you a token like:

123456789:AAFxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

⚠️ Keep this token secret. Anyone with it can control your bot.


Step 2: Install the Library

pip install python-telegram-bot

Step 3: Write the Bot

Create a file bot.py:

from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes

TOKEN = "YOUR_BOT_TOKEN_HERE"

# This runs when user sends /start
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text("Hello! 👋 I am your bot. How can I help?")

# This runs when user sends /help
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text("Send /start to begin.")

app = ApplicationBuilder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("help", help_command))

print("Bot is running...")
app.run_polling()

Replace YOUR_BOT_TOKEN_HERE with the token BotFather gave you.


Step 4: Run Your Bot

python bot.py

You should see: Bot is running...


Step 5: Test It

  1. Open Telegram
  2. Search your bot username (e.g. @mystudyhelper_bot)
  3. Send /start

Your bot replies: Hello! 👋 I am your bot. How can I help?


How It Works

Part What it does
TOKEN Identifies your bot to Telegram
CommandHandler Listens for a specific command like /start
reply_text Sends a message back to the user
run_polling Keeps the bot running and checking for messages

Common Errors

ModuleNotFoundError: telegram → Run pip install python-telegram-bot again

Bot not responding → Make sure python bot.py is still running in your terminal

Invalid token → Copy the token from BotFather again — no extra spaces


Next Step

👉 Run this bot 24/7 on your OCI server so it works even when your PC is off.