Skip to content

Commit ee5f393

Browse files
Add support for the improved Invite Links (pyrogram#639)
* Add new invite link export methods * Implement higher-level Invite type * Update Docstrings and rename Invite * Docstrings are now more consistent with other methods * Invite is now InviteLink to be less arbitrary * Add method to get exported links * `get_exported_chat_invites` * prepare `__init__` for the other InvitesV2 methods * Update returned type `str` -> `types.InviteLink` * Add method to edit invite link Since editing the link returns a slightly different type to exporting, I have made a small "hack" in the InviteLink type. * Move Invites V2 methods to their own namespace * Add get_chat_invite_importers and InviteImporter Method to fetch information on users that joined via a specific link and the type to display the information * Add methods to delete revoked links * delete_exported_chat_invite to delete a single revoked link * delete_revoked_exported_chat_invites to delete all revoked links of a specified admin * Renaming Invite(s) to InviteLink(s) As per @delivrance's request pyrogram#630 (comment) Also sorted invites' __init__ alphabetically * Add Method to get admins with exported invite link Documentation needs an update and the respective type needs to be created. I cannot test this, as I lack Creator permissions. * Invite Links overhaul * Rearrange code Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com>
1 parent 42b1865 commit ee5f393

26 files changed

+1013
-36
lines changed

compiler/docs/compiler.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ def get_title_list(s: str) -> list:
191191
restrict_chat_member
192192
promote_chat_member
193193
set_administrator_title
194-
export_chat_invite_link
195194
set_chat_photo
196195
delete_chat_photo
197196
set_chat_title
@@ -238,6 +237,20 @@ def get_title_list(s: str) -> list:
238237
unblock_user
239238
get_common_chats
240239
""",
240+
invite_links="""
241+
Invite Links
242+
export_chat_invite_link
243+
create_chat_invite_link
244+
edit_chat_invite_link
245+
revoke_chat_invite_link
246+
delete_chat_invite_link
247+
delete_all_chat_invite_links
248+
get_chat_invite_links
249+
get_chat_invite_links_count
250+
get_chat_invite_link_members
251+
get_chat_invite_link_members_count
252+
get_chat_admins_with_invite_links
253+
""",
241254
contacts="""
242255
Contacts
243256
add_contacts
@@ -332,6 +345,8 @@ def get_title_list(s: str) -> list:
332345
ChatPhoto
333346
ChatMember
334347
ChatPermissions
348+
ChatInviteLink
349+
ChatAdminWithInviteLinks
335350
ChatEvent
336351
ChatEventFilter
337352
Dialog

compiler/docs/template/methods.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ Users
8888

8989
{users}
9090

91+
Invite Links
92+
------------
93+
94+
.. autosummary::
95+
:nosignatures:
96+
97+
{invite_links}
98+
99+
.. toctree::
100+
:hidden:
101+
102+
{invite_links}
103+
91104
Contacts
92105
--------
93106

pyrogram/methods/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .chats import Chats
2323
from .contacts import Contacts
2424
from .decorators import Decorators
25+
from .invite_links import InviteLinks
2526
from .messages import Messages
2627
from .password import Password
2728
from .users import Users
@@ -38,6 +39,7 @@ class Methods(
3839
Users,
3940
Messages,
4041
Decorators,
41-
Utilities
42+
Utilities,
43+
InviteLinks,
4244
):
4345
pass

pyrogram/methods/chats/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from .delete_chat_photo import DeleteChatPhoto
2626
from .delete_supergroup import DeleteSupergroup
2727
from .delete_user_history import DeleteUserHistory
28-
from .export_chat_invite_link import ExportChatInviteLink
2928
from .get_chat import GetChat
3029
from .get_chat_event_log import GetChatEventLog
3130
from .get_chat_member import GetChatMember
@@ -58,7 +57,6 @@
5857

5958
class Chats(
6059
GetChat,
61-
ExportChatInviteLink,
6260
LeaveChat,
6361
JoinChat,
6462
KickChatMember,

pyrogram/methods/chats/set_chat_photo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
from pyrogram import raw
2323
from pyrogram import utils
24-
from pyrogram.scaffold import Scaffold
2524
from pyrogram.file_id import FileType
25+
from pyrogram.scaffold import Scaffold
2626

2727

2828
class SetChatPhoto(Scaffold):
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <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+
20+
from .create_chat_invite_link import CreateChatInviteLink
21+
from .delete_all_chat_invite_links import DeleteAllChatInviteLinks
22+
from .delete_chat_invite_link import DeleteChatInviteLink
23+
from .edit_chat_invite_link import EditChatInviteLink
24+
from .export_chat_invite_link import ExportChatInviteLink
25+
from .get_chat_admins_with_invite_links import GetChatAdminsWithInviteLinks
26+
from .get_chat_invite_link_members import GetChatInviteLinkMembers
27+
from .get_chat_invite_link_members_count import GetChatInviteLinkMembersCount
28+
from .get_chat_invite_links import GetChatInviteLinks
29+
from .get_chat_invite_links_count import GetChatInviteLinksCount
30+
from .revoke_chat_invite_link import RevokeChatInviteLink
31+
32+
33+
class InviteLinks(
34+
RevokeChatInviteLink,
35+
DeleteChatInviteLink,
36+
EditChatInviteLink,
37+
CreateChatInviteLink,
38+
GetChatInviteLinkMembers,
39+
GetChatInviteLinkMembersCount,
40+
GetChatInviteLinks,
41+
ExportChatInviteLink,
42+
DeleteAllChatInviteLinks,
43+
GetChatInviteLinksCount,
44+
GetChatAdminsWithInviteLinks
45+
):
46+
pass
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <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 Union
20+
21+
from pyrogram import raw
22+
from pyrogram import types
23+
from pyrogram.scaffold import Scaffold
24+
25+
26+
class CreateChatInviteLink(Scaffold):
27+
async def create_chat_invite_link(
28+
self,
29+
chat_id: Union[int, str],
30+
expire_date: int = None,
31+
member_limit: int = None,
32+
) -> "types.ChatInviteLink":
33+
"""Create an additional invite link for a chat.
34+
35+
You must be an administrator in the chat for this to work and must have the appropriate admin rights.
36+
37+
The link can be revoked using the method :meth:`~pyrogram.Client.revoke_chat_invite_link`.
38+
39+
Parameters:
40+
chat_id (``int`` | ``str``):
41+
Unique identifier for the target chat or username of the target channel/supergroup
42+
(in the format @username).
43+
44+
expire_date (``int``, *optional*):
45+
Point in time (Unix timestamp) when the link will expire.
46+
Defaults to None (no expiration date).
47+
48+
member_limit (``int``, *optional*):
49+
Maximum number of users that can be members of the chat simultaneously after joining the chat via
50+
this invite link; 1-99999.
51+
Defaults to None (no member limit).
52+
53+
Returns:
54+
:obj:`~pyrogram.types.ChatInviteLink`: On success, the new invite link is returned.
55+
56+
Example:
57+
.. code-block:: python
58+
59+
# Create a new link without limits
60+
link = app.create_chat_invite_link(chat_id)
61+
62+
# Create a new link for up to 7 new users
63+
link = app.create_chat_invite_link(chat_id, member_limit=7)
64+
"""
65+
r = await self.send(
66+
raw.functions.messages.ExportChatInvite(
67+
peer=await self.resolve_peer(chat_id),
68+
expire_date=expire_date,
69+
usage_limit=member_limit,
70+
)
71+
)
72+
73+
return types.ChatInviteLink._parse(self, r)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <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 Union
20+
21+
from pyrogram import raw
22+
from pyrogram.scaffold import Scaffold
23+
24+
25+
class DeleteAllChatInviteLinks(Scaffold):
26+
async def delete_all_chat_invite_links(
27+
self,
28+
chat_id: Union[int, str],
29+
admin_id: Union[int, str],
30+
) -> bool:
31+
"""Delete all revoked invite links of an administrator.
32+
33+
Parameters:
34+
chat_id (``int`` | ``str``):
35+
Unique identifier for the target chat or username of the target channel/supergroup
36+
(in the format @username).
37+
38+
admin_id (``int`` | ``str``):
39+
Unique identifier (int) or username (str) of the target user.
40+
For you yourself you can simply use "me" or "self".
41+
For a contact that exists in your Telegram address book you can use his phone number (str).
42+
43+
Returns:
44+
``bool``: On success ``True`` is returned.
45+
"""
46+
47+
return await self.send(
48+
raw.functions.messages.DeleteRevokedExportedChatInvites(
49+
peer=await self.resolve_peer(chat_id),
50+
admin_id=await self.resolve_peer(admin_id),
51+
)
52+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <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 Union
20+
21+
from pyrogram import raw
22+
from pyrogram.scaffold import Scaffold
23+
24+
25+
class DeleteChatInviteLink(Scaffold):
26+
async def delete_chat_invite_link(
27+
self,
28+
chat_id: Union[int, str],
29+
invite_link: str,
30+
) -> bool:
31+
"""Delete an already revoked invite link.
32+
33+
Parameters:
34+
chat_id (``int`` | ``str``):
35+
Unique identifier for the target chat or username of the target channel/supergroup
36+
(in the format @username).
37+
38+
invite_link (``str``):
39+
The revoked invite link to delete.
40+
41+
Returns:
42+
``bool``: On success ``True`` is returned.
43+
"""
44+
45+
return await self.send(
46+
raw.functions.messages.DeleteExportedChatInvite(
47+
peer=await self.resolve_peer(chat_id),
48+
link=invite_link,
49+
)
50+
)

0 commit comments

Comments
 (0)