Skip to content

Upload media files of rich messages - #2640

Open
cristianchiriac wants to merge 1 commit into
eternnoir:masterfrom
cristianchiriac:rich-message-media-uploads
Open

cristianchiriac wants to merge 1 commit into
eternnoir:masterfrom
cristianchiriac:rich-message-media-uploads

Conversation

@cristianchiriac

Copy link
Copy Markdown
Contributor

Description

Covers items 1 and 2 of #2624.

Rich messages were sent with rich_message.to_json() only. A media object built from an InputFile is serialized as an attach://<name> reference, but the file itself was never sent, so uploads in InputRichBlockAnimation, InputRichBlockAudio, InputRichBlockPhoto, InputRichBlockVideo, InputRichBlockVoiceNote, InputRichBlockDocument and InputRichMessageMedia could not work.

  • InputRichMessage.convert_input_rich_message() returns the JSON and the files of every InputMedia in the message, collected with each media's own convert_input_media() (so thumbnails and video covers are included). It looks through media and blocks, including blocks nested in list items, collages, slideshows, details and quotations.
  • send_rich_message, send_rich_message_draft and edit_message_text send those files, in both apihelper.py and asyncio_helper.py. They already used POST. A rich message without uploads has no files, so the request is the same as before.

edit_ephemeral_message_text also takes a rich_message; I left it as it is because #2624 doesn't list it, but it could use the same helper.

Describe your tests

New tests/test_rich_message_uploads.py, which replaces _make_request / _process_request and checks what would be sent:

  • a rich message whose list item contains a photo block, plus an InputRichMessageMedia video, sends both files, under the names that the attach:// references in the JSON use, for each of the three methods (sync and async);
  • a rich message without uploads gives no files.

The tests fail without the change and pass with it. pytest tests --ignore=tests/test_telebot.py: 290 passed, 2 skipped. This PR is independent of #2639 (checklists and replace_sticker_in_set, item 3 of #2624).

Python version: 3.12

OS: Windows 11

Checklist:

  • I added/edited example on new feature/change (if exists)
  • My changes won't break backward compatibility
  • I made changes both for sync and async

InputRichMessage was sent with to_json() only, so a media object built
from an InputFile (in a photo, video, animation, audio, voice note or
document block, or in InputRichMessage.media) was sent as an attach://
reference without the file.

Add InputRichMessage.convert_input_rich_message(), which returns the
JSON and the files of every InputMedia in the message, including blocks
nested in lists, collages, slideshows, details and quotations, and send
those files with send_rich_message, send_rich_message_draft and
edit_message_text, in apihelper and asyncio_helper.

Items 1 and 2 of eternnoir#2624.
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.

1 participant