ytdlbot
ytdlbot copied to clipboard
Handle "editing a deleted message failed" properly
Potential problem
I prefer to delete all messages except video-results in the chat with ytdlbot as soon as possible to avoid a mess.
When I delete a message with "Your task was added to active queue. Processing..."
ytdlbot fails to edit this message and doesn't send the video, even if this situation isn't critical or process-blocking:
ytdlbot logs
[2024-09-26 01:01:01 ytdl_bot.py:589 I] start ['https://www.youtube.com/shorts/JrFTq146vJw']
[youtube] Extracting URL: https://www.youtube.com/shorts/JrFTq146vJw
[youtube] JrFTq146vJw: Downloading webpage
[youtube] JrFTq146vJw: Downloading ios player API JSON
[youtube] JrFTq146vJw: Downloading web creator player API JSON
[youtube] JrFTq146vJw: Downloading m3u8 information
[2024-09-26 01:01:01 database.py:113 I] Setting metrics: video_request
[2024-09-26 01:01:01 database.py:113 I] Setting metrics: cache_miss
[2024-09-26 01:01:01 downloader.py:227 I] Downloading for https://www.youtube.com/shorts/JrFTq146vJw with format bestvideo[ext=mp4][vcodec!*=av01][vcodec!*=vp09]+bestaudio[ext=m4a]/bestvideo+bestaudio
[download] 0.0% of 3.41MiB at 793.77KiB/s ETA 00:04[2024-09-26 01:01:01 downloader.py:236 E] Download failed for bestvideo[ext=mp4][vcodec!*=av01][vcodec!*=vp09]+bestaudio[ext=m4a]/bestvideo+bestaudio - https://www.youtube.com/shorts/JrFTq146vJw, try another way
[2024-09-26 01:01:01 downloader.py:227 I] Downloading for https://www.youtube.com/shorts/JrFTq146vJw with format bestvideo[vcodec^=avc]+bestaudio[acodec^=mp4a]/best[vcodec^=avc]/best
[2024-09-26 01:01:01 downloader.py:227 I] Downloading for https://www.youtube.com/shorts/JrFTq146vJw with format None
[download] 0.1% of 1.66MiB at Unknown B/s ETA Unknown[2024-09-26 01:01:01 downloader.py:236 E] Download failed for None - https://www.youtube.com/shorts/JrFTq146vJw, try another way
[2024-09-26 01:01:01 tasks.py:411 I] Download complete.
[2024-09-26 01:01:01 tasks.py:209 E] Failed to download https://www.youtube.com/shorts/JrFTq146vJw, error: Telegram says: [400 MESSAGE_ID_INVALID] - The message id is invalid (caused by "messages.EditMessage")
[2024-09-26 01:01:01 dispatcher.py:328 E] Telegram says: [400 MESSAGE_ID_INVALID] - The message id is invalid (caused by "messages.EditMessage")
Traceback (most recent call last):
File "/ytdlbot/ytdlbot/tasks.py", line 199, in ytdl_download_entrance
ytdl_normal_download(client, bot_msg, url)
File "/ytdlbot/ytdlbot/tasks.py", line 413, in ytdl_normal_download
bot_msg.edit_text("Download complete. Sending now...")
File "/usr/local/lib/python3.11/site-packages/pyrogram/sync.py", line 78, in async_to_sync_wrap
return asyncio.run_coroutine_threadsafe(coroutine, main_loop).result()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
raise self._exception
File "/usr/local/lib/python3.11/site-packages/pyrogram/types/messages_and_media/message.py", line 3980, in edit_text
return await self._client.edit_message_text(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pyrogram/methods/messages/edit_message_text.py", line 92, in edit_message_text
r = await self.invoke(
^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pyrogram/methods/advanced/invoke.py", line 94, in invoke
r = await session.invoke(
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pyrogram/session/session.py", line 406, in invoke
return await self.send(query, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pyrogram/session/session.py", line 374, in send
RPCError.raise_it(result, type(data))
File "/usr/local/lib/python3.11/site-packages/pyrogram/errors/rpc_error.py", line 91, in raise_it
raise getattr(
pyrogram.errors.exceptions.bad_request_400.MessageIdInvalid: Telegram says: [400 MESSAGE_ID_INVALID] - The message id is invalid (caused by "messages.EditMessage")
ytdl-1 |
During handling of the above exception, another exception occurred:
ytdl-1 |
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/pyrogram/dispatcher.py", line 317, in handler_worker
await self.loop.run_in_executor(
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/ytdlbot/ytdlbot/ytdl_bot.py", line 118, in wrapper
return func(client, message)
^^^^^^^^^^^^^^^^^^^^^
File "/ytdlbot/ytdlbot/ytdl_bot.py", line 634, in download_handler
ytdl_download_entrance(client, bot_msg, url)
File "/ytdlbot/ytdlbot/tasks.py", line 214, in ytdl_download_entrance
bot_msg.edit_text(f"Download failed!❌\n\n`{traceback.format_exc()[-2000:]}`", disable_web_page_preview=True)
File "/usr/local/lib/python3.11/site-packages/pyrogram/sync.py", line 78, in async_to_sync_wrap
return asyncio.run_coroutine_threadsafe(coroutine, main_loop).result()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
raise self._exception
File "/usr/local/lib/python3.11/site-packages/pyrogram/types/messages_and_media/message.py", line 3980, in edit_text
return await self._client.edit_message_text(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pyrogram/methods/messages/edit_message_text.py", line 92, in edit_message_text
r = await self.invoke(
^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pyrogram/methods/advanced/invoke.py", line 94, in invoke
r = await session.invoke(
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pyrogram/session/session.py", line 406, in invoke
return await self.send(query, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pyrogram/session/session.py", line 374, in send
RPCError.raise_it(result, type(data))
File "/usr/local/lib/python3.11/site-packages/pyrogram/errors/rpc_error.py", line 91, in raise_it
raise getattr(
pyrogram.errors.exceptions.bad_request_400.MessageIdInvalid: Telegram says: [400 MESSAGE_ID_INVALID] - The message id is invalid (caused by "messages.EditMessage")
Proposal
I think that all editing messages should be classified into 2 categories:
- Should be ignored if deleted;
- Should be sent with new text again if deleted.
MESSAGE_ID_INVALID during editing a message should NOT stop the bot from downloading and uploading video :)