๐Ÿค– Create a Telegram Bot using Python


๐Ÿ“˜ What You Will Learn

  • What is a Telegram Bot
  • How to create a bot using BotFather
  • How to write Python code for a bot
  • How to run and test your bot
  • Basic commands handling

๐Ÿง  Concept: What is a Telegram Bot?

A Telegram bot is an automated program that: - Responds to messages - Performs tasks automatically - Interacts with users without human intervention


๐Ÿ› ๏ธ Step 1: Create Bot using BotFather

  1. Open Telegram
  2. Search for BotFather
  3. Start chat and run:
/start
  1. Create a new bot:
/newbot
  1. Enter:
  2. Bot Name โ†’ StudyHelperBot
  3. Username โ†’ study_helper_bot

๐Ÿ”‘ Output

You will receive a BOT TOKEN: 123456:ABC-XYZ...

โš ๏ธ Important: Keep this token secret


๐Ÿ Step 2: Install Required Library

pip install python-telegram-bot

๐Ÿ’ป Step 3: Write Bot Code

Create a file named bot.py

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

TOKEN = "YOUR_BOT_TOKEN"

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text("Hello ๐Ÿ‘‹ I am your bot!")

app = ApplicationBuilder().token(TOKEN).build()

app.add_handler(CommandHandler("start", start))

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

Step 4: Run the Bot

python bot.py

๐Ÿงช Step 5: Test Your Bot

  • Open Telegram
  • Search your bot username
  • Send:
/start

โœ… Output:

  • Hello ๐Ÿ‘‹ I am your bot!