|
| 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 |
0 commit comments