Passing data#

In your dialogs you can show static data (just text) and dynamic data (variable value). You have access to the following dynamic data:

  • event - currently processed event which triggered window update. Be careful using it, because different types of events can cause refreshing same window

  • middleware_data - data passed from middlewares to handler

  • start_data - data that was passed when the current dialog was started. These data is stored in the aiogram FSM storage.

  • dialog_data - you can use this dict to store dialog-related data. It will be accessible at all windows of current dialog. These data is stored in the aiogram FSM storage.

In addition you can specify getter-functions for dialog and window. Getter-function has to return a dictionary.

Library collects all the data above into one dictionary object and passes this object to widgets.

Let’s look at the example:

Note

In this and later examples we will skip common bot creation and dialog registration code unless it has notable differences with quickstart

from aiogram.filters.state import StatesGroup, State
from aiogram.types import CallbackQuery

from aiogram_dialog import Window, Dialog, DialogManager
from aiogram_dialog.widgets.kbd import Button, Back
from aiogram_dialog.widgets.text import Const, Format

class MySG(StatesGroup):
    window1 = State()
    window2 = State()


async def window1_get_data(**kwargs):
    return {
        "something": "data from Window1 getter",
    }


async def window2_get_data(**kwargs):
    return {
        "something": "data from Window2 getter",
    }


async def dialog_get_data(**kwargs):
    return {
        "name": "Tishka17",
    }


async def button1_clicked(callback: CallbackQuery, button: Button, manager: DialogManager):
    """ Add data to `dialog_data` and switch to the next window of current dialog """
    manager.dialog_data['user_input'] = 'some data from user, stored in `dialog_data`'
    await manager.next()


dialog = Dialog(
    Window(
        Format("Hello, {name}!"),
        Format("Something: {something}"),        
        Button(Const("Next window"), id="button1", on_click=button1_clicked),
        state=MySG.window1,
        getter=window1_get_data,  # here we specify data getter for window1
    ),
    Window(
        Format("Hello, {name}!"),
        Format("Something: {something}"),        
        Format("User input: {dialog_data[user_input]}"),
        Back(text=Const("Back")),
        state=MySG.window2,
        getter=window2_get_data,  # here we specify data getter for window2
    ),
    getter=dialog_get_data  # here we specify data getter for dialog
)

Result:

../../_images/passing_data_example.png

In event handlers you don’t have access to that dictionary, but you can acess event, middleware_data, start_data, dialog_data via dialog_manager:

async def button1_clicked(callback: CallbackQuery, button: Button, manager: DialogManager):
    dialog_data = manager.dialog_data
    event = manager.event
    middleware_data = manager.middleware_data
    start_data = manager.start_data

You can find more examples of accessing dynamic data from various widgets in documentation for these widgets.