F
F
Forward12322021-07-11 16:34:28
Python
Forward1232, 2021-07-11 16:34:28

Why is the if not called in the handler?

@dp.callback_query_handler(text='qwe')
async def plt(call: CallbackQuery):
    if 'abc' in call.message:
        await call.message('Привет')


Why 'abc' is not executed

Tried to fix

@dp.callback_query_handler()
async def plt(call: CallbackQuery):
    if 'polt' in call.data:
        await call.message.edit_text('Красава', reply_markup=pltv)
        if 'amf' in call.data:
            await call.message.answer('Ура', reply_markup=btnp_all)


That doesn't work either :(
I want 'amf' to be executed only when 'polt' is done

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-07-11
@Forward1232

Is it handling clicks on virtual buttons?
Each click is a separate handler call.
The handler that received polt doesn't know anything about amf yet.
The handler that received amf will no longer know anything about polt.
You need to save state for different users. For example, maintain a collection of users who clicked polt. In general, you should read about finite automata - the theory will sound somewhat abstruse, but the idea is as simple as two pennies.
A state is when some condition is met for an object. For example, in our case, the user has two states - "didn't click on polt", "clicked on polt".
There are transitions between states. In our case, there is one transition that should be called by clicking on polt.
Hence the conclusion: we must store their state for all users known to us. The callback_query_handler() handler must, when receiving a command, look at their state and perform actions depending on the combination of command and state. Actions may include transition to another state.
For now, you can store states in a dictionary (dict), where the key is the user id, and the value is the state code. But the dictionary will be lost when the bot is restarted, so you will have to learn how to store in the database later.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question