如何删除电报机器人中的主键盘标记

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

我想删除

ReplyKeyboardMarkup
实例并显示
InlineKeyboardMarkup
,但我无法通过像
reply_markup=ReplyKeyboardRemove()
这样简单的方式来做到这一点,因为下面的代码中插入了新的
InlineKeyboardMarkup
实例

@dispatcher.message(Command("photo"))
async def send_random_photo(message: Message):
    await bot.send_photo(
        chat_id=message.chat.id,
        photo=random_photo_url,
        caption="Do you like this photo?)",
        reply_markup=photo_inline_keyboard,
)

如何删除

ReplyKeyboardMarkup
然后添加
InlineKeyboardMarkup

python telegram-bot aiogram
1个回答
0
投票

这是一种可能的直接方法。您只想删除当前的键盘,然后想在最后发送的照片上显示内联键盘。

为此,首先使用

ReplyKeyboardRemove()
发送照片并保存回复中的消息 ID。调用成功后,调用 editMessageReplyMarkup,传递聊天 ID、消息 ID 和您要设置的内联键盘标记。

我不熟悉Python,但如果我是正确的,代码将是这样的:

@dispatcher.message(Command("photo"))
async def send_random_photo(message: Message):
    # Sending the initial message with ReplyKeyboardRemove
    photo_msg = await bot.send_photo(
        chat_id=message.chat.id,
        photo=random_photo_url,
        caption="Do you like this photo?",
        reply_markup=ReplyKeyboardRemove(remove_keyboard=True),
    )
    
    # Editing the reply markup to set it to photo_inline_keyboard
    await bot.edit_message_reply_markup(
        chat_id=message.chat.id,
        message_id=photo_msg.message_id,
        reply_markup=photo_inline_keyboard,
    )

只是一个附加信息。

如果您想以这样的方式发送

ReplyKeyboardMarkup
,只要按下按钮,它就会立即删除,您可以将
one_time
上的
ReplyKeyboardMarkup
属性设置为
True
。查看 ReplyKeyboardMarkup.one_time 的文档。如果是这种情况,您不必将
ReplyKeyboardRemove
与照片消息一起发送,而只需附加
photo_inline_keyboard
本身即可。

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.