I’m working on a Telegram bot where I need users to input their phone numbers. I created a custom keyboard layout with number buttons to make data entry more convenient. However, there’s an issue - every time someone taps a number button, it immediately sends that digit as a message to the chat.
Is there a way to prevent this automatic message behavior? Or maybe there are alternative approaches like triggering the device’s native numeric keypad?
Here’s my current implementation:
number_pad = [['4', '5', '6'], ['7', '8', '9'], ['1', '2', '3'], ['*', '0', '#']]
bot_message = update.message.reply_text(
'Please type your phone number:',
reply_markup=ReplyKeyboardMarkup(keyboard=number_pad)
)
I’m already using the request_contact option as an alternative, but I’d like to explore other solutions too.
Nope, there’s no way around it - Telegram’s reply keyboards always send a message when you press a button. I hit this same issue last year building a number input bot. Here’s what worked for me: keep your custom keyboard but catch those digit messages and delete them right away with bot.delete_message(). Store the digits in a running string, then edit one persistent message to show “Current number: 123…” so users see what they’ve typed without cluttering the chat. You’ll need to add backspace and submit buttons too. It’s messier than inline keyboards, but users get that familiar number pad feel.
u can’t really buffer stuff with ReplyKeyboardMarkup. I’d say just skip the keyboard and let users type it directly, then validate with regex. Much easier than dealing with deleting messages or callbacks for a simple phone number input.
Nope, custom keyboards in Telegram can’t buffer input - every button press sends a message right away. That’s just how the Bot API works and you can’t change it with ReplyKeyboardMarkup. Your best option? Go with inline keyboards and callback queries instead. Swap ReplyKeyboardMarkup for InlineKeyboardMarkup with callback buttons. When users hit buttons, you get callback queries instead of messages. Store the digits in memory and update the current input by editing the message text. Once they’re done entering the number, process the complete phone number instead of having individual digits spam the chat. I’ve done this before and it’s way cleaner. Chat stays organized and you control exactly when to process the phone number.