[英]Send a command using InlineKeyboardButton python telegram bot
In python telegram bot, is it possible for an InlineKeyboardButton
to send a command like /cancel
when it is pressed?在 python 电报机器人中, InlineKeyboardButton
是否可以在按下时发送/cancel
之类的命令?
For example, when the user presses the cancel button, they will automatically send a /cancel command that will then be processed by the bot.例如,当用户按下取消按钮时,他们将自动发送 /cancel 命令,然后由机器人处理。
From the example here:从这里的例子:
https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard2.py 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.然后按下按钮将生成一个 /cancel 命令,该命令会将机器人带到不同的对话处理程序。
Is this possible?这可能吗?
To handle the input of InlineKeyboardButton
you define a CallbackQueryHandler
which will process the callback_data
of the button pressed by the user.要处理InlineKeyboardButton
的输入,您需要定义一个CallbackQueryHandler
,它将处理用户按下的按钮的callback_data
。 Even if you specify a command the CallbackQueryHandler
will the the one processing the input即使您指定了一个命令, CallbackQueryHandler
也将处理输入
#'/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)如果我正确理解你想要实现的目标,我会定义一个 CallbackQueryHandler 方法,然后控制工作流程,甚至可以调用命令(如果这是你需要做的)
def callback_query_handler(update, context):
# process input
input = update.callback_query.data
# invoke handler
if input == '/help':
help_command_handler(update, context)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.