Search code examples
pythoncallbacktelegramtelegram-botpython-telegram-bot

python-telegram-bot CallbackQueryHandler doesn't work on the last message after restarting the bot


I'm using python-telegram-bot to create a bot.

The bot waits for a message from the user after launch and does not work, when I click on the buttons of the message that the bot sent, it first needs to receive a message from the user

The buttons on the last message start working only when a user writes a message, that is, the past message will no longer make sense

def main() -> None: application = Application.builder().token(telegram_token).build()
    conv_handler = ConversationHandler(
        entry_points=[
                      MessageHandler(filters.COMMAND, main_menu),
                      MessageHandler(filters.TEXT, main_menu)],
        states={
            START_ROUTES: [
                CallbackQueryHandler(main_menu_over, pattern="return_to_menu"),

                CallbackQueryHandler(support_menu, pattern="support"),
            ],
            END_ROUTES: [
                CallbackQueryHandler(main_menu_over, pattern="return_to_menu"),
            ],
        },
        fallbacks=[
                   MessageHandler(filters.COMMAND, main_menu),
                   MessageHandler(filters.TEXT, main_menu)],
    )
    application.add_handler(conv_handler)
    
    application.run_polling(allowed_updates=Update.ALL_TYPES)`

I thought there was some way to receive a callback query from a user after launching the bot, and not a message


Solution

  • I'm not quite sure what way to interpret your question, so I'm providing answers to possible interpretations:

    • The entry points to your conversation handler are MessageHandler s. If you want the conversation to be started via a button, use a CallbackQueryHandler (in addition or instead)
    • The current state of a conversation is not saved across restarts of the python script by default. If you want that, please have a look at PTBs built-in persistence setup.

    Disclaimer: I'm currently the maintainer of python-telegram-bot.