Project Icon

aiogram_dialog

基于 aiogram 的 Telegram 机器人交互式界面开发框架

aiogram_dialog 是一个专为 Telegram 机器人设计的交互式界面开发框架。它支持富文本渲染、自动消息更新、多对话栈和内联键盘小部件等功能。该框架采用类似 GUI 应用程序的设计理念,实现了数据检索、渲染和操作处理的分离,并提供可重用的小部件和独立的上下文管理。适用于 aiogram v3.x 版本,aiogram_dialog 为开发者提供了一种以用户体验为中心的机器人开发方法。

Aiogram Dialog

PyPI version Doc downloads license license

Version status:

  • v2.x - actual release, supports aiogram v3.x
  • v1.x - old release, supports aiogram v2.x, critical fix only

About

aiogram-dialog is a framework for developing interactive messages and menus in your telegram bot like a normal GUI application.

It is inspired by ideas of Android SDK and other tools.

Main ideas are:

  • split data retrieving, rendering and action processing - you need nothing to do for showing same content after some actions, also you can show same data in multiple ways.
  • reusable widgets - you can create calendar or multiselect at any point of your application without copy-pasting its internal logic
  • limited scope of context - any dialog keeps some data until closed, multiple opened dialogs process their data separately

Designing you bot with aiogram-dialog you think about user, what he sees and what he does. Then you split this vision into reusable parts and design your bot combining dialogs, widows and widgets. By this moment you can review interface and add your core logic.

Many components are ready for use, but you can extend and add your own widgets and even core features.

For more details see documentation and examples

Supported features:

  • Rich text rendering using format function or Jinja2 template engine.
  • Automatic message updating after user actions
  • Multiple independent dialog stacks with own data storage and transitions
  • Inline keyboard widgets like SwitchTo, Start, Cancel for state switching, Calendar for date selection and others.
  • Stateful widgets: Checkbox, Multiselect, Counter, TextInput. They record user actions and allow you to retrieve this data later.
  • Multiple buttons layouts including simple grouping (Group, Column), page scrolling (ScrollingGroup), repeating of same buttons for list of data (ListGroup).
  • Sending media (like photo or video) with fileid caching and handling switching to/from message with no media.
  • Different rules of transitions between windows/dialogs like keeping only one dialog on top of stack or force sending enw message instead of updating one.
  • Offline HTML-preview for messages and transitions diagram. They can be used to check all states without emulating real use cases or exported for demonstration purposes.

Usage

Example below is suitable for aiogram_dialog v2.x and aiogram v3.x

Declaring Window

Each window consists of:

  • Text widgets. Render text of message.
  • Keyboard widgets. Render inline keyboard
  • Media widget. Renders media if need
  • Message handler. Called when user sends a message when window is shown
  • Data getter functions (getter=). They load data from any source which can be used in text/keyboard
  • State. Used when switching between windows

Info: always create State inside StatesGroup

from aiogram.fsm.state import StatesGroup, State
from aiogram_dialog.widgets.text import Format, Const
from aiogram_dialog.widgets.kbd import Button
from aiogram_dialog import Window


class MySG(StatesGroup):
    main = State()


async def get_data(**kwargs):
    return {"name": "world"}


Window(
    Format("Hello, {name}!"),
    Button(Const("Empty button"), id="nothing"),
    state=MySG.main,
    getter=get_data,
)

Declaring dialog

Window itself can do nothing, just prepares message. To use it you need dialog:

from aiogram.fsm.state import StatesGroup, State
from aiogram_dialog import Dialog, Window


class MySG(StatesGroup):
    first = State()
    second = State()


dialog = Dialog(
    Window(..., state=MySG.first),
    Window(..., state=MySG.second),
)

Info: All windows in a dialog MUST have states from then same StatesGroup

After creating a dialog you need to register it into the Dispatcher and set it up using the setup_dialogs function:

from aiogram import Dispatcher
from aiogram_dialog import setup_dialogs

...
dp = Dispatcher(storage=storage)  # create as usual
dp.include_router(dialog)
setup_dialogs(dp)

Then start dialog when you are ready to use it. Dialog is started via start method of DialogManager instance. You should provide corresponding state to switch into (usually it is state of first window in dialog).

For example in /start command handler:

async def user_start(message: Message, dialog_manager: DialogManager):
    await dialog_manager.start(MySG.first, mode=StartMode.RESET_STACK)

dp.message.register(user_start, CommandStart())

Info: Always set mode=StartMode.RESET_STACK in your top level start command. Otherwise, dialogs are stacked just as they do on your mobile phone, so you can reach stackoverflow error

项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

白日梦AI

白日梦AI提供专注于AI视频生成的多样化功能,包括文生视频、动态画面和形象生成等,帮助用户快速上手,创造专业级内容。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

讯飞绘镜

讯飞绘镜是一个支持从创意到完整视频创作的智能平台,用户可以快速生成视频素材并创作独特的音乐视频和故事。平台提供多样化的主题和精选作品,帮助用户探索创意灵感。

Project Cover

讯飞文书

讯飞文书依托讯飞星火大模型,为文书写作者提供从素材筹备到稿件撰写及审稿的全程支持。通过录音智记和以稿写稿等功能,满足事务性工作的高频需求,帮助撰稿人节省精力,提高效率,优化工作与生活。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号