Removing custom keyboard after user interaction in Telegram Bot

I’m working on a Telegram bot using Node.js and I’m stuck with a custom keyboard issue. Here’s what I’m trying to do:

  1. Show a custom keyboard with a ‘Share Phone Number’ button
  2. When clicked, the bot should get the contact info and the keyboard should disappear

My current code looks like this:

bot.sendMessage({
  text: 'Can you share your phone number?',
  reply_markup: JSON.stringify({
    keyboard: [[{
      text: 'Share Phone Number',
      request_contact: true
    }]],
    resize_keyboard: true,
    one_time_keyboard: true
  })
});

But I’m running into some problems:

  • The button stays visible after the user clicks it
  • If I remove request_contact, the keyboard hides but can be brought back

How can I make the keyboard disappear completely after the user shares their contact? Any help would be great!

I’ve faced a similar issue with Telegram bots before, and I found a workaround that might help you out. Instead of relying solely on the one_time_keyboard parameter, you can explicitly remove the keyboard after receiving the contact info.

Here’s what I did:

  1. Set up a listener for the ‘contact’ event.
  2. When the contact is received, send a new message with reply_markup: { remove_keyboard: true }.

Something like this:

bot.on('contact', (msg) => {
  // Process the contact info here
  bot.sendMessage(msg.chat.id, 'Thanks for sharing your number!', {
    reply_markup: {
      remove_keyboard: true
    }
  });
});

This approach ensures the keyboard is removed regardless of the user’s client settings. It’s been reliable in my experience, though it does add an extra message to the conversation. Hope this helps!

hey there! i’ve run into this too. try using the remove_keyboard option after getting the contact. something like:

bot.on(‘contact’, (msg) => {
bot.sendMessage(msg.chat.id, ‘got it!’, {
reply_markup: {
remove_keyboard: true
}
});
});

this should make the keyboard go away for good. good luck!