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