Imports:
JavaScript
x
4
1
from telethon import TelegramClient, events, sync
2
from telethon.tl.types import InputPhoneContact
3
from telethon import functions, types
4
Code:
JavaScript
1
8
1
phone_number = "some_friend_phone_no"
2
contact = InputPhoneContact(client_id=0, phone=phone_number, first_name="first", last_name="last") #enter phone number with settings
3
contacts = client(functions.contacts.ImportContactsRequest([contact])) #import that number to contacts
4
if len(contacts.users) > 0: #if user exists on telegram
5
username = contacts.users[0].username #then insert user value into username variable
6
if username is not None: #if username is not empty
7
client(functions.contacts.DeleteContactsRequest(id=[username])) #then DELETE that contact
8
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
.
Advertisement
Answer
According to the docs, you can make an InputUser
and then give it to DeleteContactRequest
.
As you already have the user, you can:
JavaScript
1
2
1
client(functions.contacts.DeleteContactsRequest(id=[contacts.users[0]]))
2