diff --git a/docs/source/pyrogram/InputMedia.rst b/docs/source/pyrogram/InputMedia.rst new file mode 100644 index 00000000..c637bdc0 --- /dev/null +++ b/docs/source/pyrogram/InputMedia.rst @@ -0,0 +1,6 @@ +InputMedia +========== + +.. autoclass:: pyrogram.InputMedia + :members: + :undoc-members: diff --git a/docs/source/pyrogram/index.rst b/docs/source/pyrogram/index.rst index e484bd5e..160766eb 100644 --- a/docs/source/pyrogram/index.rst +++ b/docs/source/pyrogram/index.rst @@ -11,6 +11,7 @@ the same parameters as well, thus offering a familiar look to Bot developers. Client ChatAction ParseMode + InputMedia Error .. _Telegram Bot API: https://core.telegram.org/bots/api#available-methods diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index 09545a6c..767a4cab 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -29,3 +29,4 @@ from .api.errors import Error from .client import ChatAction from .client import Client from .client import ParseMode +from .client.input_media import InputMedia diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index b6180a9c..4da6bc88 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -48,6 +48,7 @@ from pyrogram.api.types import ( ) from pyrogram.crypto import CTR from pyrogram.session import Auth, Session +from .input_media import InputMedia from .style import Markdown, HTML log = logging.getLogger(__name__) @@ -1941,3 +1942,79 @@ class Client: ) else: return False + + def send_media_group(self, + chat_id: int or str, + media: list, + disable_notification: bool = None, + reply_to_message_id: int = None): + multi_media = [] + + for i in media: + if isinstance(i, InputMedia.Photo): + style = self.html if i.parse_mode.lower() == "html" else self.markdown + media = self.save_file(i.media) + + media = self.send( + functions.messages.UploadMedia( + peer=self.resolve_peer(chat_id), + media=types.InputMediaUploadedPhoto( + file=media + ) + ) + ) + + single_media = types.InputSingleMedia( + media=types.InputMediaPhoto( + id=types.InputPhoto( + id=media.photo.id, + access_hash=media.photo.access_hash + ) + ), + random_id=self.rnd_id(), + **style.parse(i.caption) + ) + + multi_media.append(single_media) + elif isinstance(i, InputMedia.Video): + style = self.html if i.parse_mode.lower() == "html" else self.markdown + media = self.save_file(i.media) + + media = self.send( + functions.messages.UploadMedia( + peer=self.resolve_peer(chat_id), + media=types.InputMediaUploadedDocument( + file=media, + mime_type=mimetypes.types_map[".mp4"], + attributes=[ + types.DocumentAttributeVideo( + duration=i.duration, + w=i.width, + h=i.height + ) + ] + ) + ) + ) + + single_media = types.InputSingleMedia( + media=types.InputMediaDocument( + id=types.InputDocument( + id=media.document.id, + access_hash=media.document.access_hash + ) + ), + random_id=self.rnd_id(), + **style.parse(i.caption) + ) + + multi_media.append(single_media) + + return self.send( + functions.messages.SendMultiMedia( + peer=self.resolve_peer(chat_id), + multi_media=multi_media, + silent=disable_notification or None, + reply_to_msg_id=reply_to_message_id + ) + ) diff --git a/pyrogram/client/input_media.py b/pyrogram/client/input_media.py new file mode 100644 index 00000000..d7612121 --- /dev/null +++ b/pyrogram/client/input_media.py @@ -0,0 +1,56 @@ +class InputMedia: + class Photo: + """This object represents a photo to be sent inside an album. + + Args: + media (:obj:`str`): + File to send. + Pass a file path as string to send a photo that exists on your local machine. + + caption (:obj:`str`): + Caption of the photo to be sent, 0-200 characters + + parse_mode (:obj:`str`): + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your caption. + Defaults to Markdown. + """ + + def __init__(self, + media: str, + caption: str = "", + parse_mode: str = ""): + self.media = media + self.caption = caption + self.parse_mode = parse_mode + + class Video: + """This object represents a video to be sent inside an album. + + Args: + media (:obj:`str`): + File to send. + Pass a file path as string to send a video that exists on your local machine. + + caption (:obj:`str`): + Caption of the video to be sent, 0-200 characters + + parse_mode (:obj:`str`): + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your caption. + Defaults to Markdown. + """ + + def __init__(self, + media: str, + caption: str = "", + parse_mode: str = "", + width: int = 0, + height: int = 0, + duration: int = 0): + self.media = media + self.caption = caption + self.parse_mode = parse_mode + self.width = width + self.height = height + self.duration = duration