Add an optional reply_to parameter to .send_message/media

This commit is contained in:
Lonami Exo 2017-09-13 11:51:23 +02:00
parent 7043f4c9ba
commit d12fb5b924

View File

@ -408,18 +408,20 @@ class TelegramClient(TelegramBareClient):
def send_message(self, def send_message(self,
entity, entity,
message, message,
reply_to=None,
link_preview=True): link_preview=True):
"""Sends a message to the given entity (or input peer) """Sends a message to the given entity (or input peer)
and returns the sent message ID. and returns the sent message ID.
The entity may be a phone or an username at the expense of If 'reply_to' is set to either a message or a message ID,
some performance loss. the sent message will be replying to such message.
""" """
request = SendMessageRequest( request = SendMessageRequest(
peer=self.get_entity(entity), peer=self.get_entity(entity),
message=message, message=message,
entities=[], entities=[],
no_webpage=not link_preview no_webpage=not link_preview,
reply_to_msg_id=self._get_reply_to(reply_to)
) )
result = self(request) result = self(request)
return request.random_id return request.random_id
@ -500,12 +502,29 @@ class TelegramClient(TelegramBareClient):
max_id=max_id max_id=max_id
)) ))
@staticmethod
def _get_reply_to(reply_to):
"""Sanitizes the 'reply_to' parameter a user may send"""
if reply_to is None:
return None
if isinstance(reply_to, int):
return reply_to
if isinstance(reply_to, TLObject) and \
type(reply_to).subclass_of_id == 0x790009e3:
# hex(crc32(b'Message')) = 0x790009e3
return reply_to.id
raise ValueError('Invalid reply_to type: ', type(reply_to))
# endregion # endregion
# region Uploading files # region Uploading files
def send_file(self, entity, file, caption='', def send_file(self, entity, file, caption='',
force_document=False, progress_callback=None, force_document=False, progress_callback=None,
reply_to=None,
**kwargs): **kwargs):
"""Sends a file to the specified entity. """Sends a file to the specified entity.
The file may either be a path, a byte array, or a stream. The file may either be a path, a byte array, or a stream.
@ -523,7 +542,9 @@ class TelegramClient(TelegramBareClient):
If "progress_callback" is not None, it should be a function that If "progress_callback" is not None, it should be a function that
takes two parameters, (bytes_uploaded, total_bytes). takes two parameters, (bytes_uploaded, total_bytes).
If 'is_voice_note' in kwargs, despite its value, and the file is The "reply_to" parameter works exactly as the one on .send_message.
If "is_voice_note" in kwargs, despite its value, and the file is
sent as a document, it will be sent as a voice note. sent as a document, it will be sent as a voice note.
The entity may be a phone or an username at the expense of The entity may be a phone or an username at the expense of
@ -581,13 +602,16 @@ class TelegramClient(TelegramBareClient):
# send the media message to the desired entity. # send the media message to the desired entity.
self(SendMediaRequest( self(SendMediaRequest(
peer=self.get_entity(entity), peer=self.get_entity(entity),
media=media media=media,
reply_to_msg_id=self._get_reply_to(reply_to)
)) ))
def send_voice_note(self, entity, file, caption='', upload_progress=None): def send_voice_note(self, entity, file, caption='', upload_progress=None,
reply_to=None):
"""Wrapper method around .send_file() with is_voice_note=()""" """Wrapper method around .send_file() with is_voice_note=()"""
return self.send_file(entity, file, caption, return self.send_file(entity, file, caption,
upload_progress=upload_progress, upload_progress=upload_progress,
reply_to=reply_to,
is_voice_note=()) # empty tuple is enough is_voice_note=()) # empty tuple is enough
def clear_file_cache(self): def clear_file_cache(self):