LonamiWebs / Telethon

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

send_file can't handle link with space and round brackets #4464

Closed Orkoliator closed 6 days ago

Orkoliator commented 2 weeks ago

Code that causes the issue

async def handle_start_command(event): message_text, urls = shop_check.get_upcoming_data() chat_id = event.message.peer_id if isinstance(chat_id, (PeerUser, PeerChat, PeerChannel)): await client.send_file(chat_id, urls, caption=message_text)

Expected behavior

Bot should be able to send message with image from url.

Actual behavior

telethon.errors.rpcerrorlist.MediaInvalidError: Media invalid (caused by SendMultiMediaRequest) I think the reason is that there is a space and round brackets in my image url. I've tried to use "from urllib.parse import quote" but just received another error: ValueError: Failed to convert quoted_url to media. Not an existing file, an HTTP URL or a valid bot-API-like file ID

Traceback

Traceback (most recent call last): File "C:\Users\user\tg_bot\tg_bot_venv\Lib\site-packages\telethon\client\updates.py", line 570, in _dispatch_update await callback(event) File "C:\Users\user\tg_bot\main.py", line 70, in handle_start_command await client.send_file(chat_id, urls, caption=message_text) File "C:\Users\user\tg_bot\tg_bot_venv\Lib\site-packages\telethon\client\uploads.py", line 389, in send_file result += await self._send_album( ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\tg_bot\tg_bot_venv\Lib\site-packages\telethon\client\uploads.py", line 504, in _send_album result = await self(request) ^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\tg_bot\tg_bot_venv\Lib\site-packages\telethon\client\users.py", line 30, in call return await self._call(self._sender, request, ordered=ordered) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\tg_bot\tg_bot_venv\Lib\site-packages\telethon\client\users.py", line 87, in _call result = await future ^^^^^^^^^^^^ telethon.errors.rpcerrorlist.MediaInvalidError: Media invalid (caused by SendMultiMediaRequest)

Telethon version

1.36.0

Python version

3.12.6

Operating system (including distribution name and version)

Windows 11

Other details

message_text, urls = shop_check.get_upcoming_data() This method shares string in message_text and list of urls in urls. One of urls happened to have space and round brackets in it.

Checklist

Orkoliator commented 2 weeks ago

link to reproduce the issue: https://cdn1.epicgames.com/salesEvent/salesEvent/EGS_TOEM_SomethingWeMade_S2 (1)_1200x1600-df680f435db3f53c2021e8727e312df7

Lonami commented 1 week ago

I don't plan to look into this any time soon. Though, the URL doesn't have an extension, so perhaps the library simply fails to send the proper information.

You can try appending something like #a.jpg or ?a.jpg. The server shouldn't care, but the library would know the type.

Or you can construct the correct input media directly (don't remember exactly how, but it's somewhere in the code).

Orkoliator commented 6 days ago

The workaround I used is to just download an image with name defined by me and then send it as file.

In my case additional logic to check any link and modify it if it is necessary would be too much. Thanks for answer.