在 python 中添加与 telethon 的联系

add contact with telethon in python

最近我尝试根据本教程使用telethon在电报中添加联系人: 1) ,我使用了这个代码:

contact = InputPhoneContact(client_id=0, phone='+989122725691', first_name="user",
                        last_name="test")
result = ImportContactsRequest(contacts=[contact])
print(result)

但我在输出中得到了这个:

ImportContactsRequest(contacts=[InputPhoneContact(client_id=0, phone='+989122725691', first_name='user', last_name='test')])

我找不到我的问题所在,但是当我转到我的电报应用程序时,没有添加此联系人。

您应该使用您的客户端实例调用 ImportContactsRequest。 例如

import random
contact = InputPhoneContact(client_id=random.randint(0,9999), phone='+98912******', 
    first_name="user",
    last_name="test")
result = client(ImportContactsRequest(contacts=[contact]))
print(result.__dict__)

作者注

Official applications use random numbers, and we have had issues with it in the past.