Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user_id of a phone number in telegram

Tags:

Im creating a bot to send message to multiple contact in Telegram. user can share contact with bot. then i use getUpdates and obtain the phone number. For using the sendMessage Method i need the chat_id. now how to get chat_id of a user with his phone number? Is there any way better than this method to do this?

like image 795
defectivepixel Avatar asked Jan 16 '17 23:01

defectivepixel


People also ask

How can I find someone by location in Telegram?

Armed with the three locations, an attacker can then open Google Earth Pro, plug in the spoofed locations, and use a ruler to find the middle point between the three. “The intersection of the three circles is the location of the user,” Hassan explained.

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

It's possible with bots 2.0 check out bot api docs. 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.


1 Answers

For internet services to stay alive, they need to fight with any type of spam. As the result, Telegram bots can't start sending messages to users that didn't start using the bot yet!

Only when a user starts using the bot you can see his/hers chat_id and send messages to him.

That being said, if a user (User A) sends you a contact details of another user (Let's call this one, User B), you can see user_id of the user B. However you still can't send any messages directly, unless he/she also started using the bot before.

This behavior allows us to make a workaround and query users by phone number or at least confirm a user's phone number if required.

To do so, first, you need to create contact message. It doesn't matter who sents the message, even the bot can send the message. The idea is to let Telegram fill the user_id of the new contact message. You can read about this object here: Contact Object

The method we need to use is sendContact and it needs a target chat_id, a first_name and a phone_number. Now first_name can be any string and its value is irrelevant to the process. Also, chat_id can be the identification of any user's chat with the bot, even yours. It can also be the name of a group or a channel in which the bot is an administrator with write access, for example, @my_private_bot_channel. In short, anywhere that bot can post a message. So you only need to provide the phone_number.

After sending the message, you will get a server response, along with the Message that your bot just posted. The contact field of this newly created message contains information about the user you just shared his contact, possibly along with his/hers telegram user_id which is the same thing as the user's chat id.

Right afterward you can delete your message with the deleteMessage method.

Following is an example of doing so in plain request/response format, but you should probably use a framework for this:

https://api.telegram.org/{BOT_TOKEN}/sendContact?chat_id=123456789&phone_number=+989123456789&first_name=Some+Random+String

The response to this request is as follow:

{
    "ok": true,
    "result": {
        "message_id": 12345678987654321,
        "from": {
            "id": 987654321,
            "first_name": "The Bot Name",
            "username": "mybot_bot"
        },
        "chat": {
            "id": 123456789,
            "first_name": "Your Chat Party",
            "last_name": "Your Chat Party",
            "type": "private"
        },
        "date": 1484612850,
        "contact": {
            "phone_number": "989123456789",
            "first_name": "Some Random String",
            "user_id": 654789321
        }
    }
}

"user_id": 654789321 is the part that we are interested in. Now you can delete your message.

like image 167
علیرضا Avatar answered Oct 26 '22 13:10

علیرضا