3

我想使用电话号码发送带有 Telethon 的消息,但它给了我一个电话格式不正确的错误。这是我的代码:

from telethon import TelegramClient
from telethon.tl.types import PeerUser

api_id = 123456
api_hash = 'Something'

client = TelegramClient('Telethon', api_id, api_hash)
client.start()

contact = client.get_entity("+98XXXXXXXXXX")

注意:Python 3.6 版和 Telethon 的最新版本。

4

1 回答 1

2

get_entity仅适用于保存的电话号码。您必须先将电话号码保存在联系人中,然后才能获取用户实体。要保存联系人,您可以执行以下操作:

from telethon.tl.types import InputPhoneContact
from telethon.tl.functions.contacts import ImportContactsRequest

# Here you must connect to your client.
contact = InputPhoneContact(
        client_id=0,
        phone=phone_number,
        first_name="FN",
        last_name="LN"
    ) # For new contacts you should use client_id = 0
    result = client(ImportContactsRequest([contact]))
    try:
        client.get_entity(phone_number)
        print("There is an entity with the phone number")
    except:
        print("There is no such entity")
于 2018-09-16T06:07:46.313 回答