aiogram

3.27.0 · active · verified Fri Apr 10

aiogram is a modern and fully asynchronous framework for the Telegram Bot API, built on `asyncio`. It provides a high-level, opinionated API for creating Telegram bots efficiently. The library closely follows Telegram Bot API updates, releasing new versions frequently to incorporate the latest features and changes. The current stable version is 3.27.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic 'echo' bot using `aiogram v3`. It initializes a `Bot` and `Dispatcher`, defines handlers for the `/start` command and any text message, and then starts polling for updates. Remember to replace 'YOUR_BOT_TOKEN_HERE' or set the `TELEGRAM_BOT_TOKEN` environment variable.

import asyncio
import os
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from aiogram.filters import CommandStart

BOT_TOKEN = os.environ.get('TELEGRAM_BOT_TOKEN', 'YOUR_BOT_TOKEN_HERE') # Replace or set env var

async def main():
    bot = Bot(token=BOT_TOKEN)
    dp = Dispatcher()

    @dp.message(CommandStart())
    async def handle_start(message: Message):
        await message.answer(f"Hello, {message.from_user.full_name}!")

    @dp.message(F.text == "hi")
    async def handle_hi(message: Message):
        await message.answer("Hi there!")

    @dp.message(F.text)
    async def handle_text(message: Message):
        await message.answer(f"You said: {message.text}")

    await dp.start_polling(bot)

if __name__ == "__main__":
    if BOT_TOKEN == 'YOUR_BOT_TOKEN_HERE':
        print("Please replace 'YOUR_BOT_TOKEN_HERE' with your actual bot token or set the TELEGRAM_BOT_TOKEN environment variable.")
    else:
        asyncio.run(main())

view raw JSON →