how to get user_id from phone_number using Telegram Bot API
Asked Answered
C

1

4

I'm currently working on a Telegram Bot that gets user information in Telegram once you know a phone number. The main goal is to get all the user information stored in Telegram for a user, just knowing his/her phone_number.

So, I have tried using the sendContact function as explained in the Telegram Bot API and like this post: How to get user_id of a phone number in telegram

The problem is that if I try to so this sending my own number as a contact to my chat_id, I receive the Contact structure, but if I send from the bot to myself another phone_number (which corresponds to another active Telegram account), I get no user_id in the JSON file. The problem is that I can just get an user_id, if I use the phone_number of the chat_id account, that means that I can't get any information about anyone using his phone number. But I have seen a lot of possible solutions, but none is working for me.

Any help would be appreciated!

I'm using: https://api.telegram.org/bot<BOT TOKEN>/sendContact?chat_id=<MY CHAT ID>&phone_number=<PHONE_NUMBER I WANT TO KNOW USER_ID>&first_name=<RANDOM NAME>

If use use my own chat_id to receive the message and my phone_number it works, whenever I use another phone_number it does not.

Chantell answered 5/7, 2018 at 14:10 Comment(2)
your bot doesn't know your friends' Chat_ID because they are not a user of your bot, instead, you should use core API of telegram to find chat_id of each phone number.Frozen
@Frozen I get your point, but I have been looking for a solution and I still can't find it. So you know that there is a way to find out user information just knowing the phone_number? Any additional help would be appreciated. Thank you!Chantell
F
1

Bot API can't give you chat_id, you should use Core_API of Telegram to do the task. you should use something like below snippet to get the chat_id of a user:

contact = InputPhoneContact(client_id = 0, phone = phone, first_name="a", last_name="")
result = client(ImportContactsRequest([contact]))
usersDic = result.__dict__['users']
if(len(usersDic)>0):
    chatID = usersDic[0].__dict__["id"]

note that I have used Telethon library in the above code, but it somehow demonstrates how to do the process.

Frozen answered 5/7, 2018 at 16:38 Comment(2)
Thank you man! I'll try to implement it right now, I was coding the Bot in PHP, but I guess than I can retrieve the data I want just by using Python as you showed me above. I have never coded in it before but I guess I'll be able to, so thank you for your answer!Chantell
Just be careful, it's a python code that i have written above. But you can do it in any language that has a well-implemented library of Telegram Core_APIs.Frozen

© 2022 - 2024 — McMap. All rights reserved.