From 854c42b7ef25763bb5f14290e32c714164d0ace4 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 3 Mar 2018 23:12:05 +0100 Subject: [PATCH] Add a file= parameter to client.send_message() --- .../extra/basic/working-with-updates.rst | 4 ++++ telethon/telegram_client.py | 21 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/readthedocs/extra/basic/working-with-updates.rst b/readthedocs/extra/basic/working-with-updates.rst index 652f6000..3c57b792 100644 --- a/readthedocs/extra/basic/working-with-updates.rst +++ b/readthedocs/extra/basic/working-with-updates.rst @@ -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 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 the same handler. You can also have a handler work on only specific chats, for example: diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index fbb57d63..a86b257e 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -667,8 +667,8 @@ class TelegramClient(TelegramBareClient): return message, msg_entities - def send_message(self, entity, message, reply_to=None, parse_mode='md', - link_preview=True): + def send_message(self, entity, message='', reply_to=None, parse_mode='md', + link_preview=True, file=None, force_document=False): """ Sends the given message to the specified entity (user/chat/channel). @@ -692,9 +692,26 @@ class TelegramClient(TelegramBareClient): link_preview (:obj:`bool`, optional): 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: 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) if isinstance(message, Message): if (message.media