I am using Node.js to work with the telegram-bot-api. Here’s what I aim to achieve:
- Display a keyboard with a button labeled ‘Share my phone number’.
- After the user presses this button, share their contact info and remove the button from view.
Here is the current code:
bot.sendMessage({
text: 'Please share your phone number',
reply_markup: JSON.stringify({
keyboard: [
[{
text: 'Share my phone number',
request_contact: true
}]
],
resize_keyboard: true,
one_time_keyboard: true
})
});
The issues are:
- The button remains visible even after sharing contact info.
- Without the
request_contact flag, the one_time_keyboard hides the button after use, but the user can still bring it back, which is not ideal.
Am I doing something wrong? Any help is appreciated. Thanks.
hey, i had a similar issue. try using the remove_keyboard option after getting the contact. something like this:
bot.on(‘message’, (msg) => {
if (msg.contact) {
bot.sendMessage({
text: ‘thanks!’,
reply_markup: JSON.stringify({
remove_keyboard: true
})
});
}
});
this should hide the keyboard after they share the contact. hope it helps!
I’ve encountered this issue before. The key is to use the ‘remove_keyboard’ option after receiving the contact info. Here’s a solution that worked for me:
Send your initial message with the keyboard as you’re doing. Then, set up a listener for the contact message:
bot.on(‘message’, (msg) => {
if (msg.contact) {
// Process the contact info
bot.sendMessage({
chat_id: msg.chat.id,
text: ‘Thanks for sharing your number!’,
reply_markup: JSON.stringify({
remove_keyboard: true
})
});
}
});
This removes the keyboard once they share their contact. Ensure you’re using the latest API version, as older ones might have issues with this method. Also, consider handling cases where users don’t share their contact - perhaps offer an option to skip or cancel.
I’ve dealt with this exact problem before. The trick is to use the ‘remove_keyboard’ option after you get the contact info. Here’s what worked for me:
First, send your message with the keyboard as you’re doing now. Then, set up a listener for the contact message:
bot.on(‘message’, (msg) => {
if (msg.contact) {
// Do whatever you need with the contact info
bot.sendMessage({
chat_id: msg.chat.id,
text: ‘Thanks for sharing your number!’,
reply_markup: JSON.stringify({
remove_keyboard: true
})
});
}
});
This will remove the keyboard as soon as they share their contact. Just make sure you’re using the latest version of the API, as older versions might have issues with this method. Also, remember to handle cases where the user might not share their contact - you might want to provide an option to skip or cancel.