Case#
To select one of the texts depending on some condition you can use Case
.
To use this widget you have to specify at least two parameters:
texts
- dictionary, which contains optionsselector
- expression wich is used to get thecondition
value that will be used to select which option ofCase
widget to show.
selector
can be:
string key - this string will be used as a key of
data
dictionary to get the value ofcondition
magic-filter (
F
) - the result of resolving mafic-filter will be used as acondition
function - the result of this function will be used as a
condition`
Note
selector
uses dynamic data. You need to either save this data in dialog_data
beforehand or use getter
to pass data to the widget (see passing data).
You can use ...
(Ellipsis object) as a key in texts
dictionary to provide default option which will be used if no suitable options found.
Code example:
from typing import Any, Dict
from magic_filter import F
from aiogram.filters.state import StatesGroup, State
from aiogram_dialog import Window, DialogManager, Dialog
from aiogram_dialog.widgets.text import Const, Format, Case
class MySG(StatesGroup):
window1 = State()
window2 = State()
# let's assume this is our window data getter
async def get_data(**kwargs):
return {"color": "red", "number": 42}
# Using string selector as a `selector`.
# The value of data["color"] will be used to select wich option of ``Case`` widget to show.
#
# `text` will produce text `Square`
text = Case(
{
"red": Const("Square"),
"green": Const("Unicorn"),
"blue": Const("Moon"),
...: Const("Unknown creature"),
},
selector="color",
)
# Using function as a `selector`.
# The result of this function will be used to select wich option of ``Case`` widget to show.
#
# `text2` will produce text `42 is even!`
def parity_selector(data: Dict, case: Case, manager: DialogManager):
return data["number"] % 2
text2 = Case(
{
0: Format("{number} is even!"),
1: Const("It is Odd"),
},
selector=parity_selector,
)
# Using F-filters as a selector.
# The value of data["dialog_data"]["user"]["test_result"] will be used to select wich option
# of ``Case`` widget to show.
#
# `text3` will produce text `Great job!`
text3 = Case(
{
True: Const("Great job!"),
False: Const("Try again.."),
},
selector=F["dialog_data"]["user"]["test_result"],
)
async def on_dialog_start(start_data: Any, manager: DialogManager):
manager.dialog_data['user'] = {
'test_result': True,
}
dialog = Dialog(
Window(
text,
text2,
text3,
state=MySG.window1,
getter=get_data
),
on_start=on_dialog_start
)
Result:
- class aiogram_dialog.widgets.text.Case(texts, selector, when=None)#
- Parameters: