telethon.errors.rpcerrorlist.FileReferenceExpiredError: The file reference has expired and is no longer valid

47 Views Asked by At

vesions:

python: 3.10.12
telethon: 1.34.0

The below downloads media files from telegram.

import asyncio
import tempfile

from telethon import TelegramClient
from telethon.tl.types import MessageMediaDocument, User


async def download_media(client, dialog):
    total_downloads = 0
    async for message in client.iter_messages(dialog):
        if isinstance(message.media, MessageMediaDocument):
            with tempfile.TemporaryFile('wb') as tmp:
                print(f'\rtotal downloads: {total_downloads}', end='')
                await client.download_media(message, tmp.name)
                process_video(tmp.name)  # time consuming
                total_downloads += 1


async def main():
    api_id = 'API_ID'
    api_hash = 'API_HASH'
    client = TelegramClient('SESSION', api_id, api_hash)
    await client.start(phone='PHONE')
    async for dialog in client.iter_dialogs():
        if isinstance(dialog.entity, User) and dialog.entity.first_name in (
            'NAME1',
            'NAME2',
        ):
            await download_media(client, dialog)


if __name__ == '__main__':
    asyncio.run(main())

It runs successfully and downloads a few files, then after a while, issues start occurring. First, I start getting repeating messages like this:

total downloads: 0
Server sent a very old message with ID 7350958896137580545, ignoring (see FAQ for details)
Server sent a very old message with ID 7350958902767389697, ignoring (see FAQ for details)
Server sent a very old message with ID 7350958906952594433, ignoring (see FAQ for details)
Server sent a very old message with ID 7350959031208322049, ignoring (see FAQ for details)
Server sent a very old message with ID 7350959159667548161, ignoring (see FAQ for details)
Server sent a very old message with ID 7350959289209278465, ignoring (see FAQ for details)
Server sent a very old message with ID 7350959417536965633, ignoring (see FAQ for details)
Server sent a very old message with ID 7350959546499766273, ignoring (see FAQ for details)
Server sent a very old message with ID 7350959676516447233, ignoring (see FAQ for details)
Server sent a very old message with ID 7350963570432817153, ignoring (see FAQ for details)
Security error while unpacking a received message: Too many messages had to be ignored consecutively
Server sent a very old message with ID 7350963636540084225, ignoring (see FAQ for details)
Security error while unpacking a received message: Too many messages had to be ignored consecutively
total downloads: 1

It works for a while, and files are downloaded, then it raises an exception, and keeps raising if downloading the same message is retried.

Traceback (most recent call last):
  File "/download/telegram.py", line 14, in download_video
    await client.download_media(
  File "/usr/local/lib/python3.10/dist-packages/telethon/client/downloads.py", line 422, in download_media
    return await self._download_document(
  File "/usr/local/lib/python3.10/dist-packages/telethon/client/downloads.py", line 895, in _download_document
    result = await self._download_file(
  File "/usr/local/lib/python3.10/dist-packages/telethon/client/downloads.py", line 545, in _download_file
    async for chunk in self._iter_download(
  File "/usr/local/lib/python3.10/dist-packages/telethon/requestiter.py", line 74, in __anext__
    if await self._load_next_chunk():
  File "/usr/local/lib/python3.10/dist-packages/telethon/client/downloads.py", line 66, in _load_next_chunk
    cur = await self._request()
  File "/usr/local/lib/python3.10/dist-packages/telethon/client/downloads.py", line 76, in _request
    result = await self.client._call(self._sender, self.request)
  File "/usr/local/lib/python3.10/dist-packages/telethon/client/users.py", line 87, in _call
    result = await future
telethon.errors.rpcerrorlist.FileReferenceExpiredError: The file reference has expired and is no longer valid or it belongs to self-destructing media and cannot be resent (caused by GetFileRequest)

Edit

I fixed the issue by replacing telethon with telethon.sync client and fetching messages by id based on Lonami's answer.

1

There are 1 best solutions below

2
Lonami On

iter_messages fetches messages in chunks of 100 by default. If you have a lot of messages with videos in a row, and they all take a long time to download, it's possible they will expire.

If this happens, you need to refetch the messages (you can use get_messages with the ids= parameter for those that failed or are missing).

You can also pip install cryptg, which should increase the download speed somewhat and make this error less likely.