如何在电报中获取电话号码的 user_id [英] How to get user_id of a phone number in telegram

查看:56
本文介绍了如何在电报中获取电话号码的 user_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个机器人来向 Telegram 中的多个联系人发送消息.用户可以与机器人共享联系人.然后我使用 getUpdates 并获取电话号码.为了使用sendMessage 方法,我需要chat_id.现在如何通过他的电话号码获取用户的chat_id?有没有比这种方法更好的方法?

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?

推荐答案

为了让 Internet 服务保持活力,他们需要与任何类型的垃圾邮件作斗争.因此,Telegram 机器人无法向尚未开始使用该机器人的用户发送消息!

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!

只有当用户开始使用机器人时,您才能看到他/她的 chat_id 并向他发送消息.

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

话虽如此,如果用户(用户 A)向您发送另一个用户(我们称此用户为用户 B)的联系人详细信息,您可以看到 user_id用户 B.但是你仍然不能直接发送任何消息,除非他/她之前也开始使用机器人.

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.

为此,首先,您需要创建contact 消息.谁发送消息并不重要,即使是机器人也可以发送消息.这个想法是让 Telegram 填充新的 contact 消息的 user_id.您可以在此处阅读有关此对象的信息:联系对象

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

我们需要使用的方法是 sendContact 并且它需要一个目标 chat_idfirst_namephone_number.现在 first_name 可以是任何字符串,它的值与过程无关.此外,chat_id 可以是任何用户与机器人聊天的标识,甚至是你的.它也可以是机器人是具有写访问权限的管理员的组或频道的名称,例如,@my_private_bot_channel.简而言之,机器人可以在任何地方发布消息.所以你只需要提供phone_number.

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.

发送消息后,您将收到服务器响应以及消息你的机器人刚刚发布.这个新创建的消息的 contact 字段包含有关您刚刚分享他的联系人的用户的信息,可能还有他/她的电报 user_id,这与用户的聊天内容相同身份证.

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.

随后您可以使用 deleteMessage 方法删除您的消息.

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

对此请求的响应如下:

{
    "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 是我们感兴趣的部分.现在您可以删除您的消息了.

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

这篇关于如何在电报中获取电话号码的 user_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆