Reputation: 609
Imports:
from telethon import TelegramClient, events, sync
from telethon.tl.types import InputPhoneContact
from telethon import functions, types
Code:
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
That code first add an phone_number
to our contacts list and after we can easily get their username.
After we delete that contact as we just needed their username.
But some users don't have set username so we get username as None
.
Now how to remove that contact ?
Currently I use client(functions.contacts.DeleteContactsRequest(id=[username]))
, which will fail if username is None
.
Upvotes: 0
Views: 595
Reputation: 46
According to the docs, you can make an InputUser
and then give it to DeleteContactRequest
.
As you already have the user, you can:
client(functions.contacts.DeleteContactsRequest(id=[contacts.users[0]]))
Upvotes: 2