1
votes

In python telegram bot, is it possible for an InlineKeyboardButton to send a command like /cancel when it is pressed?

For example, when the user presses the cancel button, they will automatically send a /cancel command that will then be processed by the bot.

From the example here:

https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard2.py

conv_handler = ConversationHandler(
    entry_points=[CommandHandler('start', start)],
    states={
        FIRST: [CallbackQueryHandler(one, pattern='^' + str(ONE) + '$'),
                CallbackQueryHandler(two, pattern='^' + str(TWO) + '$'),
                CallbackQueryHandler(three, pattern='^' + str(THREE) + '$'),
                CallbackQueryHandler(four, pattern='^' + str(FOUR) + '$')],
        SECOND: [CallbackQueryHandler(start_over, pattern='^' + str(ONE) + '$'),
                 CallbackQueryHandler(end, pattern='^' + str(TWO) + '$')]
    },
    fallbacks=[CommandHandler('start', start)]
)

I would like to be able to do this so that I can change my entry point and use a different conversation handler upon button press.

Pressing the button would then generate a /cancel command that would bring the bot to a different conversation handler.

Is this possible?

1

1 Answers

0
votes

To handle the input of InlineKeyboardButton you define a CallbackQueryHandler which will process the callback_dataof the button pressed by the user. Even if you specify a command the CallbackQueryHandler will the the one processing the input

 #'/help' sent to CallbackQueryHandler
 options.append(InlineKeyboardButton("a", callback_data="/help"))

If I understand correctly what you want to achieve I would define a CallbackQueryHandler method which then controls the workflow, and can even call the command (if this what you need to do)

def callback_query_handler(update, context):
   # process input
   input = update.callback_query.data
   # invoke handler
   if input == '/help':
       help_command_handler(update, context)