9

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.

Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202

4 Answers4

8

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

Maak
  • 4,720
  • 3
  • 28
  • 39
  • Looks like a very simple solution, funny I haven't tried it myself. Thanks, I will give it a try on a first occasion. – Slava Fomin II Jan 07 '18 at 03:14
  • 4
    This does not work, if the message does not have inline keyboards, as per [official documentation](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.` – Andrew Savinykh Jul 30 '22 at 03:48
  • 3
    IDK why this answer has such a high rating. It doesn't work at all. – Viacheslav Dobromyslov Mar 16 '23 at 06:18
  • 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? – MickeyDickey Sep 02 '23 at 22:09
3

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.

dmitry_romanov
  • 5,146
  • 1
  • 33
  • 36
2

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 },
});
R. Gurung
  • 1,356
  • 1
  • 14
  • 34
-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)

tim4dev
  • 2,846
  • 2
  • 24
  • 30
  • 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. – Viacheslav Dobromyslov Mar 16 '23 at 06:14