Search code examples
telegram-botpython-telegram-bot

CallbackQueryHandler or ConversationHandler for a message sent from bot class


Using python-telegram-bot, I have a bot running with very similar settings as for the other examples. On the other side i have parallel processes that allow me to send messages to users interacting with the bot periodically. The parallel proccess communicates with the users using:

bot = Bot(token=TELEGRAM_TOKEN)
def get_button_options():
    keyboard = [[ InlineKeyboardButton("reply", callback_data='reply),]]
    reply_markup = InlineKeyboardMarkup(keyboard)
    return reply_markup
bot.send_message(chat_id=self.telegram_id, text=text, reply_markup=get_button_options())

The thing is, even if I'm capable to send messages to the user, the above code does not allow the user to interact with the bot (By that I mean that, once the user presses the button, the bot doesn't execute any callback). This is because I need either a CallbackQueryHandler or ConversationHandler within the bot.

However for the ConversationHandler I don't seem to find any appropiate entry_point, since it is a message sent from a parallel process. On the other hand, using the following CallbackQueryHandler (taken from the example) in the main bot doesn't seem to have any effect:

def button(update: Update, _: CallbackContext) -> None:
    query = update.callback_query

    # CallbackQueries need to be answered, even if no notification to the user is needed
    # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
    query.answer()

    query.edit_message_text(text=f"Selected option: {query.data}")

in the main function, I have added the handler using updater.dispatcher.add_handler(CallbackQueryHandler(button))

Any ideas on how to solve that?


Solution

  • As said by @CallMeStag, the solution is the following:

    ConversationHandler(
        entry_points=[CallbackQueryHandler(button)])
    

    the correct entry point is a CallbackQueryHandler, so then it captures the selected button