skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search