2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 04:48:06 +00:00

Add get_chat_preview method and ChatPreview object

This commit is contained in:
Dan 2018-12-28 14:08:09 +01:00
parent 9fadbbd728
commit 0e62b3472a
8 changed files with 152 additions and 3 deletions

View File

@ -74,6 +74,7 @@ Chats
join_chat
leave_chat
get_chat_preview
kick_chat_member
unban_chat_member
restrict_chat_member

View File

@ -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:

View File

@ -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,

View File

@ -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

View File

@ -0,0 +1,63 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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 <pyrogram.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")

View File

@ -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
)

View File

@ -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

View File

@ -0,0 +1,78 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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