Skip to content

Fix: Add thumbnail support for edit_message_media #2471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025

Conversation

skyblade-x
Copy link

Description:

By Telegram specification, thumbnails can be used in the following methods:

  • InputMediaAnimation
  • InputMediaDocument
  • InputMediaAudio
  • InputMediaVideo

When using send_media_group, everything works correctly. For example:

bot.send_media_group(
    chat_id=CHAT_ID,
    media=[InputMediaAudio(
        media=InputFile(file=audio_path), 
        thumbnail=InputFile(file=thumbnail_path)
    )],
)

Result:
image

Audio is successfully sent with a thumbnail.


However, currently edit_message_media does not support sending thumbnails.

Example with edit_message_media:

initial_message = bot.send_message(CHAT_ID, "Тестовое сообщение.")

  bot.edit_message_media(
     chat_id=CHAT_ID,
     message_id=initial_message.message_id,
     media=InputMediaAudio(
         media=InputFile(file=audio_path),
         thumbnail=InputFile(file=thumbnail_path)
     )
 )

This throws an exception:
image-1

The issue is caused because we try to JSON serialize a raw file, but according to Telegram's specification, the thumbnail should be sent using attach://<file_attach_name>, not serialized directly.

Relevant part of the spec:

"The thumbnail should be in JPEG format and less than 200 kB in size. Thumbnails can't be reused and must be uploaded as a new file, so you can pass attach://<file_attach_name> if the thumbnail was uploaded using multipart/form-data."


Why this fix is needed:
Without this fix, it is impossible to send or edit media messages with a thumbnail via edit_message_media, even though Telegram's API supports it.

Polite closing:
Please review this PR. Feedback is welcome!

@coder2020official
Copy link
Collaborator

Hi, thanks for the PR, I will review it

@Badiboy
Copy link
Collaborator

Badiboy commented Apr 19, 2025

We'll check later after uprade to API 9.0

@Badiboy
Copy link
Collaborator

Badiboy commented May 3, 2025

@skyblade-x Any plans to fix or close the PR?

@skyblade-x
Copy link
Author

Hi @Badiboy,

Apologies for the delay in responding — I didn’t mean to leave the PR hanging. Thank you for your patience and your feedback.

I’ve now made the requested changes, including aligning the thumbnail check logic with media and handling the optional _thumbnail_name as suggested. I really appreciate the clarity of your review — it helped me understand the style and expectations of the project better.

I'm glad to contribute and happy to make any further improvements if needed. Thanks again for the opportunity to work on this!

@skyblade-x
Copy link
Author

@Badiboy, I have fixed your first review, and in my opinion, everything will work as expected.
изображение
The condition will only set the media if it has a non-empty string value: media_dict[self._thumbnail_name], only by executing the following code:
изображение

@Badiboy
Copy link
Collaborator

Badiboy commented May 6, 2025

The condition will only set the media if it has a non-empty string value: media_dict[self._thumbnail_name],

Yep, sorry, I missed this condition.

The issue is that if thumbnail is defined as string - thumbnail will never be processed.
image
Right?

@skyblade-x
Copy link
Author

@Badiboy if thumbnail is defined as string - for example http link to thumbnail it will be processed by this code:
изображение

@Badiboy
Copy link
Collaborator

Badiboy commented May 7, 2025

ok )

@Badiboy Badiboy merged commit 57f9e80 into eternnoir:master May 7, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants