Skip to content

Commit e1dac6c

Browse files
taijefdelivrance
andauthored
Add new method get_media_group (pyrogram#550)
* Update __init__.py Support for GetMediaGroup * Create get_media_group.py Added new method - get_media_group * Update get_media_group.py Add pyro stuff * Update get_media_group.py * Update compiler.py Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com>
1 parent 503327c commit e1dac6c

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def get_title_list(s: str) -> list:
168168
send_chat_action
169169
delete_messages
170170
get_messages
171+
get_media_group
171172
get_history
172173
get_history_count
173174
read_history

pyrogram/methods/messages/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .forward_messages import ForwardMessages
3131
from .get_history import GetHistory
3232
from .get_history_count import GetHistoryCount
33+
from .get_media_group import GetMediaGroup
3334
from .get_messages import GetMessages
3435
from .iter_history import IterHistory
3536
from .read_history import ReadHistory
@@ -65,6 +66,7 @@ class Messages(
6566
EditMessageText,
6667
ForwardMessages,
6768
GetHistory,
69+
GetMediaGroup,
6870
GetMessages,
6971
SendAudio,
7072
SendChatAction,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)