mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 18:01:04 +00:00
Fixed lines in FSM example
This commit is contained in:
parent
dc7c99763e
commit
a332e88bc3
2 changed files with 13 additions and 34 deletions
|
|
@ -36,70 +36,49 @@ Step by step
|
||||||
Before handle any states you will need to specify what kind of states you want to handle
|
Before handle any states you will need to specify what kind of states you want to handle
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/finite_state_machine.py
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
||||||
:language: python
|
:pyobject: Form
|
||||||
:linenos:
|
|
||||||
:lineno-start: 15
|
|
||||||
:lines: 15-18
|
|
||||||
|
|
||||||
And then write handler for each state separately from the start of dialog
|
And then write handler for each state separately from the start of dialog
|
||||||
|
|
||||||
Here is dialog can be started only via command :code:`/start`, so lets handle it and make transition user to state :code:`Form.name`
|
Here is dialog can be started only via command :code:`/start`, so lets handle it and make transition user to state :code:`Form.name`
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/finite_state_machine.py
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
||||||
:language: python
|
:pyobject: command_start
|
||||||
:linenos:
|
|
||||||
:lineno-start: 21
|
|
||||||
:lines: 21-27
|
|
||||||
|
|
||||||
After that you will need to save some data to the storage and make transition to next step.
|
After that you will need to save some data to the storage and make transition to next step.
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/finite_state_machine.py
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
||||||
:language: python
|
:pyobject: process_name
|
||||||
:linenos:
|
|
||||||
:lineno-start: 48
|
|
||||||
:lines: 48-63
|
|
||||||
|
|
||||||
At the next steps user can make different answers, it can be `yes`, `no` or any other
|
At the next steps user can make different answers, it can be `yes`, `no` or any other
|
||||||
|
|
||||||
Handle :code:`yes` and soon we need to handle :code:`Form.language` state
|
Handle :code:`yes` and soon we need to handle :code:`Form.language` state
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/finite_state_machine.py
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
||||||
:language: python
|
:pyobject: process_like_write_bots
|
||||||
:linenos:
|
|
||||||
:lineno-start: 77
|
|
||||||
:lines: 77-84
|
|
||||||
|
|
||||||
Handle :code:`no`
|
Handle :code:`no`
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/finite_state_machine.py
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
||||||
:language: python
|
:pyobject: process_dont_like_write_bots
|
||||||
:linenos:
|
|
||||||
:lineno-start: 66
|
|
||||||
:lines: 66-74
|
|
||||||
|
|
||||||
And handle any other answers
|
And handle any other answers
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/finite_state_machine.py
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
||||||
:language: python
|
:pyobject: process_unknown_write_bots
|
||||||
:linenos:
|
|
||||||
:lineno-start: 87
|
|
||||||
:lines: 87-89
|
|
||||||
|
|
||||||
All possible cases of `like_bots` step was covered, let's implement finally step
|
All possible cases of `like_bots` step was covered, let's implement finally step
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/finite_state_machine.py
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
||||||
:language: python
|
:pyobject: process_language
|
||||||
:linenos:
|
|
||||||
:lineno-start: 92
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
||||||
:lines: 92-102
|
:pyobject: show_summary
|
||||||
|
|
||||||
And now you have covered all steps from the image, but you can make possibility to cancel conversation, lets do that via command or text
|
And now you have covered all steps from the image, but you can make possibility to cancel conversation, lets do that via command or text
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/finite_state_machine.py
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
||||||
:language: python
|
:pyobject: cancel_handler
|
||||||
:linenos:
|
|
||||||
:lineno-start: 30
|
|
||||||
:lines: 30-45
|
|
||||||
|
|
||||||
Complete example
|
Complete example
|
||||||
----------------
|
----------------
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ class Form(StatesGroup):
|
||||||
language = State()
|
language = State()
|
||||||
|
|
||||||
|
|
||||||
@form_router.message(Command(commands=["start"]))
|
@form_router.message(Command("start"))
|
||||||
async def command_start(message: Message, state: FSMContext) -> None:
|
async def command_start(message: Message, state: FSMContext) -> None:
|
||||||
await state.set_state(Form.name)
|
await state.set_state(Form.name)
|
||||||
await message.answer(
|
await message.answer(
|
||||||
|
|
@ -33,7 +33,7 @@ async def command_start(message: Message, state: FSMContext) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@form_router.message(Command(commands=["cancel"]))
|
@form_router.message(Command("cancel"))
|
||||||
@form_router.message(F.text.casefold() == "cancel")
|
@form_router.message(F.text.casefold() == "cancel")
|
||||||
async def cancel_handler(message: Message, state: FSMContext) -> None:
|
async def cancel_handler(message: Message, state: FSMContext) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue