Skip to content

Commit

Permalink
fixed redis data bug on set_state
Browse files Browse the repository at this point in the history
  • Loading branch information
coder2020official committed Jul 26, 2024
1 parent dd0dfa9 commit 7f99176
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 7 additions & 0 deletions telebot/asyncio_storage/redis_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions telebot/storage/redis_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7f99176

Please sign in to comment.