Add a file= parameter to client.send_message()

This commit is contained in:
Lonami Exo 2018-03-03 23:12:05 +01:00
parent 0f72aa8f94
commit 854c42b7ef
2 changed files with 23 additions and 2 deletions

View File

@ -99,6 +99,10 @@ done! The event that will be passed always is of type ``XYZ.Event`` (for
instance, ``NewMessage.Event``), except for the ``Raw`` event which just instance, ``NewMessage.Event``), except for the ``Raw`` event which just
passes the ``Update`` object. passes the ``Update`` object.
Note that ``.reply()`` and ``.respond()`` are just wrappers around the
``client.send_message()`` method which supports the ``file=`` parameter.
This means you can reply with a photo if you do ``client.reply(file=photo)``.
You can put the same event on many handlers, and even different events on You can put the same event on many handlers, and even different events on
the same handler. You can also have a handler work on only specific chats, the same handler. You can also have a handler work on only specific chats,
for example: for example:

View File

@ -667,8 +667,8 @@ class TelegramClient(TelegramBareClient):
return message, msg_entities return message, msg_entities
def send_message(self, entity, message, reply_to=None, parse_mode='md', def send_message(self, entity, message='', reply_to=None, parse_mode='md',
link_preview=True): link_preview=True, file=None, force_document=False):
""" """
Sends the given message to the specified entity (user/chat/channel). Sends the given message to the specified entity (user/chat/channel).
@ -692,9 +692,26 @@ class TelegramClient(TelegramBareClient):
link_preview (:obj:`bool`, optional): link_preview (:obj:`bool`, optional):
Should the link preview be shown? Should the link preview be shown?
file (:obj:`file`, optional):
Sends a message with a file attached (e.g. a photo,
video, audio or document). The ``message`` may be empty.
force_document (:obj:`bool`, optional):
Whether to send the given file as a document or not.
Returns: Returns:
the sent message the sent message
""" """
if file is not None:
return self.send_file(
entity, file, caption=message, reply_to=reply_to,
parse_mode=parse_mode, force_document=force_document
)
elif not message:
raise ValueError(
'The message cannot be empty unless a file is provided'
)
entity = self.get_input_entity(entity) entity = self.get_input_entity(entity)
if isinstance(message, Message): if isinstance(message, Message):
if (message.media if (message.media