I have a simple telegram bot with a KeyboardButton that appears when a user presses on “Start”. After the user presses on KeyboardButton a window appears and in that window the user presses a button “Share phone number”. I need to get this phone number but a “Contact” object in response is empty. The module that I use – pyTelegramBotApi.
import telebot
from telebot import types
bot = telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def register(message):
keyboard = types.ReplyKeyboardMarkup(one_time_keyboard=True)
reg_button = types.KeyboardButton(text="Share your phone number", request_contact=True)
keyboard.add(reg_button)
response = bot.send_message(message.chat.id,
"You should share your phone number",
reply_markup=keyboard)
print(response.contact) # response.contact = None here
if __name__ == '__main__':
bot.polling(none_stop=True)
Is that a right way to get a phone number from user? If no, how can i get it?
3
Answers
No it’s wrong. You should define a new
handler
to handle thecontact
.then you should define your
get_contact
function:You should define new handler for contact
You should use handler with filter.
It is not text it is contact.