Skip to content

Commit

Permalink
Allowing nosound_video to be specified (#4090)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deer-Spangle committed Apr 14, 2023
1 parent 7b1b33f commit 6e7423e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
15 changes: 13 additions & 2 deletions telethon/client/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,8 @@ async def send_message(
background: bool = None,
supports_streaming: bool = False,
schedule: 'hints.DateLike' = None,
comment_to: 'typing.Union[int, types.Message]' = None
comment_to: 'typing.Union[int, types.Message]' = None,
nosound_video: bool = None,
) -> 'types.Message':
"""
Sends a message to the specified user, chat or channel.
Expand Down Expand Up @@ -754,6 +755,15 @@ async def send_message(
This parameter takes precedence over ``reply_to``. If there is
no linked chat, `telethon.errors.sgIdInvalidError` is raised.
nosound_video (`bool`, optional):
Only applicable when sending a video file without an audio
track. If set to ``True``, the video will be displayed in
Telegram as a video. If set to ``False``, Telegram will attempt
to display the video as an animated gif. (It may still display
as a video due to other factors.) The value is ignored if set
on non-video files. This is set to ``True`` for albums, as gifs
cannot be sent in albums.
Returns
The sent `custom.Message <telethon.tl.custom.message.Message>`.
Expand Down Expand Up @@ -821,7 +831,8 @@ async def callback(event):
buttons=buttons, clear_draft=clear_draft, silent=silent,
schedule=schedule, supports_streaming=supports_streaming,
formatting_entities=formatting_entities,
comment_to=comment_to, background=background
comment_to=comment_to, background=background,
nosound_video=nosound_video,
)

entity = await self.get_input_entity(entity)
Expand Down
21 changes: 16 additions & 5 deletions telethon/client/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ async def send_file(
schedule: 'hints.DateLike' = None,
comment_to: 'typing.Union[int, types.Message]' = None,
ttl: int = None,
nosound_video: bool = None,
**kwargs) -> 'types.Message':
"""
Sends message with the given file to the specified entity.
Expand Down Expand Up @@ -286,6 +287,15 @@ async def send_file(
Not all types of media can be used with this parameter, such
as text documents, which will fail with ``TtlMediaInvalidError``.
nosound_video (`bool`, optional):
Only applicable when sending a video file without an audio
track. If set to ``True``, the video will be displayed in
Telegram as a video. If set to ``False``, Telegram will attempt
to display the video as an animated gif. (It may still display
as a video due to other factors.) The value is ignored if set
on non-video files. This is set to ``True`` for albums, as gifs
cannot be sent in albums.
Returns
The `Message <telethon.tl.custom.message.Message>` (or messages)
containing the sent file, or messages if a list of them was passed.
Expand Down Expand Up @@ -389,7 +399,8 @@ def callback(current, total):
progress_callback=progress_callback,
attributes=attributes, allow_cache=allow_cache, thumb=thumb,
voice_note=voice_note, video_note=video_note,
supports_streaming=supports_streaming, ttl=ttl
supports_streaming=supports_streaming, ttl=ttl,
nosound_video=nosound_video,
)

# e.g. invalid cast from :tl:`MessageMediaWebPage`
Expand Down Expand Up @@ -439,13 +450,13 @@ async def _send_album(self: 'TelegramClient', entity, files, caption='',
media = []
for sent_count, file in enumerate(files):
# Albums want :tl:`InputMedia` which, in theory, includes
# :tl:`InputMediaUploadedPhoto`. However using that will
# :tl:`InputMediaUploadedPhoto`. However, using that will
# make it `raise MediaInvalidError`, so we need to upload
# it as media and then convert that to :tl:`InputMediaPhoto`.
fh, fm, _ = await self._file_to_media(
file, supports_streaming=supports_streaming,
force_document=force_document, ttl=ttl,
progress_callback=used_callback)
progress_callback=used_callback, nosound_video=True)
if isinstance(fm, (types.InputMediaUploadedPhoto, types.InputMediaPhotoExternal)):
r = await self(functions.messages.UploadMediaRequest(
entity, media=fm
Expand Down Expand Up @@ -675,7 +686,7 @@ async def _file_to_media(
progress_callback=None, attributes=None, thumb=None,
allow_cache=True, voice_note=False, video_note=False,
supports_streaming=False, mime_type=None, as_image=None,
ttl=None):
ttl=None, nosound_video=None):
if not file:
return None, None, None

Expand Down Expand Up @@ -762,7 +773,7 @@ async def _file_to_media(

# setting `nosound_video` to `True` doesn't affect videos with sound
# instead it prevents sending silent videos as GIFs
nosound_video = True if mime_type.split("/")[0] == 'video' else None
nosound_video = nosound_video if mime_type.split("/")[0] == 'video' else None

media = types.InputMediaUploadedDocument(
file=file_handle,
Expand Down
2 changes: 1 addition & 1 deletion telethon/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Versions should comply with PEP440.
# This line is parsed in setup.py:
__version__ = '1.28.4'
__version__ = '1.28.5'

0 comments on commit 6e7423e

Please sign in to comment.