From 749255fa070f1f46a16899c322f5c86e278dfecb Mon Sep 17 00:00:00 2001 From: JRoot Junior Date: Fri, 10 Nov 2023 22:45:33 +0200 Subject: [PATCH] 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. --- aiogram/fsm/scene.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/aiogram/fsm/scene.py b/aiogram/fsm/scene.py index d39a4f01..7305c1c1 100644 --- a/aiogram/fsm/scene.py +++ b/aiogram/fsm/scene.py @@ -17,6 +17,7 @@ from aiogram.dispatcher.router import Router from aiogram.exceptions import SceneException from aiogram.filters import StateFilter from aiogram.fsm.context import FSMContext +from aiogram.fsm.state import State from aiogram.fsm.storage.memory import MemoryStorageRecord from aiogram.types import TelegramObject, Update @@ -401,7 +402,7 @@ class Scene: return router @classmethod - def as_handler(cls) -> CallbackType: + def as_handler(cls, **kwargs: Any) -> CallbackType: """ Create an entry point handler for the scene, can be used to simplify the handler that starts the scene. @@ -410,7 +411,7 @@ class Scene: """ async def enter_to_scene_handler(event: TelegramObject, scenes: ScenesManager) -> None: - await scenes.enter(cls) + await scenes.enter(cls, **kwargs) return enter_to_scene_handler @@ -809,6 +810,8 @@ class SceneRegistry: """ if inspect.isclass(scene) and issubclass(scene, Scene): scene = scene.__scene_config__.state + if isinstance(scene, State): + scene = scene.state if scene is not None and not isinstance(scene, str): raise SceneException("Scene must be a subclass of Scene or a string")