How to hide ReplyKeyboardMarkup after user click in Telegram Bot API
Asked Answered
H

3

17

I am using Node.js telegram-bot-api.

Idea:

  1. Show a custom keyboard with one button - "Share my phone number".
  2. When user clicks this button, contact should be sent and button should be removed from screen.

Here is a code I am using right now:

bot.sendMessage({
    text: 'Please give us your phone number',
    reply_markup: JSON.stringify({
        keyboard: [
            [{
                text: 'Share my phone number',
                request_contact: true
            }]
        ],
        resize_keyboard: true,
        one_time_keyboard: true
    })
});

Problems:

  • When user clicks "Share my phone number" button, it shares his contact but button is visible even after that.
  • When I am not using request_contact flag, one_time_keyboard works correctly (hides the button after its usage), but even in that case it just hides the button, so user can click an icon to bring it back to screen, which is not good at all.

Please tell me if I am doing something wrong here. Thanks

Holozoic answered 1/8, 2016 at 10:41 Comment(0)
H
25

Found it.

Here is a solution:

bot.sendMessage({
    chat_id: message.chat.id,
    text: 'Some text...',
    reply_markup: JSON.stringify({
        hide_keyboard: true
    })
});
Holozoic answered 1/8, 2016 at 12:10 Comment(2)
hide_keyboard has been renamed to remove_keyboard since API 2.3.Ladyfinger
Notice it seems not to work if you send and empty message or even only 1 space.Spousal
L
10

hide_keyboard has been renamed to remove_keyboard since API 2.3.

bot.sendMessage({
    chat_id: message.chat.id,
    text: 'Some text...',
    reply_markup: JSON.stringify({
        remove_keyboard: true
    })
});
Ladyfinger answered 27/7, 2018 at 7:58 Comment(0)
W
0

You should use editMessageReplyMarkup and update that message's replyMarkup with null string('') after client user's click recieved.

UPDATE this is applicable for inline keyboards.

Whetstone answered 1/8, 2016 at 11:24 Comment(2)
here is what I am getting now: { ok: false, error_code: 400, description: 'Bad Request: Message can\'t be edited' }Holozoic
you should provide message id too. Last Message Id that you have sent and have contained the keyboard markupWhetstone

© 2022 - 2024 — McMap. All rights reserved.