Skip to content

Commit 0e62b34

Browse files
committed
Add get_chat_preview method and ChatPreview object
1 parent 9fadbbd commit 0e62b34

File tree

8 files changed

+152
-3
lines changed

8 files changed

+152
-3
lines changed

docs/source/pyrogram/Client.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Chats
7474

7575
join_chat
7676
leave_chat
77+
get_chat_preview
7778
kick_chat_member
7879
unban_chat_member
7980
restrict_chat_member

docs/source/pyrogram/Types.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Users & Chats
1212
User
1313
UserStatus
1414
Chat
15+
ChatPreview
1516
ChatPhoto
1617
ChatMember
1718
ChatMembers
@@ -82,6 +83,9 @@ Input Media
8283
.. autoclass:: Chat
8384
:members:
8485

86+
.. autoclass:: ChatPreview
87+
:members:
88+
8589
.. autoclass:: ChatPhoto
8690
:members:
8791

pyrogram/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, User, UserStatus,
3333
UserProfilePhotos, Venue, Animation, Video, VideoNote, Voice, CallbackQuery, Messages, ForceReply,
3434
InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove,
35-
Poll, PollOption
35+
Poll, PollOption, ChatPreview
3636
)
3737
from .client import (
3838
Client, ChatAction, ParseMode, Emoji,

pyrogram/client/methods/chats/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .get_chat_member import GetChatMember
2323
from .get_chat_members import GetChatMembers
2424
from .get_chat_members_count import GetChatMembersCount
25+
from .get_chat_preview import GetChatPreview
2526
from .get_dialogs import GetDialogs
2627
from .join_chat import JoinChat
2728
from .kick_chat_member import KickChatMember
@@ -54,6 +55,7 @@ class Chats(
5455
PinChatMessage,
5556
UnpinChatMessage,
5657
GetDialogs,
57-
GetChatMembersCount
58+
GetChatMembersCount,
59+
GetChatPreview
5860
):
5961
pass
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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 pyrogram
20+
from pyrogram.api import functions, types
21+
from ...ext import BaseClient
22+
23+
24+
class GetChatPreview(BaseClient):
25+
def get_chat_preview(self,
26+
invite_link: str):
27+
"""Use this method to get the preview of a chat using the invite link.
28+
29+
This method only returns a chat preview, if you want to join a chat use :meth:`join_chat`
30+
31+
Args:
32+
invite_link (``str``):
33+
Unique identifier for the target chat in form of *t.me/joinchat/* links.
34+
35+
Returns:
36+
Either :obj:`Chat` or :obj:`ChatPreview`, depending on whether you already joined the chat or not.
37+
38+
Raises:
39+
:class:`Error <pyrogram.Error>` in case of a Telegram RPC error.
40+
``ValueError`` in case of an invalid invite_link.
41+
"""
42+
match = self.INVITE_LINK_RE.match(invite_link)
43+
44+
if match:
45+
r = self.send(
46+
functions.messages.CheckChatInvite(
47+
hash=match.group(1)
48+
)
49+
)
50+
51+
if isinstance(r, types.ChatInvite):
52+
return pyrogram.ChatPreview._parse(self, r)
53+
54+
if isinstance(r, types.ChatInviteAlready):
55+
chat = r.chat
56+
57+
if isinstance(chat, types.Chat):
58+
return pyrogram.Chat._parse_chat_chat(self, chat)
59+
60+
if isinstance(chat, types.Channel):
61+
return pyrogram.Chat._parse_channel_chat(self, chat)
62+
else:
63+
raise ValueError("The invite_link is invalid")

pyrogram/client/types/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
)
3636
from .user_and_chats import (
3737
Chat, ChatMember, ChatMembers, ChatPhoto,
38-
Dialog, Dialogs, User, UserStatus
38+
Dialog, Dialogs, User, UserStatus, ChatPreview
3939
)

pyrogram/client/types/user_and_chats/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .chat_member import ChatMember
2121
from .chat_members import ChatMembers
2222
from .chat_photo import ChatPhoto
23+
from .chat_preview import ChatPreview
2324
from .dialog import Dialog
2425
from .dialogs import Dialogs
2526
from .user import User
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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+
from typing import List
20+
21+
import pyrogram
22+
from pyrogram.api import types
23+
from .chat_photo import ChatPhoto
24+
from ..pyrogram_type import PyrogramType
25+
from ..user_and_chats.user import User
26+
27+
28+
class ChatPreview(PyrogramType):
29+
"""This object represents a chat preview.
30+
31+
Args:
32+
title (``str``):
33+
Title of the chat.
34+
35+
photo (:obj:`ChatPhoto`):
36+
Chat photo. Suitable for downloads only.
37+
38+
type (``str``):
39+
Type of chat, can be either, "group", "supergroup" or "channel".
40+
41+
members_count (``int``):
42+
Chat members count.
43+
44+
members (List of :obj:`User`, *optional*):
45+
Preview of some of the chat members.
46+
"""
47+
48+
def __init__(self,
49+
*,
50+
client: "pyrogram.client.ext.BaseClient",
51+
title: str,
52+
photo: ChatPhoto,
53+
type: str,
54+
members_count: int,
55+
members: List[User] = None):
56+
super().__init__(client)
57+
58+
self.title = title
59+
self.photo = photo
60+
self.type = type
61+
self.members_count = members_count
62+
self.members = members
63+
64+
@staticmethod
65+
def _parse(client, chat_invite: types.ChatInvite) -> "ChatPreview":
66+
return ChatPreview(
67+
title=chat_invite.title,
68+
photo=ChatPhoto._parse(client, chat_invite.photo),
69+
type=("group" if not chat_invite.channel else
70+
"channel" if chat_invite.broadcast else
71+
"supergroup"),
72+
members_count=chat_invite.participants_count,
73+
members=[User._parse(client, user) for user in chat_invite.participants] or None,
74+
client=client
75+
)
76+
77+
# TODO: Maybe just merge this object into Chat itself by adding the "members" field.
78+
# get_chat can be used as well instead of get_chat_preview

0 commit comments

Comments
 (0)