Learn Coding
57 subscribers
21 links
Download Telegram
This media is not supported in your browser
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:
➑️ Project structure and .env setup
➑️ Setting up Bot and Dispatcher
➑️ Your first message handler
➑️ Running the bot with polling
➑️ Understanding async/await in bots
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“Œ Project Setup

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
πŸ“Œ Understanding async/await

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
πŸ“Œ Your First Bot β€” Full Code

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
πŸ“Œ Breaking Down the Code

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
🚨 Video Reference

Watch this for a full visual walkthrough of setting up aiogram v3

➑️ aiogram 3 beginner series β€” full playlist
➑️ Official aiogram 3 docs

πŸ”– Watch: Aiogram Basics: Build Your First Telegram Bot (15:00) + Lesson 1 β€” First Handlers (23:39)
Please open Telegram to view this post
VIEW IN TELEGRAM
✏️ Lecture 14 Homework

Get the echo bot running then extend it:

➑️ Add a /about command that tells the user what your bot does
➑️ Add a /time command that sends the current time

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}")


➑️ Make the echo handler only respond to text messages:

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

⚠️ Next lecture drops in 2 days β€” Commands & Handlers
Please open Telegram to view this post
VIEW IN TELEGRAM
This media is not supported in your browser
VIEW IN TELEGRAM