如何检查 phone 个号码的数据库是否存在 Telegram 信使?

How can I check the database of phone numbers for the presence of the Telegram messenger?

我有我村居民的电话号码 - 如何检查这些号码上是否安装了 Telegram?我用谷歌搜索,但找不到任何解决方案。

这是在 python 中使用 telethon 和普通用户帐户

的一种方法
import random
from telethon import TelegramClient
from telethon import functions, types

api_id = ...
api_hash = ' ... '
client = TelegramClient('session', api_id, api_hash)

async def main():
    phone_number = '+ ... '
    result = await client(functions.contacts.ImportContactsRequest(
        contacts=[types.InputPhoneContact(
            client_id=random.randrange(-2**63, 2**63),
            phone=phone_number,
            first_name='Some Name',
            last_name=''
        )]
    ))

    if len(result.users):
        print(f"{phone_number} has a telegram account")
        await client(functions.contacts.DeleteContactsRequest(result.users))
    else:
        print(f"couldn't find an account for {phone_number}")

client.start()
client.loop.run_until_complete(main())

这里我们尝试添加一个新联系人(通过使用 ImportContactsRequest) with the phone number and if that succeeds we get the users back inside the users field of the result (ImportedContacts),否则是一个空列表。所以我们可以只检查该字段的长度来确定用户是否有电报帐户。

最后我们通过执行DeleteContactsRequest删除新创建的联系人。