diff --git a/telebot/asyncio_storage/redis_storage.py b/telebot/asyncio_storage/redis_storage.py index 5c0c1bb0a..69b18d5a1 100644 --- a/telebot/asyncio_storage/redis_storage.py +++ b/telebot/asyncio_storage/redis_storage.py @@ -95,7 +95,14 @@ async def set_state(self, pipe, chat_id: int, user_id: int, state: str, state = state.name _key = self._get_key(chat_id, user_id, self.prefix, self.separator, business_connection_id, message_thread_id, bot_id) + pipe.hget(_key, "data") + result = await pipe.execute() + data = result[0] + if data is None: + pipe.hset(_key, "data", json.dumps({})) + await pipe.hset(_key, "state", state) + return True async def get_state(self, chat_id: int, user_id: int, business_connection_id: Optional[str] = None, diff --git a/telebot/storage/redis_storage.py b/telebot/storage/redis_storage.py index 3a81ac033..1d1413fa2 100644 --- a/telebot/storage/redis_storage.py +++ b/telebot/storage/redis_storage.py @@ -79,9 +79,17 @@ def set_state( def set_state_action(pipe): pipe.multi() - #pipe.hset(_key, mapping={"state": state, "data": "{}"}) + + data = pipe.hget(_key, "data") + result = pipe.execute() + data = result[0] + if data is None: + # If data is None, set it to an empty dictionary + data = {} + pipe.hset(_key, "data", json.dumps(data)) + pipe.hset(_key, "state", state) - + self.redis.transaction(set_state_action, _key) return True