Can I get a phone number by user id via Telegram Bot API?

103,427

Solution 1

It's possible with bots 2.0 check out bot api docs.

https://core.telegram.org/bots/2-0-intro#locations-and-numbers https://core.telegram.org/bots/api#keyboardbutton

Solution 2

No, unfortunately Telegram Bot API doesn't return phone number. You should either use Telegram API methods instead or ask it explicitly from the user. You cannot get "friends" of a user as well.

You will definitely retrieve the following information:

  1. userid
  2. first_name
  3. content (whatever it is: text, photo, etc.)
  4. date (unixtime)
  5. chat_id

If user configured it, you will also get last_name and username.

Share:
103,427
isxaker
Author by

isxaker

Updated on July 05, 2022

Comments

  • isxaker
    isxaker almost 2 years

    I am using Telegram Bot API for sending instant messages to users. I have installed nuget package. This package is recommend by telegram developers.

    I have created a telegram bot and successfully got access to it by using code. When I send messsage to bot, bot gets some info about sender.

    enter image description here

    I need the phone numbers of users to identify them in our system and send the information back to them.

    My question is Can i get a user phone number by telegramUserId?

    I'm doing it for user convenience. If I could to get a user phone number I should't have to ask for it from the user.

    Now my command like this:

    debt 9811201243
    

    I want

    debt
    
  • azerafati
    azerafati over 8 years
    but if we explicitly ask the user for a phone number how could we make sure he doesn't send his own number and not someone else's?
  • Sergey Ivanov
    Sergey Ivanov over 8 years
    There is no integrated way to do this. One way to verify though, would be to send a code to this phone number and ask him to provide this code in the chat.
  • Groosha
    Groosha about 8 years
    You're wrong. phone numbers are still unavailable to bots unless user explicitly sends it to bot using special command.
  • Groosha
    Groosha about 8 years
    Why unfortunately? This prevents spammers from sending their crap to all users (just like WhatsApp)
  • Danil Pyatnitsev
    Danil Pyatnitsev about 8 years
    you can check what is it. User can send itself phone number and also user can sand a contact. You need to compare user_id of sender of the message and user_id of contact. If it's the same that is real phone number of the user.
  • Groosha
    Groosha about 8 years
    Yes, but user can means, that by default phone numbers are unavailable to bots.That's what I was saying.
  • Danil Pyatnitsev
    Danil Pyatnitsev about 8 years
    Ok it's look like permission request in mobile app. If user grand access to number, bot can do, else - dear user, sorry, but bot can not do this things without phone number. You are right.
  • VSB
    VSB almost 8 years
    @SergeyIvanov Is it possible to find user name, using userid?
  • Sergey Ivanov
    Sergey Ivanov almost 8 years
    To my knowledge, you can get it only if a user interact with your bot via Message content, i.e. you cannot provide user_id and username from Telegram Bot API.
  • cyrus2500
    cyrus2500 over 7 years
  • cyrus2500
    cyrus2500 over 7 years
  • LA_
    LA_ about 7 years
    If I request user's phone with request_contact - can I be sure that this is user's phone? Can not user send manually some other user's phone instead? For ex., by typing it instead of pressing button? Or even by using modified Telegram client?
  • Danil Pyatnitsev
    Danil Pyatnitsev about 7 years
    I think, that you can be sure that it's actual user's number (if using official client), but I can mistake If bot ask for userphone and user answer to this request you, you get special type of message. and if user just send contact to bot, you also have a phone, but you can check, that it's forward user instead of actual user. Just try :)