使用InlineKeyboardButton发送命令 python telegram bot

问题描述 投票:0回答:1

在 python telegram bot 中,是否可以让一个 InlineKeyboardButton 发出这样的命令 /cancel 当它被按下时?

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

从这里的例子来看。

https:/github.comython-telegram-botpython-telegram-botblobmasterexamplesinlinekeyboard2.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)]
)

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

按下按钮后会产生一个取消命令,将机器人带到不同的对话处理程序。

这可能吗?

python python-3.x telegram telegram-bot python-telegram-bot
1个回答
0
投票

处理输入的 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)  
© www.soinside.com 2019 - 2024. All rights reserved.