Search code examples
pythonaiogram

How to reset state in aiogram FSM without resetting data


I am developing a telegram bot using Python 3.9.7 and aiogram 2.16, at the end of my function I want to reset the user's state without deleting the stored data.

I tried using state.finish() but this method removes all saved data from storage Here's the code:

if user_data.get("chosen_groups") == "fired":
    # some code here
    await state.finish()

I can set the user to the initial stage using State.state.set(), but this reduces the readability of the code and increases the probability of an error. How can I set the user to the initial state without deleting the data?


Solution

  • if you only need to reset the state without loosing data, use await state.reset_state (with_data = False).

    You can read about this in aiogram documentation