mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-14 19:00:23 +00:00
* Base implementation
* Small refactoring + added possibility to specify post-action on handlers
* Move scene properties to config object
* Revise aiogram/scenes with wizard-based design pattern
Modified files in aiogram/scenes to incorporate the Wizard design pattern. Files affected are _marker.py, _registry.py, _wizard.py and __init__.py. The changes introduced a SceneWizard Class and ScenesManager, both of which aid in controlling navigation between different scenes or states. This helps clarifying the codebase, streamline scene transitions and offer more control over the app flow.
* Added example
* Small optimizations
* Replace ValueError with SceneException in scenes. Added error safety in scene resolver.
* str
* Added possibility to reset context on scene entered and to handle callback query in any state
* Remove inline markup in example
* Small changes
* Docs + example
* Small refactoring
* Remove scene inclusion methods from router
The methods for including scenes as sub-routers have been removed from the router.py file. Instead, the SceneRegistry class is now set to register scenes by default upon initializing. This streamlines the scene management process by removing redundant routers and making registration automatic.
* Init tests
* Small fix in tests
* Add support for State instance in the scene
The aiogram FSM scene now allows the use of State instance as an argument, enabling more customization. Modified the 'as_handler' method to receive **kwargs arguments, allowing passing of attributes to the handler. An additional type check has been also added to ensure the 'scene' is either a subclass of Scene or a string.
* Fixed test
* Expand test coverage for test_fsm module
The commit enhances tests for the test_fsm module to improve code reliability. It includes additional unit tests for the ObserverDecorator and ActionContainer classes and introduces new tests for the SceneHandlerWrapper class. This ensures the correct functionality of the decorator methods, the action container execution, and the handler wrapper.
* Reformat code
* Fixed long line in the example
* Skip some tests on PyPy
* Change mock return_value
* Compatibility...
* Compatibility...
* Compatibility...
* Added base changes description
* Scenes Tests (#1369)
* ADD tests for `SceneRegistry`
* ADD tests for `ScenesManager`
* ADD Changelog
* Revert "ADD Changelog"
This reverts commit 6dd9301252.
* Remove `@pytest.mark.asyncio`, Reformat code
* Scenes Tests. Part 2 (#1371)
* ADD tests for `SceneWizard`
* ADD tests for `Scene`
* Refactor ObserverDecorator to use on.message syntax in test_scene.py
Cover `Scene::__init_subclass__::if isinstance(value, ObserverDecorator):`
* Refactor `HistoryManager` in `aiogram/fsm/scene.py`
Removed condition that checked if 'history' is empty before calling 'update_data' in 'Scene'.
* ADD tests for `HistoryManager`
* Small changes in the documentation
* Small changes in the documentation
* Small changes in the documentation
---------
Co-authored-by: Andrew <11490628+andrew000@users.noreply.github.com>
101 lines
2.9 KiB
ReStructuredText
101 lines
2.9 KiB
ReStructuredText
.. _Finite State Machine:
|
|
|
|
====================
|
|
Finite State Machine
|
|
====================
|
|
|
|
A finite-state machine (FSM) or finite-state automaton (FSA, plural: automata), finite automaton,
|
|
or simply a state machine, is a mathematical model of computation.
|
|
|
|
It is an abstract machine that can be in exactly one of a finite number of states at any given time.
|
|
The FSM can change from one state to another in response to some inputs;
|
|
the change from one state to another is called a transition.
|
|
|
|
An FSM is defined by a list of its states, its initial state,
|
|
and the inputs that trigger each transition.
|
|
|
|
.. raw:: html
|
|
|
|
<hr>
|
|
|
|
Source: `WikiPedia <wiki>`_
|
|
|
|
Usage example
|
|
=============
|
|
|
|
Not all functionality of the bot can be implemented as single handler,
|
|
for example you will need to collect some data from user in separated steps you will need to use FSM.
|
|
|
|
|
|
.. image:: ../../_static/fsm_example.png
|
|
:alt: FSM Example
|
|
|
|
Let's see how to do that step-by-step
|
|
|
|
Step by step
|
|
------------
|
|
|
|
Before handle any states you will need to specify what kind of states you want to handle
|
|
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
:pyobject: Form
|
|
|
|
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`
|
|
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
:pyobject: command_start
|
|
|
|
After that you will need to save some data to the storage and make transition to next step.
|
|
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
:pyobject: process_name
|
|
|
|
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
|
|
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
:pyobject: process_like_write_bots
|
|
|
|
Handle :code:`no`
|
|
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
:pyobject: process_dont_like_write_bots
|
|
|
|
And handle any other answers
|
|
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
:pyobject: process_unknown_write_bots
|
|
|
|
All possible cases of `like_bots` step was covered, let's implement finally step
|
|
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
:pyobject: process_language
|
|
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
: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
|
|
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
:pyobject: cancel_handler
|
|
|
|
Complete example
|
|
----------------
|
|
.. literalinclude:: ../../../examples/finite_state_machine.py
|
|
:language: python
|
|
:linenos:
|
|
|
|
|
|
Read more
|
|
=========
|
|
|
|
.. toctree::
|
|
|
|
storages
|
|
scene
|
|
|
|
|
|
.. _wiki: https://en.wikipedia.org/wiki/Finite-state_machine
|