在我的基于 Python-telegram-bot 包的程序中,有一个名为
params_set
的方法,包含以下代码:
def params_set(update: Update, context: CallbackContext):
query = update.callback_query
custom_keyboard = []
for i in range(10):
custom_keyboard.append([KeyboardButton(str(i))])
reply_markup = ReplyKeyboardMarkup(custom_keyboard, one_time_keyboard=True)
update.message.reply_text("Some text", reply_markup=reply_markup)
出现此问题是因为
params_set
方法是由 CallbackQueryHandler
触发的,导致 update.message
变为 None
。要解决此问题,我需要将 update.message.reply_text
替换为 query.edit_message_text
并包含 reply_markup
参数。
但是,当我尝试打电话时:
query.edit_message_text("Some text", reply_markup=reply_markup)
发生下一个错误:
Can't parse inline keyboard button: text buttons are unallowed in the inline keyboard
问题:是否可以在
ReplyKeyboardMarkup
触发方法中使用CallbackQueryHandler
?
编辑键盘仅支持使用内联键盘
ReplyKeyboardMarkup
只能在发送新消息时使用
对于处理回调查询,这意味着仅当您发送新消息作为 ReplyKeyboardMarkup
的结果时,才可以使用 CallbackQuery
。这可以例如通过 context.bot.send_message
或 update.callback_query.message.reply_text
(或 update.effective_message.reply_text
,等效)完成。请注意,后者仅在 update.callback_query.message
可访问时才有效。
免责声明:我目前是
python-telegram-bot
的维护者。