LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
10.01k stars 1.41k forks source link

`BadRequestError`:`FILE_REFERENCE_\d_EXPIRED` won't be converted to `FileReferenceExpiredError` #3249

Closed Rongronggg9 closed 2 years ago

Rongronggg9 commented 2 years ago

Checklist

Code that causes the issue

Please read the notes below.

# send an album in which media are provided as external URLs
media_list = [InputMediaPhotoExternal(url1), InputMediaPhotoExternal(url2)]
bot.send_message(entity, text, file=media_list)

Traceback

Please read the notes below.

Traceback (most recent call last):
File "/app/src/parsing/post.py", line 159, in send_message
await msg.send(chat_id, reply_to_msg_id)
File "/app/src/message.py", line 74, in send
await self._send(chat_id, reply_to_msg_id)
File "/app/src/message.py", line 142, in _send
await env.bot.send_message(chat_id, self.text,
File "/app/venv/lib/python3.9/site-packages/telethon/client/messages.py", line 800, in send_message
return await self.send_file(
File "/app/venv/lib/python3.9/site-packages/telethon/client/uploads.py", line 362, in send_file
result += await self._send_album(
File "/app/venv/lib/python3.9/site-packages/telethon/client/uploads.py", line 480, in _send_album
result = await self(request)
File "/app/venv/lib/python3.9/site-packages/telethon/client/users.py", line 30, in __call__
return await self._call(self._sender, request, ordered=ordered)
File "/app/venv/lib/python3.9/site-packages/telethon/client/users.py", line 84, in _call
result = await future
telethon.errors.rpcbaseerrors.BadRequestError: RPCError 400: FILE_REFERENCE_1_EXPIRED (caused by SendMultiMediaRequest)

Notes

The error seems to only occur when sending an album. And the number represents which media caused this error, I guess (because I observed that this number can be any number less than the amount of media in the album).

It is hard to reproduce this error in my use case. I wrote an RSS bot that sends media using their URLs (InputMediaPhotoExternal/InputMediaDocumentExternal), instead of downloading them then uploading them. In logs, I found this error several times. However, I could not reproduce it even if I tried to make the bot send exactly the same messages! The error is not listed in the possible errors of messages.sendMultiMedia, and according to the doc from Telegram, the error seems shouldn't occur when sending media as external URLs (because Telegram is the one that will fetch media and send, and there should not be any too long delay between UploadMediaRequest and SendMultiMediaRequest). Do I misunderstand or not? The error itself is not really a problem for me because I can always resend the media in other ways though.

The problem is, the error FILE_REFERENCE_\d_EXPIRED is not converted to a FileReferenceExpiredError, which makes catching it with ease impossible. (catching it in other complicated ways is possible though) In telethon_generator/data/errors.csv (L138), only FILE_REFERENCE_EXPIRED is contained:

FILE_REFERENCE_EXPIRED 400 The file reference has expired and is no longer valid or it belongs to self-destructing media and cannot be resent

Thus in telethon/errors/__init__.py (L28), FILE_REFERENCE_\d_EXPIRED cannot be converted because nothing matched:

cls = rpc_errors_dict.get(rpc_error.error_message.upper(), None)

IDK if this error should be converted by design, maybe it's not only a bug report but also a feature request? :)

Lonami commented 2 years ago

Thanks for the detailed report. Version 2 of the library will offer dynamic errors, so this won't be an issue. This is probably not actionable in v1 because there can't be two classes with the same name (one matching a number and one not).

Rongronggg9 commented 2 years ago

@Lonami

Version 2 of the library will offer dynamic errors

Wow, that's awesome! May I ask about the schedule for releasing v2?

Lonami commented 2 years ago

I have plans to keep on working on it in January, but no specific release date.