Reputation: 33
I want to remove a user from a client's contact list.
My aim is to remove a user, which was added to the contact list with a phone number, from a contact list of a client.
I have followed this and this to add a user to a client's contact list. But can't figure out how to remove that user from the contact list.
I have searched for the telethon doc, And I'm sure it's somewhere in there but found nothing related for hours.
The code to add a user to contact list is this.
client = TelegramClient(name, api_id, api_hash)
async def main():
contact = InputPhoneContact(client_id=random.randint(0,9999), phone = "+23xxxxxxxxxx", first_name="fname", last_name="lname")
result = await client(ImportContactsRequest(contacts=[contact]))
with client:
client.loop.run_until_complete(main())
Upvotes: 2
Views: 2485
Reputation: 1237
To do so you need to use the raw API requests directly which can be found here. https://tl.telethon.dev/index.html
for your question, the request is DeleteContactsRequest which can be found at https://tl.telethon.dev/methods/contacts/delete_contacts.html and can be used as such.
from telethon.sync import TelegramClient
from telethon import functions, types
with TelegramClient(name, api_id, api_hash) as client:
result = client(functions.contacts.DeleteContactsRequest(
id=['username']
))
print(result.stringify())
Upvotes: 3