Skip to content

Commit 036a739

Browse files
committed
Add new methods: add_chat_members, create_* and delete_* chats
- add_chat_members - create_group - create_channel - create_supergroup - delete_channel - delete_supergroup
1 parent 184f851 commit 036a739

8 files changed

Lines changed: 345 additions & 1 deletion

File tree

compiler/docs/compiler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ def get_title_list(s: str) -> list:
204204
update_chat_username
205205
archive_chats
206206
unarchive_chats
207+
add_chat_members
208+
create_channel
209+
create_group
210+
create_supergroup
211+
delete_channel
212+
delete_supergroup
207213
""",
208214
users="""
209215
Users

pyrogram/client/methods/chats/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from .add_chat_members import AddChatMembers
1920
from .archive_chats import ArchiveChats
21+
from .create_channel import CreateChannel
22+
from .create_group import CreateGroup
23+
from .create_supergroup import CreateSupergroup
24+
from .delete_channel import DeleteChannel
2025
from .delete_chat_photo import DeleteChatPhoto
26+
from .delete_supergroup import DeleteSupergroup
2127
from .export_chat_invite_link import ExportChatInviteLink
2228
from .get_chat import GetChat
2329
from .get_chat_member import GetChatMember
@@ -68,6 +74,12 @@ class Chats(
6874
RestrictChat,
6975
GetDialogsCount,
7076
ArchiveChats,
71-
UnarchiveChats
77+
UnarchiveChats,
78+
CreateGroup,
79+
CreateSupergroup,
80+
CreateChannel,
81+
AddChatMembers,
82+
DeleteChannel,
83+
DeleteSupergroup
7284
):
7385
pass
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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, List
20+
21+
from pyrogram.api import functions, types
22+
from ...ext import BaseClient
23+
24+
25+
class AddChatMembers(BaseClient):
26+
def add_chat_members(
27+
self,
28+
chat_id: Union[int, str],
29+
user_ids: Union[Union[int, str], List[Union[int, str]]],
30+
forward_limit: int = 100
31+
) -> bool:
32+
"""Add new chat members to a group, supergroup or channel
33+
34+
Parameters:
35+
chat_id (``int`` | ``str``):
36+
The group, supergroup or channel id
37+
38+
user_ids (``int`` | ``str`` | List of ``int`` or ``str``):
39+
Users to add in the chat
40+
You can pass an ID (int), username (str) or phone number (str).
41+
Multiple users can be added by passing a list of IDs, usernames or phone numbers.
42+
43+
forward_limit (``int``, *optional*):
44+
How many of the latest messages you want to forward to the new members. Pass 0 to forward none of them.
45+
Only applicable to basic groups (the argument is ignored for supergroups or channels).
46+
Defaults to 100 (max amount).
47+
48+
Returns:
49+
``bool``: On success, True is returned.
50+
"""
51+
peer = self.resolve_peer(chat_id)
52+
53+
if not isinstance(user_ids, list):
54+
user_ids = [user_ids]
55+
56+
if isinstance(peer, types.InputPeerChat):
57+
for user_id in user_ids:
58+
self.send(
59+
functions.messages.AddChatUser(
60+
chat_id=peer.chat_id,
61+
user_id=self.resolve_peer(user_id),
62+
fwd_limit=forward_limit
63+
)
64+
)
65+
else:
66+
self.send(
67+
functions.channels.InviteToChannel(
68+
channel=peer,
69+
users=[
70+
self.resolve_peer(user_id)
71+
for user_id in user_ids
72+
]
73+
)
74+
)
75+
76+
return True
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-2019 Dan Tès <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+
import pyrogram
20+
from pyrogram.api import functions
21+
from ...ext import BaseClient
22+
23+
24+
class CreateChannel(BaseClient):
25+
def create_channel(
26+
self,
27+
title: str,
28+
description: str = ""
29+
) -> "pyrogram.Chat":
30+
"""Create a new broadcast channel.
31+
32+
Parameters:
33+
title (``title``):
34+
The channel title.
35+
36+
description (``str``, *optional*):
37+
The channel description.
38+
39+
Returns:
40+
:obj:`Chat`: On success, a chat object is returned.
41+
"""
42+
r = self.send(
43+
functions.channels.CreateChannel(
44+
title=title,
45+
about=description,
46+
broadcast=True
47+
)
48+
)
49+
50+
return pyrogram.Chat._parse_chat(self, r.chats[0])
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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, List
20+
21+
import pyrogram
22+
from pyrogram.api import functions
23+
from ...ext import BaseClient
24+
25+
26+
class CreateGroup(BaseClient):
27+
def create_group(
28+
self,
29+
title: str,
30+
users: Union[Union[int, str], List[Union[int, str]]]
31+
) -> "pyrogram.Chat":
32+
"""Create a new basic group.
33+
34+
.. note::
35+
36+
If you want to create a new supergroup, use :meth:`~pyrogram.Client.create_supergroup` instead.
37+
38+
Parameters:
39+
title (``title``):
40+
The group title.
41+
42+
users (``int`` | ``str`` | List of ``int`` or ``str``):
43+
Users to create a chat with.
44+
You must pass at least one user using their IDs (int), usernames (str) or phone numbers (str).
45+
Multiple users can be invited by passing a list of IDs, usernames or phone numbers.
46+
47+
Returns:
48+
:obj:`Chat`: On success, a chat object is returned.
49+
"""
50+
if not isinstance(users, list):
51+
users = [users]
52+
53+
r = self.send(
54+
functions.messages.CreateChat(
55+
title=title,
56+
users=[self.resolve_peer(u) for u in users]
57+
)
58+
)
59+
60+
return pyrogram.Chat._parse_chat(self, r.chats[0])
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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+
import pyrogram
20+
from pyrogram.api import functions
21+
from ...ext import BaseClient
22+
23+
24+
class CreateSupergroup(BaseClient):
25+
def create_supergroup(
26+
self,
27+
title: str,
28+
description: str = ""
29+
) -> "pyrogram.Chat":
30+
"""Create a new supergroup.
31+
32+
.. note::
33+
34+
If you want to create a new basic group, use :meth:`~pyrogram.Client.create_group` instead.
35+
36+
Parameters:
37+
title (``title``):
38+
The supergroup title.
39+
40+
description (``str``, *optional*):
41+
The supergroup description.
42+
43+
Returns:
44+
:obj:`Chat`: On success, a chat object is returned.
45+
"""
46+
r = self.send(
47+
functions.channels.CreateChannel(
48+
title=title,
49+
about=description,
50+
megagroup=True
51+
)
52+
)
53+
54+
return pyrogram.Chat._parse_chat(self, r.chats[0])
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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 typing import Union
21+
22+
from pyrogram.api import functions
23+
from ...ext import BaseClient
24+
25+
26+
class DeleteChannel(BaseClient):
27+
def delete_channel(self, chat_id: Union[int, str]) -> bool:
28+
"""Delete a channel.
29+
30+
Parameters:
31+
chat_id (``int`` | ``str``):
32+
The id of the channel to be deleted.
33+
34+
Returns:
35+
``bool``: On success, True is returned.
36+
"""
37+
self.send(
38+
functions.channels.DeleteChannel(
39+
channel=self.resolve_peer(chat_id)
40+
)
41+
)
42+
43+
return True
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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 typing import Union
21+
22+
from pyrogram.api import functions
23+
from ...ext import BaseClient
24+
25+
26+
class DeleteSupergroup(BaseClient):
27+
def delete_supergroup(self, chat_id: Union[int, str]) -> bool:
28+
"""Delete a supergroup.
29+
30+
Parameters:
31+
chat_id (``int`` | ``str``):
32+
The id of the supergroup to be deleted.
33+
34+
Returns:
35+
``bool``: On success, True is returned.
36+
"""
37+
self.send(
38+
functions.channels.DeleteChannel(
39+
channel=self.resolve_peer(chat_id)
40+
)
41+
)
42+
43+
return True

0 commit comments

Comments
 (0)