|
| 1 | +import asyncio |
| 2 | +from collections.abc import Sequence |
| 3 | + |
| 4 | +from aiogram.dispatcher.event.handler import CallbackType |
| 5 | +from aiogram.fsm.storage.base import StorageKey |
| 6 | +from aiogram.types import Message |
| 7 | +from typing_extensions import NamedTuple |
| 8 | + |
| 9 | +__all__ = ("ConversationRegistry",) |
| 10 | + |
| 11 | + |
| 12 | +class ConversationData(NamedTuple): |
| 13 | + """Container for an active conversation. |
| 14 | +
|
| 15 | + Stores the Future object awaiting a message and the filters to apply. |
| 16 | +
|
| 17 | + :param future: asyncio.Future that will hold the incoming Message |
| 18 | + :param filters: Sequence of CallbackType filters to validate the message |
| 19 | + """ |
| 20 | + |
| 21 | + future: asyncio.Future[Message] |
| 22 | + filters: Sequence[CallbackType] |
| 23 | + |
| 24 | + |
| 25 | +class ConversationRegistry: |
| 26 | + """Registry for managing active conversations with users. |
| 27 | +
|
| 28 | + This class allows setting up a "wait for message" scenario where |
| 29 | + a handler can pause and wait for a specific message from a user, |
| 30 | + optionally filtered by aiogram filters. |
| 31 | + """ |
| 32 | + |
| 33 | + STATE = "raito__conversation" |
| 34 | + |
| 35 | + def __init__(self) -> None: |
| 36 | + """Initialize the conversation registry.""" |
| 37 | + self._conversations: dict[StorageKey, ConversationData] = {} |
| 38 | + |
| 39 | + def listen(self, key: StorageKey, *filters: CallbackType) -> asyncio.Future[Message]: |
| 40 | + """Start listening for a message with a specific StorageKey. |
| 41 | +
|
| 42 | + :param key: StorageKey identifying the conversation (user/chat/bot) |
| 43 | + :param filters: Optional filters to apply when the message arrives |
| 44 | + :return: Future that will resolve with the Message when received |
| 45 | + """ |
| 46 | + future = asyncio.get_running_loop().create_future() |
| 47 | + self._conversations[key] = ConversationData(future, filters) |
| 48 | + return future |
| 49 | + |
| 50 | + def get_filters(self, key: StorageKey) -> Sequence[CallbackType] | None: |
| 51 | + """Get the filters associated with an active conversation. |
| 52 | +
|
| 53 | + :param key: StorageKey identifying the conversation |
| 54 | + :return: Sequence of CallbackType filters or None if no conversation exists |
| 55 | + """ |
| 56 | + data = self._conversations.get(key) |
| 57 | + return data.filters if data else None |
| 58 | + |
| 59 | + def resolve(self, key: StorageKey, message: Message) -> None: |
| 60 | + """Complete the conversation with a received message. |
| 61 | +
|
| 62 | + :param key: StorageKey identifying the conversation |
| 63 | + :param message: Message object that satisfies the filters |
| 64 | + """ |
| 65 | + data = self._conversations.pop(key, None) |
| 66 | + if data and not data.future.done(): |
| 67 | + data.future.set_result(message) |
| 68 | + |
| 69 | + def cancel(self, key: StorageKey) -> None: |
| 70 | + """Cancel an active conversation. |
| 71 | +
|
| 72 | + Cancels the Future and removes the conversation from the registry. |
| 73 | +
|
| 74 | + :param key: StorageKey identifying the conversation |
| 75 | + """ |
| 76 | + data = self._conversations.pop(key, None) |
| 77 | + if data and not data.future.done(): |
| 78 | + data.future.cancel() |
0 commit comments