如何使用 Telegram Bot API 从 phone_number 获取 user_id [英] how to get user_id from phone_number using Telegram Bot API

查看:93
本文介绍了如何使用 Telegram Bot API 从 phone_number 获取 user_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 Telegram Bot,一旦您知道电话号码,它就会在 Telegram 中获取用户信息.主要目标是为用户获取存储在 Telegram 中的所有用户信息,只需知道他/她的电话号码即可.

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.

因此,我尝试使用 Telegram Bot API 中解释的 sendContact 函数,并喜欢这篇文章:如何在电报中获取电话号码的 user_id

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

问题是,如果我尝试将自己的号码作为联系人发送到我的 chat_id,我会收到 Contact 结构,但是如果我从机器人向自己发送另一个 phone_number(对应于另一个活动的 Telegram 帐户),我在 JSON 文件中没有得到 user_id.问题是我只能得到一个user_id,如果我使用chat_id 帐户的电话号码,这意味着我无法获得任何使用他电话号码的人的任何信息.但是我看到了很多可能的解决方案,但没有一个对我有用.

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.

任何帮助将不胜感激!

我正在使用:https://api.telegram.org/bot/sendContact?chat_id=<我的聊天ID>&phone_number=&first_name=;

如果使用我自己的 chat_id 来接收消息,而我的 phone_number 可以使用,那么每当我使用另一个 phone_number 时它就不行.

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.

推荐答案

Bot API 不能给你chat_id,你应该使用Telegram 的Core_API 来做这个任务.您应该使用以下代码段来获取用户的 chat_id:

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"]

请注意,我在上面的代码中使用了 Telethon 库,但它以某种方式演示了如何执行该过程.

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

这篇关于如何使用 Telegram Bot API 从 phone_number 获取 user_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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