我正在使用 this Python 库创建一个 Telegram 机器人。机器人的流程如下:
使用自定义键盘向用户显示菜单。 (使用ReplyKeyboardMarkup())
用户选择“输入数字”后,出现标准键盘供用户输入数字。
用户发送号码后,机器人会回复“使用标准键盘(再次)输入号码”。然后它显示与自定义键盘相同的菜单。
我在下面创建了这个基本功能来测试它。我刚刚将这三个步骤包含在一个函数中,专门尝试弄清楚自定义键盘的工作原理。我知道这个流程实际上不会允许用户输入数字。
我只想看看:
自定义键盘 -> 普通键盘 -> 自定义键盘
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Start the conversation and ask user for input."""
markup1 = ReplyKeyboardMarkup([
["Enter a number"],
])
await update.message.reply_text(
"Select an option:",
reply_markup=markup1,
)
time.sleep(3)
# should display the standard keyboard for the user to input a number,
# but the custom keyboard from before (markup1) stays
await update.message.reply_text(
"Enter a number using the standard keyboard:"
)
time.sleep(3)
# essentially the same menu as markup1 but changed to make sure this,
# displays and it's not just markup1 again
markup2 = ReplyKeyboardMarkup([
["Enter a number (again)"],
])
await update.message.reply_text(
"Enter a number using the standard keyboard (again):", reply_markup=markup2
)
return MAIN_MENU
我不确定剥离程序并在这样的单个函数中测试逻辑是否是最好的主意,因为这个流程实际上可能会导致不同的问题。但是当我用其他所有东西测试不同的键盘选项时,它有点混乱。
我似乎无法让它按照我的意图工作。我似乎不太了解如何在需要时显示自定义键盘,然后在要求用户输入数字时显示普通键盘。
我试过的东西
我尝试使用不带“reply_markup”参数的“update.message.reply_text”发送消息。我的理解是,如果没有指定“reply_markup”,客户端只会显示标准键盘。
当我尝试使用“one_time_keyboard”时,这似乎没有帮助。
我也尝试过“reply_markup=ReplyKeyboardRemove()”,但这似乎会导致其他问题。
任何关于此的信息或建议将不胜感激,因为我对这个库应该如何实现感到很困惑。我在这里发布的内容不多,所以如果我能提供更多有用的信息,请告诉我。
如果您发送
ReplyKeyboardMarkup
,它将在您发送ReplyKeyboardRemove
之前可用。用户可以选择隐藏该自定义键盘(这也是one_time_keyboard_does
),但这并没有将其删除。发送ReplyKeyboardRemove
是完全删除自定义键盘的唯一方法。