Add method to .delete_messages() (#282)

This commit is contained in:
Joscha Götzer 2017-10-02 18:59:29 +02:00 committed by Lonami
parent a6099f92bf
commit 6c375a7aed
2 changed files with 52 additions and 8 deletions

View File

@ -1,10 +1,9 @@
import os import os
import re
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import lru_cache from functools import lru_cache
from mimetypes import guess_type from mimetypes import guess_type
import re
try: try:
import socks import socks
except ImportError: except ImportError:
@ -32,6 +31,10 @@ from .tl.functions.messages import (
GetDialogsRequest, GetHistoryRequest, ReadHistoryRequest, SendMediaRequest, GetDialogsRequest, GetHistoryRequest, ReadHistoryRequest, SendMediaRequest,
SendMessageRequest, GetChatsRequest SendMessageRequest, GetChatsRequest
) )
from .tl.functions import channels
from .tl.functions import messages
from .tl.functions.users import ( from .tl.functions.users import (
GetUsersRequest GetUsersRequest
) )
@ -348,6 +351,39 @@ class TelegramClient(TelegramBareClient):
return None # Should not happen return None # Should not happen
def delete_messages(self, entity, message_ids, revoke=True):
"""
Deletes a message from a chat, optionally "for everyone" with argument
`revoke` set to `True`.
The `revoke` argument has no effect for Channels and Supergroups,
where it inherently behaves as being `True`.
Note: The `entity` argument can be `None` for normal chats, but it's
mandatory to delete messages from Channels and Supergroups. It is also
possible to supply a chat_id which will be automatically resolved to
the right type of InputPeer.
:param entity: ID or Entity of the chat
:param list message_ids: ID(s) or `Message` object(s) of the message(s) to delete
:param revoke: Delete the message for everyone or just this client
:returns .messages.AffectedMessages: Messages affected by deletion.
"""
if not isinstance(message_ids, list):
message_ids = [message_ids]
message_ids = [m.id if isinstance(m, Message) else int(m) for m in message_ids]
if entity is None:
return self(messages.DeleteMessagesRequest(message_ids, revoke=revoke))
entity = self.get_input_entity(entity)
if isinstance(entity, InputPeerChannel):
return self(channels.DeleteMessagesRequest(entity, message_ids))
else:
return self(messages.DeleteMessagesRequest(message_ids, revoke=revoke))
def get_message_history(self, def get_message_history(self,
entity, entity,
limit=20, limit=20,

View File

@ -94,11 +94,11 @@ class InteractiveTelegramClient(TelegramClient):
# Enter a while loop to chat as long as the user wants # Enter a while loop to chat as long as the user wants
while True: while True:
# Retrieve the top dialogs # Retrieve the top dialogs
dialog_count = 10 dialog_count = 15
# Entities represent the user, chat or channel # Entities represent the user, chat or channel
# corresponding to the dialog on the same index # corresponding to the dialog on the same index
dialogs, entities = self.get_dialogs(dialog_count) dialogs, entities = self.get_dialogs(limit=dialog_count)
i = None i = None
while i is None: while i is None:
@ -141,6 +141,7 @@ class InteractiveTelegramClient(TelegramClient):
print(' !h: prints the latest messages (message History).') print(' !h: prints the latest messages (message History).')
print(' !up <path>: Uploads and sends the Photo from path.') print(' !up <path>: Uploads and sends the Photo from path.')
print(' !uf <path>: Uploads and sends the File from path.') print(' !uf <path>: Uploads and sends the File from path.')
print(' !d <msg-id>: Deletes a message by its id')
print(' !dm <msg-id>: Downloads the given message Media (if any).') print(' !dm <msg-id>: Downloads the given message Media (if any).')
print(' !dp: Downloads the current dialog Profile picture.') print(' !dp: Downloads the current dialog Profile picture.')
print() print()
@ -206,6 +207,13 @@ class InteractiveTelegramClient(TelegramClient):
# Slice the message to get the path # Slice the message to get the path
self.send_document(path=msg[len('!uf '):], entity=entity) self.send_document(path=msg[len('!uf '):], entity=entity)
# Delete messages
elif msg.startswith('!d '):
# Slice the message to get message ID
deleted_msg = self.delete_messages(entity, msg[len('!d '):])
print('Deleted. {}'.format(deleted_msg))
# Download media # Download media
elif msg.startswith('!dm '): elif msg.startswith('!dm '):
# Slice the message to get message ID # Slice the message to get message ID