mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 12:57:52 +00:00
Remove get_chat_preview and have get_chat deal with ChatPreview objects
This commit is contained in:
parent
82c3bb2dba
commit
53d0cc30f6
@ -22,7 +22,6 @@ 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 .iter_chat_members import IterChatMembers
|
||||
from .iter_dialogs import IterDialogs
|
||||
@ -61,7 +60,6 @@ class Chats(
|
||||
UnpinChatMessage,
|
||||
GetDialogs,
|
||||
GetChatMembersCount,
|
||||
GetChatPreview,
|
||||
IterDialogs,
|
||||
IterChatMembers,
|
||||
UpdateChatUsername,
|
||||
|
@ -27,8 +27,9 @@ class GetChat(BaseClient):
|
||||
def get_chat(
|
||||
self,
|
||||
chat_id: Union[int, str]
|
||||
) -> "pyrogram.Chat":
|
||||
"""Get up to date information about the chat.
|
||||
) -> Union["pyrogram.Chat", "pyrogram.ChatPreview"]:
|
||||
"""Get up to date information about a chat.
|
||||
|
||||
Information include current name of the user for one-on-one conversations, current username of a user, group or
|
||||
channel, etc.
|
||||
|
||||
@ -39,7 +40,8 @@ class GetChat(BaseClient):
|
||||
of the target channel/supergroup (in the format @username).
|
||||
|
||||
Returns:
|
||||
:obj:`Chat`: On success, a chat object is returned.
|
||||
:obj:`Chat` | :obj:`ChatPreview`: On success, if you've already joined the chat, a chat object is returned,
|
||||
otherwise, a chat preview object is returned.
|
||||
|
||||
Raises:
|
||||
RPCError: In case of a Telegram RPC error.
|
||||
@ -48,16 +50,14 @@ class GetChat(BaseClient):
|
||||
match = self.INVITE_LINK_RE.match(str(chat_id))
|
||||
|
||||
if match:
|
||||
h = match.group(1)
|
||||
|
||||
r = self.send(
|
||||
functions.messages.CheckChatInvite(
|
||||
hash=h
|
||||
hash=match.group(1)
|
||||
)
|
||||
)
|
||||
|
||||
if isinstance(r, types.ChatInvite):
|
||||
raise ValueError("You haven't joined \"t.me/joinchat/{}\" yet".format(h))
|
||||
return pyrogram.ChatPreview._parse(self, r)
|
||||
|
||||
self.fetch_peers([r.chat])
|
||||
|
||||
|
@ -1,67 +0,0 @@
|
||||
# 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
|
||||
):
|
||||
"""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`
|
||||
|
||||
Parameters:
|
||||
invite_link (``str``):
|
||||
Unique identifier for the target chat in form of *t.me/joinchat/* links.
|
||||
|
||||
Returns:
|
||||
:obj:`Chat`: In case you already joined the chat.
|
||||
|
||||
:obj:`ChatPreview` -- In case you haven't joined the chat yet.
|
||||
|
||||
Raises:
|
||||
RPCError: 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")
|
@ -32,7 +32,7 @@ class ChatPreview(PyrogramType):
|
||||
title (``str``):
|
||||
Title of the chat.
|
||||
|
||||
photo (:obj:`ChatPhoto`):
|
||||
photo (:obj:`ChatPhoto`, *optional*):
|
||||
Chat photo. Suitable for downloads only.
|
||||
|
||||
type (``str``):
|
||||
@ -52,7 +52,7 @@ class ChatPreview(PyrogramType):
|
||||
*,
|
||||
client: "pyrogram.BaseClient" = None,
|
||||
title: str,
|
||||
photo: ChatPhoto,
|
||||
photo: ChatPhoto = None,
|
||||
type: str,
|
||||
members_count: int,
|
||||
members: List[User] = None
|
||||
|
Loading…
x
Reference in New Issue
Block a user