使用InlineKeyboardButton PYTHON电报机器人发送命令 [英] Send a command using InlineKeyboardButton python telegram bot

查看:43
本文介绍了使用InlineKeyboardButton PYTHON电报机器人发送命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如,当用户按下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命令,该命令将使机器人转到不同的对话处理程序。

这可能吗?

推荐答案

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

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

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

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

这篇关于使用InlineKeyboardButton PYTHON电报机器人发送命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆