From 0d12d8c1bb92ce7473218361ed3742e677d6c0aa Mon Sep 17 00:00:00 2001 From: Christy Roys Date: Sat, 22 May 2021 13:30:23 +0530 Subject: [PATCH] Add support for in-memory uploads in send_media_group (#519) * Add support for in-memory uploads for send_media_group * update input_media_photo docs * update type hints Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com> --- pyrogram/methods/messages/send_media_group.py | 5 +++-- pyrogram/types/input_media/input_media_photo.py | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pyrogram/methods/messages/send_media_group.py b/pyrogram/methods/messages/send_media_group.py index d591c99b..1669ce32 100644 --- a/pyrogram/methods/messages/send_media_group.py +++ b/pyrogram/methods/messages/send_media_group.py @@ -19,6 +19,7 @@ import logging import os import re +import io from typing import Union, List from pyrogram import raw @@ -87,7 +88,7 @@ class SendMediaGroup(Scaffold): for i in media: if isinstance(i, types.InputMediaPhoto): - if os.path.isfile(i.media): + if os.path.isfile(i.media) or isinstance(i.media, io.IOBase): media = await self.send( raw.functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), @@ -124,7 +125,7 @@ class SendMediaGroup(Scaffold): else: media = utils.get_input_media_from_file_id(i.media, FileType.PHOTO) elif isinstance(i, types.InputMediaVideo): - if os.path.isfile(i.media): + if os.path.isfile(i.media) or isinstance(i.media, io.IOBase): media = await self.send( raw.functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), diff --git a/pyrogram/types/input_media/input_media_photo.py b/pyrogram/types/input_media/input_media_photo.py index a8213540..e84a7616 100644 --- a/pyrogram/types/input_media/input_media_photo.py +++ b/pyrogram/types/input_media/input_media_photo.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import Optional, List +from typing import Optional, List, Union, BinaryIO from .input_media import InputMedia from ..messages_and_media import MessageEntity @@ -27,10 +27,11 @@ class InputMediaPhoto(InputMedia): It is intended to be used with :obj:`~pyrogram.Client.send_media_group`. Parameters: - media (``str``): + media (``str`` | ``BinaryIO``): Photo to send. Pass a file_id as string to send a photo that exists on the Telegram servers or pass a file path as string to upload a new photo that exists on your local machine or + pass a binary file-like object with its attribute “.name” set for in-memory uploads or pass an HTTP URL as a string for Telegram to get a photo from the Internet. caption (``str``, *optional*): @@ -50,7 +51,7 @@ class InputMediaPhoto(InputMedia): def __init__( self, - media: str, + media: Union[str, BinaryIO], caption: str = "", parse_mode: Optional[str] = object, caption_entities: List[MessageEntity] = None