From e9023043600e3ad15d1d4bef88298360065fc99d Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 11 Jul 2018 10:16:21 +0200 Subject: [PATCH] Expose silent parameter when sending messages --- telethon/client/messages.py | 25 +++++++++++++++++++++---- telethon/client/uploads.py | 19 +++++++++++++------ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/telethon/client/messages.py b/telethon/client/messages.py index 4932639d..62b6ef10 100644 --- a/telethon/client/messages.py +++ b/telethon/client/messages.py @@ -334,7 +334,8 @@ class MessageMethods(UploadMethods, ButtonMethods, MessageParseMethods): async def send_message( self, entity, message='', *, reply_to=None, parse_mode=utils.Default, link_preview=True, file=None, - force_document=False, clear_draft=False, buttons=None): + force_document=False, clear_draft=False, buttons=None, + silent=None): """ Sends the given message to the specified entity (user/chat/channel). @@ -390,6 +391,11 @@ class MessageMethods(UploadMethods, ButtonMethods, MessageParseMethods): you have signed in as a bot. You can also pass your own :tl:`ReplyMarkup` here. + silent (`bool`, optional): + Whether the message should notify people in a broadcast + channel or not. Defaults to ``False``, which means it will + notify them. Set it to ``True`` to alter this behaviour. + Returns: The sent `custom.Message `. """ @@ -431,10 +437,13 @@ class MessageMethods(UploadMethods, ButtonMethods, MessageParseMethods): else: markup = self._build_reply_markup(buttons) + if silent is None: + silent = message.silent + request = functions.messages.SendMessageRequest( peer=entity, message=message.message or '', - silent=message.silent, + silent=silent, reply_to_msg_id=reply_id, reply_markup=markup, entities=message.entities, @@ -453,6 +462,7 @@ class MessageMethods(UploadMethods, ButtonMethods, MessageParseMethods): no_webpage=not link_preview, reply_to_msg_id=utils.get_message_id(reply_to), clear_draft=clear_draft, + silent=silent, reply_markup=self._build_reply_markup(buttons) ) @@ -471,7 +481,8 @@ class MessageMethods(UploadMethods, ButtonMethods, MessageParseMethods): return self._get_response_message(request, result, entity) - async def forward_messages(self, entity, messages, from_peer=None): + async def forward_messages(self, entity, messages, from_peer=None, + *, silent=None): """ Forwards the given message(s) to the specified entity. @@ -487,6 +498,11 @@ class MessageMethods(UploadMethods, ButtonMethods, MessageParseMethods): of the ``Message`` class, this *must* be specified in order for the forward to work. + silent (`bool`, optional): + Whether the message should notify people in a broadcast + channel or not. Defaults to ``False``, which means it will + notify them. Set it to ``True`` to alter this behaviour. + Returns: The list of forwarded `telethon.tl.custom.message.Message`, or a single one if a list wasn't provided as input. @@ -514,7 +530,8 @@ class MessageMethods(UploadMethods, ButtonMethods, MessageParseMethods): req = functions.messages.ForwardMessagesRequest( from_peer=from_peer, id=[m if isinstance(m, int) else m.id for m in messages], - to_peer=entity + to_peer=entity, + silent=silent ) result = await self(req) if isinstance(result, (types.Updates, types.UpdatesCombined)): diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index c987e464..f7f4171d 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -31,7 +31,8 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): self, entity, file, *, caption='', force_document=False, progress_callback=None, reply_to=None, attributes=None, thumb=None, allow_cache=True, parse_mode=utils.Default, - voice_note=False, video_note=False, buttons=None, **kwargs): + voice_note=False, video_note=False, buttons=None, silent=None, + **kwargs): """ Sends a file to the specified entity. @@ -106,6 +107,11 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): you have signed in as a bot. You can also pass your own :tl:`ReplyMarkup` here. + silent (`bool`, optional): + Whether the message should notify people in a broadcast + channel or not. Defaults to ``False``, which means it will + notify them. Set it to ``True`` to alter this behaviour. + Notes: If the ``hachoir3`` package (``hachoir`` module) is installed, it will be used to determine metadata from audio and video files. @@ -134,7 +140,7 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): result += await self._send_album( entity, images[:10], caption=caption, progress_callback=progress_callback, reply_to=reply_to, - parse_mode=parse_mode + parse_mode=parse_mode, silent=silent ) images = images[10:] @@ -144,7 +150,8 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): caption=caption, force_document=force_document, progress_callback=progress_callback, reply_to=reply_to, attributes=attributes, thumb=thumb, voice_note=voice_note, - video_note=video_note, buttons=buttons, **kwargs + video_note=video_note, buttons=buttons, silent=silent, + **kwargs )) return result @@ -170,7 +177,7 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): markup = self._build_reply_markup(buttons) request = functions.messages.SendMediaRequest( entity, media, reply_to_msg_id=reply_to, message=caption, - entities=msg_entities, reply_markup=markup + entities=msg_entities, reply_markup=markup, silent=silent ) msg = self._get_response_message(request, await self(request), entity) self._cache_media(msg, file, file_handle, force_document=force_document) @@ -179,7 +186,7 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): async def _send_album(self, entity, files, caption='', progress_callback=None, reply_to=None, - parse_mode=utils.Default): + parse_mode=utils.Default, silent=None): """Specialized version of .send_file for albums""" # We don't care if the user wants to avoid cache, we will use it # anyway. Why? The cached version will be exactly the same thing @@ -222,7 +229,7 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): # Now we can construct the multi-media request result = await self(functions.messages.SendMultiMediaRequest( - entity, reply_to_msg_id=reply_to, multi_media=media + entity, reply_to_msg_id=reply_to, multi_media=media, silent=silent )) return [ self._get_response_message(update.id, result, entity)