получить телефон пользователя

Python Telebot – get user phone number

Hello to all! Actually, the essence of the problem is indicated in the title of the entry: how to get the user’s phone using Python and the Telebot library. Let’s figure it out;)

Below is an example of a working year that I hope will help you 🙂 Suddenly what – feel free to ask questions on mail or in Telegram.

import telebot #Connect the Telebot library - for working with Telegram
from telebot import types #add-ons connected
import config #We connected the Config library, with which we can store the token not in the program code;) but in the config.py file. Important: this file must be in the same directory as the code!
bot = telebot.TeleBot (config.token) # Connect a token
@bot.message_handler (commands = ['number']) # Announced a branch to work on the <strong> number </strong> command
def phone (message):
    keyboard = types.ReplyKeyboardMarkup (row_width = 1, resize_keyboard = True) # Connect the keyboard
    button_phone = types.KeyboardButton (text = "Send phone", request_contact = True) # Specify the name of the button that the user will see
    keyboard.add (button_phone) #Add this button
    bot.send_message (message.chat.id, 'Phone number', reply_markup = keyboard) # Duplicate with a message that the user will now send his phone number to the bot (just in case, but this is not necessary)
@bot.message_handler (content_types = ['contact']) # Announced a branch in which we prescribe logic in case the user decides to send a phone number :)
def contact (message):
    if message.contact is not None: # If the sent object <strong> contact </strong> is not zero
        print (message.contact) # We display the contact information in our panel. But in general, you can save them, or do something else, for example.

As you can see – everything is very, very simple 🙂
Thanks for attention!

P.S. On my YouTube channel, one of the subscribers asked an interesting question: how to hide the keyboard after the phone number has been sent? I didn’t know the answer 🙁 However, a day later, the subscriber provided a solution, which I am now sharing:

keyboard = types.ReplyKeyboardRemove()
bot.send_message(message.chat.id, msg, reply_markup=keyboard)

P.P.S. Not long ago, another question arose: how can the phone number received from a user be sent, for example, to a group in a user-friendly format? It’s quite simple:

Add the bot (the one that should send the received information to the desired group) as an administrator to that group.
In the bot’s code, make the following changes to the def contact(message) function:

@bot.message_handler(content_types=['contact'])
def contact(message):
if message.contact is not None:
print(message.contact)
print(type(message.contact))
print('Name: ' + str(message.contact.first_name))
text = 'User: ' + message.contact.first_name + ': phone: ' + message.contact.phone_number
bot.send_message(id_channel, text)

In fact, you can output anything you want—just use the name of the desired field according to this schema:

'phone_number': '***', 'first_name': '***', 'last_name': '***', 'user_id': ***, 'vcard': '***'

Where the asterisks represent the full contact data fields received from the user.

Support the Blog!

Running a blog takes a lot of effort, time, and passion. Your donations help improve the content, inspire new ideas, and keep the project going.
If you’ve enjoyed the blog’s materials, any support would mean the world to me. Thank you for being here! ❤️

PayPal Logo Donate via PayPal

Revolut Logo Donate via Revolut