From eb36a8f261e4056146770968c1d81e4c8a450d98 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 2 Feb 2018 19:00:43 +0100 Subject: [PATCH 1/4] Add InputMedia class --- pyrogram/__init__.py | 1 + pyrogram/client/input_media.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 pyrogram/client/input_media.py 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/input_media.py b/pyrogram/client/input_media.py new file mode 100644 index 00000000..e61ccc15 --- /dev/null +++ b/pyrogram/client/input_media.py @@ -0,0 +1,24 @@ +class InputMedia: + class Photo: + def __init__(self, + media: str, + caption: str = "", + parse_mode: str = ""): + self.media = media + self.caption = caption + self.parse_mode = parse_mode + + class Video: + 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 From 995167c5347fd8ef1534b296bcf111f804ef654d Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 4 Feb 2018 12:25:49 +0100 Subject: [PATCH 2/4] Add docstrings --- pyrogram/client/input_media.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pyrogram/client/input_media.py b/pyrogram/client/input_media.py index e61ccc15..d7612121 100644 --- a/pyrogram/client/input_media.py +++ b/pyrogram/client/input_media.py @@ -1,5 +1,21 @@ 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 = "", @@ -9,6 +25,22 @@ class InputMedia: 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 = "", From cfb36351f9c6193f5d8dd64654f1ee4b981a7c1e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 6 Feb 2018 15:49:43 +0100 Subject: [PATCH 3/4] Add InputMedia docs --- docs/source/pyrogram/InputMedia.rst | 6 ++++++ docs/source/pyrogram/index.rst | 1 + 2 files changed, 7 insertions(+) create mode 100644 docs/source/pyrogram/InputMedia.rst 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 From a358b1f26b25afc8ebc39939622d694b66895100 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 7 Feb 2018 03:05:02 +0100 Subject: [PATCH 4/4] Add send_media_group method --- pyrogram/client/client.py | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) 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 + ) + )