|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-2020 Dan <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +import logging |
| 20 | +from typing import Union, List |
| 21 | + |
| 22 | +from pyrogram.scaffold import Scaffold |
| 23 | +from pyrogram.types import list |
| 24 | + |
| 25 | +log = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class GetMediaGroup(Scaffold): |
| 29 | + async def get_media_group( |
| 30 | + self, |
| 31 | + chat_id: Union[int, str], |
| 32 | + message_id: int |
| 33 | + ) -> List["types.Message"]: |
| 34 | + """Get the media group a message belongs to. |
| 35 | + |
| 36 | + Parameters: |
| 37 | + chat_id (``int`` | ``str``): |
| 38 | + Unique identifier (int) or username (str) of the target chat. |
| 39 | + For your personal cloud (Saved Messages) you can simply use "me" or "self". |
| 40 | + For a contact that exists in your Telegram address book you can use his phone number (str). |
| 41 | +
|
| 42 | + message_id (``int``): |
| 43 | + The id of one of the messages that belong to the media group. |
| 44 | + |
| 45 | + Returns: |
| 46 | + List of :obj:`~pyrogram.types.Message`: On success, a list of messages of the media group is returned. |
| 47 | + |
| 48 | + Raises: |
| 49 | + ValueError: In case the passed message id doesn't belong to a media group. |
| 50 | + """ |
| 51 | + # There can be maximum 10 items in a media group. |
| 52 | + messages = await self.get_messages(chat_id, [msg_id for msg_id in range(message_id - 9, message_id + 10)], replies=0) |
| 53 | + |
| 54 | + media_group_id = messages[9].media_group_id |
| 55 | + |
| 56 | + if media_group_id is None: |
| 57 | + raise ValueError("The message doesn't belong to a media group") |
| 58 | + |
| 59 | + return list.List(msg for msg in messages if msg.media_group_id == media_group_id) |
0 commit comments