Add ttl parameter to send_file

This commit is contained in:
Lonami Exo 2021-09-11 11:02:19 +02:00
parent 3f5f5dbe48
commit 2e1be01ad4
2 changed files with 40 additions and 20 deletions

View File

@ -116,6 +116,7 @@ class UploadMethods:
supports_streaming: bool = False, supports_streaming: bool = False,
schedule: 'hints.DateLike' = None, schedule: 'hints.DateLike' = None,
comment_to: 'typing.Union[int, types.Message]' = None, comment_to: 'typing.Union[int, types.Message]' = None,
ttl: int = None,
**kwargs) -> 'types.Message': **kwargs) -> 'types.Message':
""" """
Sends message with the given file to the specified entity. Sends message with the given file to the specified entity.
@ -273,6 +274,18 @@ class UploadMethods:
This parameter takes precedence over ``reply_to``. If there is This parameter takes precedence over ``reply_to``. If there is
no linked chat, `telethon.errors.sgIdInvalidError` is raised. no linked chat, `telethon.errors.sgIdInvalidError` is raised.
ttl (`int`. optional):
The Time-To-Live of the file (also known as "self-destruct timer"
or "self-destructing media"). If set, files can only be viewed for
a short period of time before they disappear from the message
history automatically.
The value must be at least 1 second, and at most 60 seconds,
otherwise Telegram will ignore this parameter.
Not all types of media can be used with this parameter, such
as text documents, which will fail with ``TtlMediaInvalidError``.
Returns Returns
The `Message <telethon.tl.custom.message.Message>` (or messages) The `Message <telethon.tl.custom.message.Message>` (or messages)
containing the sent file, or messages if a list of them was passed. containing the sent file, or messages if a list of them was passed.
@ -382,7 +395,7 @@ class UploadMethods:
progress_callback=progress_callback, progress_callback=progress_callback,
attributes=attributes, allow_cache=allow_cache, thumb=thumb, attributes=attributes, allow_cache=allow_cache, thumb=thumb,
voice_note=voice_note, video_note=video_note, voice_note=voice_note, video_note=video_note,
supports_streaming=supports_streaming supports_streaming=supports_streaming, ttl=ttl
) )
# e.g. invalid cast from :tl:`MessageMediaWebPage` # e.g. invalid cast from :tl:`MessageMediaWebPage`
@ -402,7 +415,7 @@ class UploadMethods:
progress_callback=None, reply_to=None, progress_callback=None, reply_to=None,
parse_mode=(), silent=None, schedule=None, parse_mode=(), silent=None, schedule=None,
supports_streaming=None, clear_draft=None, supports_streaming=None, clear_draft=None,
force_document=False, background=None): force_document=False, background=None, ttl=None):
"""Specialized version of .send_file for albums""" """Specialized version of .send_file for albums"""
# We don't care if the user wants to avoid cache, we will use it # 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 # anyway. Why? The cached version will be exactly the same thing
@ -432,7 +445,7 @@ class UploadMethods:
# it as media and then convert that to :tl:`InputMediaPhoto`. # it as media and then convert that to :tl:`InputMediaPhoto`.
fh, fm, _ = await self._file_to_media( fh, fm, _ = await self._file_to_media(
file, supports_streaming=supports_streaming, file, supports_streaming=supports_streaming,
force_document=force_document) force_document=force_document, ttl=ttl)
if isinstance(fm, (types.InputMediaUploadedPhoto, types.InputMediaPhotoExternal)): if isinstance(fm, (types.InputMediaUploadedPhoto, types.InputMediaPhotoExternal)):
r = await self(functions.messages.UploadMediaRequest( r = await self(functions.messages.UploadMediaRequest(
entity, media=fm entity, media=fm
@ -654,7 +667,8 @@ class UploadMethods:
self, file, force_document=False, file_size=None, self, file, force_document=False, file_size=None,
progress_callback=None, attributes=None, thumb=None, progress_callback=None, attributes=None, thumb=None,
allow_cache=True, voice_note=False, video_note=False, allow_cache=True, voice_note=False, video_note=False,
supports_streaming=False, mime_type=None, as_image=None): supports_streaming=False, mime_type=None, as_image=None,
ttl=None):
if not file: if not file:
return None, None, None return None, None, None
@ -683,7 +697,8 @@ class UploadMethods:
force_document=force_document, force_document=force_document,
voice_note=voice_note, voice_note=voice_note,
video_note=video_note, video_note=video_note,
supports_streaming=supports_streaming supports_streaming=supports_streaming,
ttl=ttl
), as_image) ), as_image)
except TypeError: except TypeError:
# Can't turn whatever was given into media # Can't turn whatever was given into media
@ -702,13 +717,13 @@ class UploadMethods:
) )
elif re.match('https?://', file): elif re.match('https?://', file):
if as_image: if as_image:
media = types.InputMediaPhotoExternal(file) media = types.InputMediaPhotoExternal(file, ttl_seconds=ttl)
else: else:
media = types.InputMediaDocumentExternal(file) media = types.InputMediaDocumentExternal(file, ttl_seconds=ttl)
else: else:
bot_file = utils.resolve_bot_file_id(file) bot_file = utils.resolve_bot_file_id(file)
if bot_file: if bot_file:
media = utils.get_input_media(bot_file) media = utils.get_input_media(bot_file, ttl=ttl)
if media: if media:
pass # Already have media, don't check the rest pass # Already have media, don't check the rest
@ -718,7 +733,7 @@ class UploadMethods:
'an HTTP URL or a valid bot-API-like file ID'.format(file) 'an HTTP URL or a valid bot-API-like file ID'.format(file)
) )
elif as_image: elif as_image:
media = types.InputMediaUploadedPhoto(file_handle) media = types.InputMediaUploadedPhoto(file_handle, ttl_seconds=ttl)
else: else:
attributes, mime_type = utils.get_attributes( attributes, mime_type = utils.get_attributes(
file, file,
@ -743,7 +758,8 @@ class UploadMethods:
mime_type=mime_type, mime_type=mime_type,
attributes=attributes, attributes=attributes,
thumb=thumb, thumb=thumb,
force_file=force_document and not is_image force_file=force_document and not is_image,
ttl_seconds=ttl
) )
return file_handle, media, as_image return file_handle, media, as_image

View File

@ -429,7 +429,8 @@ def get_input_geo(geo):
def get_input_media( def get_input_media(
media, *, media, *,
is_photo=False, attributes=None, force_document=False, is_photo=False, attributes=None, force_document=False,
voice_note=False, video_note=False, supports_streaming=False voice_note=False, video_note=False, supports_streaming=False,
ttl=None
): ):
""" """
Similar to :meth:`get_input_peer`, but for media. Similar to :meth:`get_input_peer`, but for media.
@ -442,37 +443,39 @@ def get_input_media(
if media.SUBCLASS_OF_ID == 0xfaf846f4: # crc32(b'InputMedia') if media.SUBCLASS_OF_ID == 0xfaf846f4: # crc32(b'InputMedia')
return media return media
elif media.SUBCLASS_OF_ID == 0x846363e0: # crc32(b'InputPhoto') elif media.SUBCLASS_OF_ID == 0x846363e0: # crc32(b'InputPhoto')
return types.InputMediaPhoto(media) return types.InputMediaPhoto(media, ttl_seconds=ttl)
elif media.SUBCLASS_OF_ID == 0xf33fdb68: # crc32(b'InputDocument') elif media.SUBCLASS_OF_ID == 0xf33fdb68: # crc32(b'InputDocument')
return types.InputMediaDocument(media) return types.InputMediaDocument(media, ttl_seconds=ttl)
except AttributeError: except AttributeError:
_raise_cast_fail(media, 'InputMedia') _raise_cast_fail(media, 'InputMedia')
if isinstance(media, types.MessageMediaPhoto): if isinstance(media, types.MessageMediaPhoto):
return types.InputMediaPhoto( return types.InputMediaPhoto(
id=get_input_photo(media.photo), id=get_input_photo(media.photo),
ttl_seconds=media.ttl_seconds ttl_seconds=ttl or media.ttl_seconds
) )
if isinstance(media, (types.Photo, types.photos.Photo, types.PhotoEmpty)): if isinstance(media, (types.Photo, types.photos.Photo, types.PhotoEmpty)):
return types.InputMediaPhoto( return types.InputMediaPhoto(
id=get_input_photo(media) id=get_input_photo(media),
ttl_seconds=ttl
) )
if isinstance(media, types.MessageMediaDocument): if isinstance(media, types.MessageMediaDocument):
return types.InputMediaDocument( return types.InputMediaDocument(
id=get_input_document(media.document), id=get_input_document(media.document),
ttl_seconds=media.ttl_seconds ttl_seconds=ttl or media.ttl_seconds
) )
if isinstance(media, (types.Document, types.DocumentEmpty)): if isinstance(media, (types.Document, types.DocumentEmpty)):
return types.InputMediaDocument( return types.InputMediaDocument(
id=get_input_document(media) id=get_input_document(media),
ttl_seconds=ttl
) )
if isinstance(media, (types.InputFile, types.InputFileBig)): if isinstance(media, (types.InputFile, types.InputFileBig)):
if is_photo: if is_photo:
return types.InputMediaUploadedPhoto(file=media) return types.InputMediaUploadedPhoto(file=media, ttl_seconds=ttl)
else: else:
attrs, mime = get_attributes( attrs, mime = get_attributes(
media, media,
@ -483,7 +486,8 @@ def get_input_media(
supports_streaming=supports_streaming supports_streaming=supports_streaming
) )
return types.InputMediaUploadedDocument( return types.InputMediaUploadedDocument(
file=media, mime_type=mime, attributes=attrs, force_file=force_document) file=media, mime_type=mime, attributes=attrs, force_file=force_document,
ttl_seconds=ttl)
if isinstance(media, types.MessageMediaGame): if isinstance(media, types.MessageMediaGame):
return types.InputMediaGame(id=types.InputGameID( return types.InputMediaGame(id=types.InputGameID(
@ -522,7 +526,7 @@ def get_input_media(
return types.InputMediaEmpty() return types.InputMediaEmpty()
if isinstance(media, types.Message): if isinstance(media, types.Message):
return get_input_media(media.media, is_photo=is_photo) return get_input_media(media.media, is_photo=is_photo, ttl=ttl)
if isinstance(media, types.MessageMediaPoll): if isinstance(media, types.MessageMediaPoll):
if media.poll.quiz: if media.poll.quiz: