【发布时间】:2021-11-17 09:33:24
【问题描述】:
进口:
from telethon import TelegramClient, events, sync
from telethon.tl.types import InputPhoneContact
from telethon import functions, types
代码:
phone_number = "some_friend_phone_no"
contact = InputPhoneContact(client_id=0, phone=phone_number, first_name="first", last_name="last") #enter phone number with settings
contacts = client(functions.contacts.ImportContactsRequest([contact])) #import that number to contacts
if len(contacts.users) > 0: #if user exists on telegram
username = contacts.users[0].username #then insert user value into username variable
if username is not None: #if username is not empty
client(functions.contacts.DeleteContactsRequest(id=[username])) #then DELETE that contact
该代码首先将phone_number 添加到我们的联系人列表中,然后我们可以轻松获取他们的用户名。
在我们删除该联系人后,因为我们只需要他们的用户名。
但是有些用户没有设置用户名,所以我们得到用户名None。
现在如何删除该联系人?
目前我使用client(functions.contacts.DeleteContactsRequest(id=[username])),如果用户名是None会失败。
【问题讨论】: