aiogram¶
aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python 3.8+ using asyncio and aiohttp.
Make your bots faster and more powerful!
Features¶
Asynchronous (asyncio docs, PEP 492)
Supports PyPy
Supports Telegram Bot API 7.10 and gets fast updates to the latest versions of the Bot API
Telegram Bot API integration code was autogenerated and can be easily re-generated when API gets updated
Updates router (Blueprints)
Has Finite State Machine
Uses powerful magic filters
Middlewares (incoming updates and API calls)
Provides Replies into Webhook
Integrated I18n/L10n support with GNU Gettext (or Fluent)
Warning
It is strongly advised that you have prior experience working with asyncio before beginning to use aiogram.
If you have any questions, you can visit our community chats on Telegram:
🇺🇸 @aiogram
🇺🇦 @aiogramua
🇺🇿 @aiogram_uz
🇰🇿 @aiogram_kz
🇷🇺 @aiogram_ru
🇮🇷 @aiogram_fa
🇮🇹 @aiogram_it
🇧🇷 @aiogram_br
Simple usage¶
import asyncio
import logging
import sys
from os import getenv
from aiogram import Bot, Dispatcher, html
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.types import Message
# Bot token can be obtained via https://t.me/BotFather
TOKEN = getenv("BOT_TOKEN")
# All handlers should be attached to the Router (or Dispatcher)
dp = Dispatcher()
@dp.message(CommandStart())
async def command_start_handler(message: Message) -> None:
"""
This handler receives messages with `/start` command
"""
# Most event objects have aliases for API methods that can be called in events' context
# For example if you want to answer to incoming message you can use `message.answer(...)` alias
# and the target chat will be passed to :ref:`aiogram.methods.send_message.SendMessage`
# method automatically or call API method directly via
# Bot instance: `bot.send_message(chat_id=message.chat.id, ...)`
await message.answer(f"Hello, {html.bold(message.from_user.full_name)}!")
@dp.message()
async def echo_handler(message: Message) -> None:
"""
Handler will forward receive a message back to the sender
By default, message handler will handle all message types (like a text, photo, sticker etc.)
"""
try:
# Send a copy of the received message
await message.send_copy(chat_id=message.chat.id)
except TypeError:
# But not all the types is supported to be copied so need to handle it
await message.answer("Nice try!")
async def main() -> None:
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# And the run events dispatching
await dp.start_polling(bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())
Usage without dispatcher¶
Just only interact with Bot API, without handling events
import asyncio
from argparse import ArgumentParser
from aiogram import Bot
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
def create_parser() -> ArgumentParser:
parser = ArgumentParser()
parser.add_argument("--token", help="Telegram Bot API Token")
parser.add_argument("--chat-id", type=int, help="Target chat id")
parser.add_argument("--message", "-m", help="Message text to sent", default="Hello, World!")
return parser
async def main():
parser = create_parser()
ns = parser.parse_args()
token = ns.token
chat_id = ns.chat_id
message = ns.message
async with Bot(
token=token,
default=DefaultBotProperties(
parse_mode=ParseMode.HTML,
),
) as bot:
await bot.send_message(chat_id=chat_id, text=message)
if __name__ == "__main__":
asyncio.run(main())
Contents¶
- Installation
- Migration FAQ (2.x -> 3.0)
- Bot API
- Bot
- Client session
- Types
- Methods
- Enums
- BotCommandScopeType
- ChatAction
- ChatBoostSourceType
- ChatMemberStatus
- ChatType
- ContentType
- Currency
- DiceEmoji
- EncryptedPassportElement
- InlineQueryResultType
- InputMediaType
- InputPaidMediaType
- KeyboardButtonPollTypeType
- MaskPositionPoint
- MenuButtonType
- MessageEntityType
- MessageOriginType
- PaidMediaType
- ParseMode
- PassportElementErrorType
- PollType
- ReactionTypeType
- RevenueWithdrawalStateType
- StickerFormat
- StickerType
- TopicIconColor
- TransactionPartnerType
- UpdateType
- How to download file?
- How to upload file?
- Global defaults
- Handling events
- Utils
- Changelog
- 3.13.1 (2024-09-18)
- 3.13.0 (2024-09-08)
- 3.12.0 (2024-08-16)
- 3.11.0 (2024-08-09)
- 3.10.0 (2024-07-07)
- 3.9.0 (2024-07-06)
- 3.8.0 (2024-06-19)
- 3.7.0 (2024-05-31)
- 3.6.0 (2024-05-06)
- 3.5.0 (2024-04-23)
- 3.4.1 (2024-02-17)
- 3.4.0 (2024-02-16)
- 3.3.0 (2023-12-31)
- 3.2.0 (2023-11-24)
- 3.1.1 (2023-09-25)
- 3.1.0 (2023-09-22)
- 3.0.0 (2023-09-01)
- 3.0.0rc2 (2023-08-18)
- 3.0.0rc1 (2023-08-06)
- 3.0.0b9 (2023-07-30)
- 3.0.0b8 (2023-07-17)
- 3.0.0b7 (2023-02-18)
- 3.0.0b6 (2022-11-18)
- 3.0.0b5 (2022-10-02)
- 3.0.0b4 (2022-08-14)
- 3.0.0b3 (2022-04-19)
- 3.0.0b2 (2022-02-19)
- 3.0.0b1 (2021-12-12)
- 3.0.0a18 (2021-11-10)
- 3.0.0a17 (2021-09-24)
- 3.0.0a16 (2021-09-22)
- 3.0.0a15 (2021-09-10)
- 3.0.0a14 (2021-08-17)
- 2.14.3 (2021-07-21)
- 2.14.2 (2021-07-26)
- 2.14 (2021-07-27)
- 2.13 (2021-04-28)
- 2.12.1 (2021-03-22)
- 2.12 (2021-03-14)
- 2.11.2 (2021-11-10)
- 2.11.1 (2021-11-10)
- 2.11 (2021-11-08)
- 2.10.1 (2021-09-14)
- 2.10 (2021-09-13)
- 2.9.2 (2021-06-13)
- 2.9 (2021-06-08)
- 2.8 (2021-04-26)
- 2.7 (2021-04-07)
- 2.6.1 (2021-01-25)
- 2.6 (2021-01-23)
- 2.5.3 (2021-01-05)
- 2.5.2 (2021-01-01)
- 2.5.1 (2021-01-01)
- 2.5 (2021-01-01)
- 2.4 (2021-10-29)
- 2.3 (2021-08-16)
- 2.2 (2021-06-09)
- 2.1 (2021-04-18)
- 2.0.1 (2021-12-31)
- 2.0 (2021-10-28)
- 1.4 (2021-08-03)
- 1.3.3 (2021-07-16)
- 1.3.2 (2021-05-27)
- 1.3.1 (2018-05-27)
- 1.3 (2021-04-22)
- 1.2.3 (2018-04-14)
- 1.2.2 (2018-04-08)
- 1.2.1 (2018-03-25)
- 1.2 (2018-02-23)
- 1.1 (2018-01-27)
- 1.0.4 (2018-01-10)
- 1.0.3 (2018-01-07)
- 1.0.2 (2017-11-29)
- 1.0.1 (2017-11-21)
- 1.0 (2017-11-19)
- 0.4.1 (2017-08-03)
- 0.4 (2017-08-05)
- 0.3.4 (2017-08-04)
- 0.3.3 (2017-07-05)
- 0.3.2 (2017-07-04)
- 0.3.1 (2017-07-04)
- 0.2b1 (2017-06-00)
- 0.1 (2017-06-03)
- Contributing