diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 414154ed..bd1cb4a4 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -252,6 +252,8 @@ def pyrogram_api(): get_folders update_folder get_similar_channels + join_folder + leave_folder """, users=""" Users diff --git a/pyrogram/methods/chats/__init__.py b/pyrogram/methods/chats/__init__.py index 66b5d11f..806701f9 100644 --- a/pyrogram/methods/chats/__init__.py +++ b/pyrogram/methods/chats/__init__.py @@ -47,7 +47,9 @@ from .get_forum_topics_by_id import GetForumTopicsByID from .get_nearby_chats import GetNearbyChats from .get_send_as_chats import GetSendAsChats from .join_chat import JoinChat +from .join_folder import JoinFolder from .leave_chat import LeaveChat +from .leave_folder import LeaveFolder from .mark_chat_unread import MarkChatUnread from .pin_chat_message import PinChatMessage from .promote_chat_member import PromoteChatMember @@ -75,7 +77,9 @@ from .update_folder import UpdateFolder class Chats( GetChat, LeaveChat, + LeaveFolder, JoinChat, + JoinFolder, BanChatMember, UnbanChatMember, RestrictChatMember, diff --git a/pyrogram/methods/chats/join_folder.py b/pyrogram/methods/chats/join_folder.py new file mode 100644 index 00000000..64dde387 --- /dev/null +++ b/pyrogram/methods/chats/join_folder.py @@ -0,0 +1,75 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# 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 re + +import pyrogram +from pyrogram import raw, utils + + +class JoinFolder: + async def join_folder( + self: "pyrogram.Client", + link: str, + ) -> bool: + """Join a folder by its invite link. + + .. include:: /_includes/usable-by/users.rst + + Parameters: + link (``str``): + Invite link of the folder. + + Returns: + ``bool``: True, on success. + + Raises: + BadRequest: In case the folder invite link not exists. + ValueError: In case the folder invite link is invalid. + + Example: + .. code-block:: python + + # join folder + app.join_folder("t.me/addlist/ebXQ0Q0I3RnGQ") + """ + match = re.match(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/(?:addlist/|\+))([\w-]+)$", link) + + if not match: + raise ValueError("Invalid folder invite link") + + r = await self.invoke( + raw.functions.chatlists.CheckChatlistInvite( + slug=match.group(1) + ) + ) + + if isinstance(r, raw.types.chatlists.ChatlistInviteAlready): + peers = r.already_peers + r.missing_peers + else: + peers = r.peers + + await self.invoke( + raw.functions.chatlists.JoinChatlistInvite( + slug=match.group(1), + peers=[ + await self.resolve_peer(utils.get_peer_id(id)) for id in peers + ], + ) + ) + + return True diff --git a/pyrogram/methods/chats/leave_folder.py b/pyrogram/methods/chats/leave_folder.py new file mode 100644 index 00000000..1ef34cde --- /dev/null +++ b/pyrogram/methods/chats/leave_folder.py @@ -0,0 +1,79 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# 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 re + +import pyrogram +from pyrogram import raw, utils + + +class LeaveFolder: + async def leave_folder( + self: "pyrogram.Client", + link: str, + keep_chats: bool = True + ) -> bool: + """Leave a folder by its invite link. + + .. include:: /_includes/usable-by/users.rst + + Parameters: + link (``str``): + Invite link of the folder. + + keep_chats (``bool``, *optional*): + If True, the chats from the folder will be kept in the user's account. + Defaults to True. + + Returns: + ``bool``: True, on success. + + Raises: + AttributeError: In case the folder invite link does not exist in the user's account. + BadRequest: In case the folder invite link not exists. + ValueError: In case the folder invite link is invalid. + + Example: + .. code-block:: python + + # leave folder + app.leave_folder("t.me/addlist/ebXQ0Q0I3RnGQ") + """ + match = re.match(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/(?:addlist/|\+))([\w-]+)$", link) + + if not match: + raise ValueError("Invalid folder invite link") + + r = await self.invoke( + raw.functions.chatlists.CheckChatlistInvite( + slug=match.group(1) + ) + ) + + await self.invoke( + raw.functions.chatlists.LeaveChatlist( + chatlist=raw.types.InputChatlistDialogFilter( + filter_id=r.filter_id + ), + peers=[ + await self.resolve_peer(utils.get_peer_id(id)) + for id in r.already_peers + ] if not keep_chats else [], + ) + ) + + return True