Python basics are done
Now we get to the actual reason you are here
Telegram Bots
But before writing a single line of bot code
You need to understand what is actually happening under the hood
Skipping this is why most beginners get confused later
This lecture covers:
Please open Telegram to view this post
VIEW IN TELEGRAM
A Telegram bot is a special account that is controlled by code instead of a human
When someone sends your bot a message — your code receives it and decides what to do
Bots can:
Bots cannot:
Please open Telegram to view this post
VIEW IN TELEGRAM
Telegram has two completely different APIs
Most beginners do not know this and get confused
Bot API:
MTProto:
We start with Bot API using aiogram
Then move to MTProto with Pyrogram and Telethon
Both are covered in this course
Please open Telegram to view this post
VIEW IN TELEGRAM
Every single thing that happens on Telegram is called an Update
A message sent — Update
A button clicked — Update
Someone joining a group — Update
A photo sent — Update
Your bot's entire job is to receive Updates and respond to them
An Update looks like this in raw JSON:
{
"update_id": 123456789,
"message": {
"message_id": 1,
"from": {
"id": 987654321,
"first_name": "Ahmed",
"username": "ahmed123"
},
"chat": {
"id": 987654321,
"type": "private"
},
"text": "/start"
}
}This is what Telegram sends your bot when someone types /start
Your code reads this and decides what to do
Libraries like aiogram handle all this for you automatically
Please open Telegram to view this post
VIEW IN TELEGRAM
There are two ways your bot can receive Updates
Polling:
Webhooks:
We use polling for now — it is perfect for learning
We switch to webhooks when we deploy
Please open Telegram to view this post
VIEW IN TELEGRAM
BotFather is the official Telegram bot for creating bots
Every bot starts here
Steps:
1. Open Telegram and search @BotFather
2. Send /newbot
3. Enter a name for your bot (example: My First Bot)
4. Enter a username — must end in "bot" (example: myfirstbot_bot)
5. BotFather gives you a token that looks like this:
1234567890:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
This token is your bot's password — never share it publicly
Anyone with this token controls your bot completely
If you accidentally leak it — go to BotFather and regenerate it immediately
Store it in a .env file — we will cover this properly next lecture
Please open Telegram to view this post
VIEW IN TELEGRAM
Before using any library — test the raw API directly
This shows you what is really happening underneath
Open your browser and visit:
https://api.telegram.org/bot<YOUR_TOKEN>/getMe
Replace <YOUR_TOKEN> with your actual token
You should see your bot's info in JSON format
Now send your bot a message on Telegram and visit:
https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates
You will see the raw Update JSON — exactly what your code will receive
This is the foundation everything is built on
Understanding this makes every library make complete sense
Please open Telegram to view this post
VIEW IN TELEGRAM
No code this time — just setup:
pip install aiogram
pip install python-dotenv
Screenshot everything and make sure aiogram installs without errors
Please open Telegram to view this post
VIEW IN TELEGRAM
Last lecture you created a bot and tested the raw API
This lecture your bot actually comes alive
It will receive messages and respond to them
We are using aiogram v3 — the most professional bot library in Python
It is fully async, modern, and what production bots are built with
This lecture covers:
Please open Telegram to view this post
VIEW IN TELEGRAM
Create a folder for your bot project with this structure:
my_bot/
├── bot.py
└── .env
Inside .env — store your token safely:
BOT_TOKEN=1234567890:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
Never put your token directly in your Python file
Never upload .env to GitHub
Create a .gitignore file and add .env to it
# .gitignore
.env
This is not optional — leaked tokens get your bot hijacked
Please open Telegram to view this post
VIEW IN TELEGRAM
Bots need to handle many users at the same time
While waiting for one user's response the bot should not freeze for everyone else
That is what async/await solves
Normal function:
def greet():
print("Hello") # runs and blocks until done
Async function:
async def greet():
print("Hello") # runs without blocking other tasks
await means "do this but let other things run while waiting":
async def handle(message):
await message.reply("Hello!") # sends reply without freezing the bot
In aiogram every handler must be async
Every Telegram API call must be awaited
You will get an error if you forget either
Please open Telegram to view this post
VIEW IN TELEGRAM
import asyncio
from aiogram import Bot, Dispatcher
from aiogram.types import Message
from aiogram.filters import Command
import os
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("BOT_TOKEN")
bot = Bot(token=TOKEN)
dp = Dispatcher()
@dp.message(Command("start"))
async def start_handler(message: Message):
await message.answer(f"Hello {message.from_user.first_name}! I am alive!")
@dp.message(Command("help"))
async def help_handler(message: Message):
await message.answer("I am a bot. Send me a message and I will echo it back.")
@dp.message()
async def echo_handler(message: Message):
await message.answer(message.text)
async def main():
await dp.start_polling(bot)
if name == "main":
asyncio.run(main())
Run this with: python bot.py
Open Telegram, send your bot /start
It responds — your bot is alive
Please open Telegram to view this post
VIEW IN TELEGRAM
load_dotenv() — loads your .env file so os.getenv() can read it
Bot(token=TOKEN) — creates the bot object that talks to Telegram
Dispatcher() — the brain of your bot, routes messages to the right handler
@dp.message(Command("start")) — a decorator
It tells the Dispatcher: when someone sends /start, run this function
message.from_user.first_name — the sender's first name
This is the OOP we covered last section — message is an object with attributes
message.answer() — sends a reply to the same chat
message.reply() — sends a reply that quotes the original message
The echo handler at the bottom has no filter
It catches every message that did not match any other handler
Order matters — put specific handlers first, catch-all last
Please open Telegram to view this post
VIEW IN TELEGRAM
Watch this for a full visual walkthrough of setting up aiogram v3
Please open Telegram to view this post
VIEW IN TELEGRAM
Get the echo bot running then extend it:
from datetime import datetime
@dp.message(Command("time"))
async def time_handler(message: Message):
now = datetime.now().strftime("%H:%M:%S")
await message.answer(f"Current time: {now}")
from aiogram.filters import F
@dp.message(F.text)
async def echo_handler(message: Message):
await message.answer(message.text)
Test all commands, screenshot the responses from your actual bot
Please open Telegram to view this post
VIEW IN TELEGRAM