aiogram是Github上一个现代且完全异步的 Telegram Bot API 框架,使用 asyncio 和 aiohttp 以 Python 3.8 编写,让您的飞机Telegram机器人更快、更强大!
主要特点
- 异步(asyncio 文档,492)
- 具有类型提示 (484) 并且可以与 mypy 一起使用
- 支持PyPy
- 支持 Telegram Bot API 7.2 并快速更新到最新版本的 Bot API
- Telegram Bot API 集成代码是自动生成的,并且可以在 API 更新时轻松重新生成
- 更新路由器(蓝图)
- 具有有限状态机
- 使用强大的Magic filters
- 中间件(传入更新和 API 调用)
- 向 Webhook 提供回复
- 与 GNU Gettext(或 Fluent)集成的 I18n/L10n 支持
简单用法示例
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())
无dispacher用法示例
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())
下载地址
Github | 百度网盘
视频教程
基于Python和Aiogram飞机Telegram机器人开发视频教程
开发文档
https://docs.aiogram.dev/en/latest/
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。