Skip to content

How to invoke callback_query_handler only in a particular State? #2101

@Katrin-Atreides

Description

@Katrin-Atreides

Please answer these questions before submitting your issue. Thanks!

  1. What version of pyTelegramBotAPI are you using?
    4.14.0

  2. What OS are you using?
    Windows 10

  3. What version of python are you using?
    Python 3.12.0

Hi everyone! I am making a bot and currently I have started to try using States. I know that it is possible to use the state as a condition for a message_handler, like this:

@bot.message_handler(state=states.some_state)
def some_function(message):
  # Some code

But what i want to do is the same thing but with callback_query_handler. Basically, I just want my callback handler to only react to calls while user is in a particular state. But is it even possible? I couldn't find any examples or info online.
I expected something like this to work:

@bot.callback_query_handler(func = lambda call: bot.get_state(call.from_user.id, call.message.chat.id) == states.some_state)
def some_function(call):
 # Some code

And it doesn't work at all. Would be nice to know if I can even do this)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions