Skip to main content

Telegram bot UI framework on top of aiogram

Project description

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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiogram_dialog-2.2.0.tar.gz (77.7 kB view details)

Uploaded Source

Built Distribution

aiogram_dialog-2.2.0-py3-none-any.whl (109.4 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_dialog-2.2.0.tar.gz.

File metadata

  • Download URL: aiogram_dialog-2.2.0.tar.gz
  • Upload date:
  • Size: 77.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for aiogram_dialog-2.2.0.tar.gz
Algorithm Hash digest
SHA256 2b443145531e2e2676d4ece12e9e0838070733f6a9f3fb7deb57946acd4af056
MD5 6846a192f9ba8b26b231b195123f8a1f
BLAKE2b-256 98b9671c925313e7d87b9c4c793bd54b3f557807194403b85a2c1a958e99fb0a

See more details on using hashes here.

File details

Details for the file aiogram_dialog-2.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiogram_dialog-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ad4c95e03675c7b07752fc6b10b0bd232eff1b89e12450771f17ae3aa9655e2
MD5 f4b15ea53dd8269cf1d8df387cb7cf40
BLAKE2b-256 83254cf141caa0dd2e04014ec73d476040b560b190f22989075e0b3b96278d10

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page