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>
This commit is contained in:
Christy Roys 2021-05-22 13:30:23 +05:30 committed by GitHub
parent 317685cf54
commit 0d12d8c1bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View File

@ -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),

View File

@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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