From 0d64fd98f75315a6255bc3a96441b5a0cb9597e3 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 30 May 2019 13:58:05 +0200 Subject: [PATCH] Create new client.delete_dialog method --- telethon/client/dialogs.py | 49 ++++++++++++++++++++++++++++++++++++ telethon/tl/custom/dialog.py | 15 ++++------- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/telethon/client/dialogs.py b/telethon/client/dialogs.py index 034eeaf4..12fbe13b 100644 --- a/telethon/client/dialogs.py +++ b/telethon/client/dialogs.py @@ -323,6 +323,55 @@ class DialogMethods(UserMethods): for x, y in zip(entities, folder) ])) + async def delete_dialog( + self: 'TelegramClient', + entity: 'hints.EntityLike', + *, + revoke: bool = False + ): + """ + Deletes a dialog (leaves a chat or channel). + + See also `Dialog.delete() `. + + Arguments + entity (entities): + The entity of the dialog to delete. If it's a chat or + channel, you will leave it. Note that the chat itself + is not deleted, only the dialog, because you left it. + + revoke (`bool`, optional): + On private chats, you may revoke the messages from + the other peer too. By default, it's ``False``. Set + it to ``True`` to delete the history for both. + + Returns + The :tl:`Updates` object that the request produces, + or nothing for private conversations. + + Example + .. code-block:: python + + # Deleting the first dialog + dialogs = client.get_dialogs(5) + client.delete_dialog(dialogs[0]) + + # Leaving a channel by username + client.delete_dialog('username') + """ + entity = await self.get_input_entity(entity) + if isinstance(entity, types.InputPeerChannel): + return await self(functions.channels.LeaveChannelRequest(entity)) + + if isinstance(entity, types.InputPeerChat): + result = await self(functions.messages.DeleteChatUserRequest( + entity.chat_id, types.InputUserSelf())) + else: + result = None + + await self(functions.messages.DeleteHistoryRequest(entity, 0, revoke=revoke)) + return result + def conversation( self: 'TelegramClient', entity: 'hints.EntityLike', diff --git a/telethon/tl/custom/dialog.py b/telethon/tl/custom/dialog.py index 99d01ebf..10510052 100644 --- a/telethon/tl/custom/dialog.py +++ b/telethon/tl/custom/dialog.py @@ -104,20 +104,15 @@ class Dialog: return await self._client.send_message( self.input_entity, *args, **kwargs) - async def delete(self): + async def delete(self, revoke=False): """ Deletes the dialog from your dialog list. If you own the channel this won't destroy it, only delete it from the list. + + Shorthand for `telethon.client.dialogs.DialogMethods.delete_dialog` + with ``entity`` already set. """ - if self.is_channel: - await self._client(functions.channels.LeaveChannelRequest( - self.input_entity)) - else: - if self.is_group: - await self._client(functions.messages.DeleteChatUserRequest( - self.entity.id, types.InputPeerSelf())) - await self._client(functions.messages.DeleteHistoryRequest( - self.input_entity, 0)) + await self._client.delete_dialog(self.input_entity, revoke=revoke) async def archive(self, folder=1): """