Fix #1743: scene handling for channel updates (#1763)

* Fix scene handling for channel updates with missing FSM state (#1743)

* Add changelog entry for scene handling fix

* Refine scene context error handling
This commit is contained in:
Kostiantyn Kriuchkov 2026-02-10 23:08:44 +02:00 committed by GitHub
parent 1708980ceb
commit da7bfdca0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 298 additions and 4 deletions

View file

@ -70,6 +70,9 @@ class FSMContextMiddleware(BaseMiddleware):
) -> FSMContext | None:
if chat_id is None:
chat_id = user_id
elif user_id is None and self.strategy in {FSMStrategy.CHAT, FSMStrategy.CHAT_TOPIC}:
# CHAT/CHAT_TOPIC are chat-scoped, so missing user_id can fallback to chat_id.
user_id = chat_id
if chat_id is not None and user_id is not None:
chat_id, user_id, thread_id = apply_strategy(

View file

@ -248,8 +248,16 @@ class SceneHandlerWrapper:
event: TelegramObject,
**kwargs: Any,
) -> Any:
state: FSMContext = kwargs["state"]
scenes: ScenesManager = kwargs["scenes"]
try:
state: FSMContext = kwargs["state"]
scenes: ScenesManager = kwargs["scenes"]
except KeyError as error:
missing_key = error.args[0]
msg = (
f"Scene context key {missing_key!r} is not available. "
"Ensure FSM is enabled and pipeline is intact."
)
raise SceneException(msg) from None
event_update: Update = kwargs["event_update"]
scene = self.scene(
wizard=SceneWizard(
@ -768,12 +776,15 @@ class SceneRegistry:
data: dict[str, Any],
) -> Any:
assert isinstance(event, Update), "Event must be an Update instance"
state = data.get("state")
if state is None:
return await handler(event, data)
data["scenes"] = ScenesManager(
registry=self,
update_type=event.event_type,
event=event.event,
state=data["state"],
state=state,
data=data,
)
return await handler(event, data)
@ -784,12 +795,16 @@ class SceneRegistry:
event: TelegramObject,
data: dict[str, Any],
) -> Any:
state = data.get("state")
if state is None:
return await handler(event, data)
update: Update = data["event_update"]
data["scenes"] = ScenesManager(
registry=self,
update_type=update.event_type,
event=event,
state=data["state"],
state=state,
data=data,
)
return await handler(event, data)