How do you remove reply keyboard without sending a message in Telegram?
Asked Answered
E

4

11

In Telegram bot you can send a message with the reply keyboard using the sendMessage method.

The keyboard is getting displayed instead of normal qwerty one.

We can remove the displayed keyboard by sending another message and by passing ReplyKeyboardRemove object with it. However, this requires some extraneous message to be sent.

Is it possible to remove the keyboard without actually sending any real message?

I'm aware of one_time_keyboard option, but it will only hide the keyboard without removing it.

Eudocia answered 5/1, 2018 at 20:0 Comment(0)
B
8

You could edit the message using editMessageText or editMessageReplyMarkup and simply not pass a reply_markup to make it disappear.

Betsey answered 5/1, 2018 at 20:24 Comment(4)
Looks like a very simple solution, funny I haven't tried it myself. Thanks, I will give it a try on a first occasion.Eudocia
This does not work, if the message does not have inline keyboards, as per official documentation: Please note, that it is currently only possible to edit messages without reply_markup or with inline keyboards.Wallsend
IDK why this answer has such a high rating. It doesn't work at all.Unweave
Not passing reply_markup does nothing. And if you do reply_markup=ReplyKeyboardRemove() it just crashes with telegram.error.BadRequest: Inline keyboard expected; How the heck is this the accepted answer?Repro
E
6

As Andrew Savinykh pointed out, there are no way to edit the message which has a markup other than inline_keyboard or nothing (official docs: https://core.telegram.org/bots/api#updating-messages):

Please note, that it is currently only possible to edit messages without reply_markup or with inline keyboards.

The possible kludge is to send a message which clears the keyboard, and than delete it immediately.

Aiogram helper:

async def remove_chat_buttons(chat_id: int, 
                              msg_text: str = r"_It is not the message you are looking for\.\.\._"):
    """Deletes buttons below the chat.
    For now there are no way to delete kbd other than inline one, check
        https://core.telegram.org/bots/api#updating-messages.
    """
    msg = await bot.send_message(chat_id,
                                 msg_text,
                                 reply_markup=aiogram.types.ReplyKeyboardRemove(),
                                 parse_mode="MarkdownV2")
    await msg.delete()

It is not the answer we want, but for now I never noticed this dummy message - it disappear instantly.

Explanation answered 11/12, 2022 at 5:52 Comment(0)
B
3

I was using node-telegram-bot-api and I was able to do it using remove_keyboard.

There is a way to do this in all the languages.

return bot.sendMessage(chatId, data, {
    parse_mode: 'HTML',
    reply_markup: { remove_keyboard: true },
});
Bouse answered 16/9, 2020 at 19:6 Comment(4)
So what is contained in the "data" var?Misogamy
@Misogamy its the string containing message.Bouse
[...] without sending a message [...] so not a solutionMisogamy
Just send it with some other message (the message which is triggering the keyboard) which will trigger the turn off of the keyboard.Bouse
C
-1

When you create a keyboard, you use a sendMessage. Save the message_id from response. And, then, to remove the keyboard, delete the message by calling deleteMessage(message_id)

Chaplin answered 18/4, 2022 at 16:40 Comment(1)
This is not reliable. For example, you may lose your state if the bot is restarted or updated or something weird happened. So you need to clean up the previous keyboard state when you do not know the previous state of the chat.Unweave

© 2022 - 2024 — McMap. All rights reserved.