From 9af8ec8ccead54553d77b9c5cc2148fc8d3382cd Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 18 Sep 2021 13:04:13 +0200 Subject: [PATCH] Officially remove bot_file_id support --- readthedocs/misc/v2-migration-guide.rst | 17 +++ telethon/_client/downloads.py | 3 - telethon/_client/telegramclient.py | 4 - telethon/_client/uploads.py | 8 +- telethon/_misc/utils.py | 161 +----------------------- telethon/types/_custom/file.py | 15 --- 6 files changed, 20 insertions(+), 188 deletions(-) diff --git a/readthedocs/misc/v2-migration-guide.rst b/readthedocs/misc/v2-migration-guide.rst index 8bf6ae6a..51f161b5 100644 --- a/readthedocs/misc/v2-migration-guide.rst +++ b/readthedocs/misc/v2-migration-guide.rst @@ -178,6 +178,23 @@ The following modules have been moved inside ``_misc``: // TODO review telethon/__init__.py isn't exposing more than it should +Support for bot-API style file_id has been removed +-------------------------------------------------- + +They have been half-broken for a while now, so this is just making an existing reality official. +See `issue #1613 `__ for details. + +An alternative solution to re-use files may be provided in the future. For the time being, you +should either upload the file as needed, or keep a message with the media somewhere you can +later fetch it (by storing the chat and message identifier). + +Additionally, the ``custom.File.id`` property is gone (which used to provide access to this +"bot-API style" file identifier. + +// TODO could probably provide an in-memory cache for uploads to temporarily reuse old InputFile. +// this should lessen the impact of the removal of this feature + + The custom.Message class and the way it is used has changed ----------------------------------------------------------- diff --git a/telethon/_client/downloads.py b/telethon/_client/downloads.py index 6cd34b10..53e22cc1 100644 --- a/telethon/_client/downloads.py +++ b/telethon/_client/downloads.py @@ -282,9 +282,6 @@ async def download_media( date = datetime.datetime.now() media = message - if isinstance(media, str): - media = utils.resolve_bot_file_id(media) - if isinstance(media, _tl.MessageService): if isinstance(message.action, _tl.MessageActionChatEditPhoto): diff --git a/telethon/_client/telegramclient.py b/telethon/_client/telegramclient.py index 796af317..b83369e9 100644 --- a/telethon/_client/telegramclient.py +++ b/telethon/_client/telegramclient.py @@ -3147,10 +3147,6 @@ class TelegramClient: send the file as "external" media, and Telegram is the one that will fetch the media and send it. - * A Bot API-like ``file_id``. You can convert previously - sent media to file IDs for later reusing with - `telethon.utils.pack_bot_file_id`. - * A handle to an existing file (for example, if you sent a message with media before, you can use its ``message.media`` as a file here). diff --git a/telethon/_client/uploads.py b/telethon/_client/uploads.py index 244c5b59..912954b0 100644 --- a/telethon/_client/uploads.py +++ b/telethon/_client/uploads.py @@ -425,17 +425,13 @@ async def _file_to_media( media = _tl.InputMediaPhotoExternal(file, ttl_seconds=ttl) else: media = _tl.InputMediaDocumentExternal(file, ttl_seconds=ttl) - else: - bot_file = utils.resolve_bot_file_id(file) - if bot_file: - media = utils.get_input_media(bot_file, ttl=ttl) if media: pass # Already have media, don't check the rest elif not file_handle: raise ValueError( - 'Failed to convert {} to media. Not an existing file, ' - 'an HTTP URL or a valid bot-API-like file ID'.format(file) + 'Failed to convert {} to media. Not an existing file or ' + 'HTTP URL'.format(file) ) elif as_image: media = _tl.InputMediaUploadedPhoto(file_handle, ttl_seconds=ttl) diff --git a/telethon/_misc/utils.py b/telethon/_misc/utils.py index 956a154d..8f4667b8 100644 --- a/telethon/_misc/utils.py +++ b/telethon/_misc/utils.py @@ -856,11 +856,7 @@ def is_image(file): """ Returns `True` if the file extension looks like an image file to Telegram. """ - match = re.match(r'\.(png|jpe?g)', _get_extension(file), re.IGNORECASE) - if match: - return True - else: - return isinstance(resolve_bot_file_id(file), _tl.Photo) + return bool(re.match(r'\.(png|jpe?g)', _get_extension(file), re.IGNORECASE)) def is_gif(file): @@ -1119,161 +1115,6 @@ def _encode_telegram_base64(string): return None # not valid base64, not valid ascii, not a string -def resolve_bot_file_id(file_id): - """ - Given a Bot API-style `file_id `, - returns the media it represents. If the `file_id ` - is not valid, `None` is returned instead. - - Note that the `file_id ` does not have information - such as image dimensions or file size, so these will be zero if present. - - For thumbnails, the photo ID and hash will always be zero. - """ - data = _rle_decode(_decode_telegram_base64(file_id)) - if not data: - return None - - # This isn't officially documented anywhere, but - # we assume the last byte is some kind of "version". - data, version = data[:-1], data[-1] - if version not in (2, 4): - return None - - if (version == 2 and len(data) == 24) or (version == 4 and len(data) == 25): - if version == 2: - file_type, dc_id, media_id, access_hash = struct.unpack('