Memory storage for states. Stores states in memory as a dictionary. .. code-block:: python3 storage = StateMemoryStorage() bot = TeleBot(token, storage=storage) :param separator: Separator for keys, default is ":". :type separator: Optional[str] :param p
| 3 | |
| 4 | |
| 5 | class StateMemoryStorage(StateStorageBase): |
| 6 | """ |
| 7 | Memory storage for states. |
| 8 | |
| 9 | Stores states in memory as a dictionary. |
| 10 | |
| 11 | .. code-block:: python3 |
| 12 | |
| 13 | storage = StateMemoryStorage() |
| 14 | bot = TeleBot(token, storage=storage) |
| 15 | |
| 16 | :param separator: Separator for keys, default is ":". |
| 17 | :type separator: Optional[str] |
| 18 | |
| 19 | :param prefix: Prefix for keys, default is "telebot". |
| 20 | :type prefix: Optional[str] |
| 21 | """ |
| 22 | |
| 23 | def __init__( |
| 24 | self, separator: Optional[str] = ":", prefix: Optional[str] = "telebot" |
| 25 | ) -> None: |
| 26 | self.separator = separator |
| 27 | self.prefix = prefix |
| 28 | if not self.prefix: |
| 29 | raise ValueError("Prefix cannot be empty") |
| 30 | |
| 31 | self.data = ( |
| 32 | {} |
| 33 | ) # key: telebot:bot_id:business_connection_id:message_thread_id:chat_id:user_id |
| 34 | |
| 35 | def set_state( |
| 36 | self, |
| 37 | chat_id: int, |
| 38 | user_id: int, |
| 39 | state: str, |
| 40 | business_connection_id: Optional[str] = None, |
| 41 | message_thread_id: Optional[int] = None, |
| 42 | bot_id: Optional[int] = None, |
| 43 | ) -> bool: |
| 44 | if hasattr(state, "name"): |
| 45 | state = state.name |
| 46 | |
| 47 | _key = self._get_key( |
| 48 | chat_id, |
| 49 | user_id, |
| 50 | self.prefix, |
| 51 | self.separator, |
| 52 | business_connection_id, |
| 53 | message_thread_id, |
| 54 | bot_id, |
| 55 | ) |
| 56 | |
| 57 | if self.data.get(_key) is None: |
| 58 | self.data[_key] = {"state": state, "data": {}} |
| 59 | else: |
| 60 | self.data[_key]["state"] = state |
| 61 | |
| 62 | return True |
no outgoing calls
no test coverage detected
searching dependent graphs…