Aiogram -- set state for a exact user

Viewed 2070

I'm writing a bot in python and aiogram. The point is that the administrator accepts (or rejects) user requests. Accordingly, when the administrator clicks on the button in his chat, I need to change the user's state (his uid is known). I didn't find how to do it anywhere.

I'm looking for something like

dp.set_state(uid, User.accepted))

Thanks!

2 Answers

I had the same problem

Found method set() in base class State:

class State:
    ...
    async def set(self):
        state = Dispatcher.get_current().current_state()
        await state.set_state(self.state)

So I created new class from State and overrode method this way:

    async def set(self, user=None):
        """Option to set state for concrete user"""
        state = Dispatcher.get_current().current_state(user=user)
        await state.set_state(self.state)

Usage:

@dp.message_handler(state='*')
async def example_handler(message: Message):
    await SomeStateGroup.SomeState.set(user=message.from_user.id)

If you want some mailing stuff, collect user ids and use that hint.

from aiogram.dispatcher import FSMContext

@dp.message_handler(state='*')
async def example_handler(message: types.Message, state: FSMContext):
    new_state = FSMContext(storage, chat_id, user_id)

then set_state() and set_data() on the new_state.

storage is the FSM storage.

Related