【问题标题】:Send a command using InlineKeyboardButton python telegram bot使用 InlineKeyboardButton python 电报机器人发送命令
【发布时间】:2020-10-05 19:25:20
【问题描述】:

在python电报机器人中,InlineKeyboardButton是否可以在按下时发送类似/cancel的命令?

例如,当用户按下取消按钮时,他们会自动发送 /cancel 命令,然后由机器人处理。

从这里的例子:

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)]
)

我希望能够这样做,以便我可以更改我的入口点并在按下按钮时使用不同的对话处理程序。

然后按下按钮会生成一个 /cancel 命令,该命令会将机器人带到不同的对话处理程序。

这可能吗?

【问题讨论】:

    标签: python python-3.x telegram telegram-bot python-telegram-bot


    【解决方案1】:

    要处理InlineKeyboardButton 的输入,您定义一个CallbackQueryHandler,它将处理用户按下的按钮的callback_data。 即使您指定了一个命令,CallbackQueryHandler 也会处理输入

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

    如果我正确理解您想要实现的目标,我会定义一个 CallbackQueryHandler 方法,然后该方法控制工作流程,甚至可以调用命令(如果这是您需要做的)

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

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 2021-08-23
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      相关资源
      最近更新 更多