From 0e62b3472ac2bab8fa19d136649a98deb81f9102 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 28 Dec 2018 14:08:09 +0100 Subject: [PATCH] Add get_chat_preview method and ChatPreview object --- docs/source/pyrogram/Client.rst | 1 + docs/source/pyrogram/Types.rst | 4 + pyrogram/__init__.py | 2 +- pyrogram/client/methods/chats/__init__.py | 4 +- .../client/methods/chats/get_chat_preview.py | 63 +++++++++++++++ pyrogram/client/types/__init__.py | 2 +- .../client/types/user_and_chats/__init__.py | 1 + .../types/user_and_chats/chat_preview.py | 78 +++++++++++++++++++ 8 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 pyrogram/client/methods/chats/get_chat_preview.py create mode 100644 pyrogram/client/types/user_and_chats/chat_preview.py diff --git a/docs/source/pyrogram/Client.rst b/docs/source/pyrogram/Client.rst index a0177a12..179910a3 100644 --- a/docs/source/pyrogram/Client.rst +++ b/docs/source/pyrogram/Client.rst @@ -74,6 +74,7 @@ Chats join_chat leave_chat + get_chat_preview kick_chat_member unban_chat_member restrict_chat_member diff --git a/docs/source/pyrogram/Types.rst b/docs/source/pyrogram/Types.rst index 47c755f9..bf1bf937 100644 --- a/docs/source/pyrogram/Types.rst +++ b/docs/source/pyrogram/Types.rst @@ -12,6 +12,7 @@ Users & Chats User UserStatus Chat + ChatPreview ChatPhoto ChatMember ChatMembers @@ -82,6 +83,9 @@ Input Media .. autoclass:: Chat :members: +.. autoclass:: ChatPreview + :members: + .. autoclass:: ChatPhoto :members: diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index e3628fe4..47af42c7 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -32,7 +32,7 @@ from .client.types import ( Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, User, UserStatus, UserProfilePhotos, Venue, Animation, Video, VideoNote, Voice, CallbackQuery, Messages, ForceReply, InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove, - Poll, PollOption + Poll, PollOption, ChatPreview ) from .client import ( Client, ChatAction, ParseMode, Emoji, diff --git a/pyrogram/client/methods/chats/__init__.py b/pyrogram/client/methods/chats/__init__.py index f9eb25f3..3d928e87 100644 --- a/pyrogram/client/methods/chats/__init__.py +++ b/pyrogram/client/methods/chats/__init__.py @@ -22,6 +22,7 @@ from .get_chat import GetChat from .get_chat_member import GetChatMember from .get_chat_members import GetChatMembers from .get_chat_members_count import GetChatMembersCount +from .get_chat_preview import GetChatPreview from .get_dialogs import GetDialogs from .join_chat import JoinChat from .kick_chat_member import KickChatMember @@ -54,6 +55,7 @@ class Chats( PinChatMessage, UnpinChatMessage, GetDialogs, - GetChatMembersCount + GetChatMembersCount, + GetChatPreview ): pass diff --git a/pyrogram/client/methods/chats/get_chat_preview.py b/pyrogram/client/methods/chats/get_chat_preview.py new file mode 100644 index 00000000..434c385b --- /dev/null +++ b/pyrogram/client/methods/chats/get_chat_preview.py @@ -0,0 +1,63 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import pyrogram +from pyrogram.api import functions, types +from ...ext import BaseClient + + +class GetChatPreview(BaseClient): + def get_chat_preview(self, + invite_link: str): + """Use this method to get the preview of a chat using the invite link. + + This method only returns a chat preview, if you want to join a chat use :meth:`join_chat` + + Args: + invite_link (``str``): + Unique identifier for the target chat in form of *t.me/joinchat/* links. + + Returns: + Either :obj:`Chat` or :obj:`ChatPreview`, depending on whether you already joined the chat or not. + + Raises: + :class:`Error ` in case of a Telegram RPC error. + ``ValueError`` in case of an invalid invite_link. + """ + match = self.INVITE_LINK_RE.match(invite_link) + + if match: + r = self.send( + functions.messages.CheckChatInvite( + hash=match.group(1) + ) + ) + + if isinstance(r, types.ChatInvite): + return pyrogram.ChatPreview._parse(self, r) + + if isinstance(r, types.ChatInviteAlready): + chat = r.chat + + if isinstance(chat, types.Chat): + return pyrogram.Chat._parse_chat_chat(self, chat) + + if isinstance(chat, types.Channel): + return pyrogram.Chat._parse_channel_chat(self, chat) + else: + raise ValueError("The invite_link is invalid") diff --git a/pyrogram/client/types/__init__.py b/pyrogram/client/types/__init__.py index 24de120f..7b30670f 100644 --- a/pyrogram/client/types/__init__.py +++ b/pyrogram/client/types/__init__.py @@ -35,5 +35,5 @@ from .messages_and_media import ( ) from .user_and_chats import ( Chat, ChatMember, ChatMembers, ChatPhoto, - Dialog, Dialogs, User, UserStatus + Dialog, Dialogs, User, UserStatus, ChatPreview ) diff --git a/pyrogram/client/types/user_and_chats/__init__.py b/pyrogram/client/types/user_and_chats/__init__.py index 0f814b92..ec082dfe 100644 --- a/pyrogram/client/types/user_and_chats/__init__.py +++ b/pyrogram/client/types/user_and_chats/__init__.py @@ -20,6 +20,7 @@ from .chat import Chat from .chat_member import ChatMember from .chat_members import ChatMembers from .chat_photo import ChatPhoto +from .chat_preview import ChatPreview from .dialog import Dialog from .dialogs import Dialogs from .user import User diff --git a/pyrogram/client/types/user_and_chats/chat_preview.py b/pyrogram/client/types/user_and_chats/chat_preview.py new file mode 100644 index 00000000..45048637 --- /dev/null +++ b/pyrogram/client/types/user_and_chats/chat_preview.py @@ -0,0 +1,78 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from typing import List + +import pyrogram +from pyrogram.api import types +from .chat_photo import ChatPhoto +from ..pyrogram_type import PyrogramType +from ..user_and_chats.user import User + + +class ChatPreview(PyrogramType): + """This object represents a chat preview. + + Args: + title (``str``): + Title of the chat. + + photo (:obj:`ChatPhoto`): + Chat photo. Suitable for downloads only. + + type (``str``): + Type of chat, can be either, "group", "supergroup" or "channel". + + members_count (``int``): + Chat members count. + + members (List of :obj:`User`, *optional*): + Preview of some of the chat members. + """ + + def __init__(self, + *, + client: "pyrogram.client.ext.BaseClient", + title: str, + photo: ChatPhoto, + type: str, + members_count: int, + members: List[User] = None): + super().__init__(client) + + self.title = title + self.photo = photo + self.type = type + self.members_count = members_count + self.members = members + + @staticmethod + def _parse(client, chat_invite: types.ChatInvite) -> "ChatPreview": + return ChatPreview( + title=chat_invite.title, + photo=ChatPhoto._parse(client, chat_invite.photo), + type=("group" if not chat_invite.channel else + "channel" if chat_invite.broadcast else + "supergroup"), + members_count=chat_invite.participants_count, + members=[User._parse(client, user) for user in chat_invite.participants] or None, + client=client + ) + + # TODO: Maybe just merge this object into Chat itself by adding the "members" field. + # get_chat can be used as well instead of get_chat_preview