mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Add MediaGroupBuilder for media group construction (#1293)
Implemented a MediaGroupBuilder class in 'aiogram/utils/media_group.py' to help construct media groups. The class supports addition of different media types (audio, photo, video, document) to the media group with a maximum limit of 10 files. The functionality is demonstrated and usage is documented in 'docs/utils/media_group.rst'. Added related test cases in 'tests/test_utils/test_media_group.py'. This is to streamline and simplify the process of media group creation
This commit is contained in:
parent
8fd110cdd1
commit
5cf8d7b565
6 changed files with 509 additions and 1 deletions
|
|
@ -10,3 +10,4 @@ Utils
|
|||
web_app
|
||||
callback_answer
|
||||
formatting
|
||||
media_group
|
||||
|
|
|
|||
46
docs/utils/media_group.rst
Normal file
46
docs/utils/media_group.rst
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
===================
|
||||
Media group builder
|
||||
===================
|
||||
|
||||
This module provides a builder for media groups, it can be used to build media groups
|
||||
for :class:`aiogram.types.input_media_photo.InputMediaPhoto`, :class:`aiogram.types.input_media_video.InputMediaVideo`,
|
||||
:class:`aiogram.types.input_media_document.InputMediaDocument` and :class:`aiogram.types.input_media_audio.InputMediaAudio`.
|
||||
|
||||
.. warning::
|
||||
|
||||
:class:`aiogram.types.input_media_animation.InputMediaAnimation`
|
||||
is not supported yet in the Bot API to send as media group.
|
||||
|
||||
|
||||
Usage
|
||||
=====
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
media_group = MediaGroupBuilder(caption="Media group caption")
|
||||
|
||||
# Add photo
|
||||
media_group.add_photo(media="https://picsum.photos/200/300")
|
||||
# Dynamically add photo with known type without using separate method
|
||||
media_group.add(type="photo", media="https://picsum.photos/200/300")
|
||||
# ... or video
|
||||
media_group.add(type="video", media=FSInputFile("media/video.mp4"))
|
||||
|
||||
|
||||
To send media group use :meth:`aiogram.methods.send_media_group.SendMediaGroup` method,
|
||||
but when you use :class:`aiogram.utils.media_group.MediaGroupBuilder`
|
||||
you should pass ``media`` argument as ``media_group.build()``.
|
||||
|
||||
If you specify ``caption`` in :class:`aiogram.utils.media_group.MediaGroupBuilder`
|
||||
it will be used as ``caption`` for first media in group.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
await bot.send_media_group(chat_id=chat_id, media=media_group.build())
|
||||
|
||||
|
||||
References
|
||||
==========
|
||||
|
||||
.. autoclass:: aiogram.utils.media_group.MediaGroupBuilder
|
||||
:members:
|
||||
Loading…
Add table
Add a link
Reference in a new issue