def set_chat_permissions(
        self,
        chat_id: Union[int, str],
        permissions: ChatPermissions,
    ) -> Chat:
        """Set default chat permissions for all members.

        You must be an administrator in the group or a supergroup for this to work and must have the
        *can_restrict_members* admin rights.

        Parameters:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            permissions (:obj:`ChatPermissions`):
                New default chat permissions.

        Returns:
            :obj:`Chat`: On success, a chat object is returned.

        Example:
            .. code-block:: python

                from pyrogram import ChatPermissions

                # Completely restrict chat
                app.set_chat_permissions(chat_id, ChatPermissions())

                # Chat members can only send text messages, media, stickers and GIFs
                app.set_chat_permissions(
                    chat_id,
                    ChatPermissions(
                        can_send_messages=True,
                        can_send_media_messages=True,
                        can_send_stickers=True,
                        can_send_animations=True
                    )
                )
        """
        r = self.send(
            functions.messages.EditChatDefaultBannedRights(
                peer=self.resolve_peer(chat_id),
                banned_rights=types.ChatBannedRights(
                    until_date=0,
                    send_messages=True if not permissions.can_send_messages else None,
                    send_media=True if not permissions.can_send_media_messages else None,
                    send_stickers=True if not permissions.can_send_stickers else None,
                    send_gifs=True if not permissions.can_send_animations else None,
                    send_games=True if not permissions.can_send_games else None,
                    send_inline=True if not permissions.can_use_inline_bots else None,
                    embed_links=True if not permissions.can_add_web_page_previews else None,
                    send_polls=True if not permissions.can_send_polls else None,
                    change_info=True if not permissions.can_change_info else None,
                    invite_users=True if not permissions.can_invite_users else None,
                    pin_messages=True if not permissions.can_pin_messages else None,
                )
            )
        )

        return Chat._parse_chat(self, r.chats[0])
    def unban_chat_member(self, chat_id: Union[int, str],
                          user_id: Union[int, str]) -> bool:
        """Unban a previously kicked user in a supergroup or channel.
        The user will **not** return to the group or channel automatically, but will be able to join via link, etc.
        You must be an administrator for this to work.

        Parameters:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            user_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target user.
                For a contact that exists in your Telegram address book you can use his phone number (str).

        Returns:
            ``bool``: True on success.

        Example:
            .. code-block:: python

                # Unban chat member right now
                app.unban_chat_member(chat_id, user_id)
        """
        self.send(
            functions.channels.EditBanned(
                channel=self.resolve_peer(chat_id),
                user_id=self.resolve_peer(user_id),
                banned_rights=types.ChatBannedRights(until_date=0)))

        return True
Exemple #3
0
    async def unban_chat_member(
        self,
        chat_id: Union[int, str],
        user_id: Union[int, str]
    ) -> bool:
        """Use this method to unban a previously kicked user in a supergroup or channel.
        The user will **not** return to the group or channel automatically, but will be able to join via link, etc.
        You must be an administrator for this to work.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            user_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target user.
                For a contact that exists in your Telegram address book you can use his phone number (str).

        Returns:
            True on success.

        Raises:
            :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
        """
        await self.send(
            functions.channels.EditBanned(
                channel=await self.resolve_peer(chat_id),
                user_id=await self.resolve_peer(user_id),
                banned_rights=types.ChatBannedRights(
                    until_date=0
                )
            )
        )

        return True
    async def kick_chat_member(
            self,
            chat_id: Union[int, str],
            user_id: Union[int, str],
            until_date: int = 0) -> Union["pyrogram.Message", bool]:
        """Kick a user from a group, a supergroup or a channel.
        In the case of supergroups and channels, the user will not be able to return to the group on their own using
        invite links, etc., unless unbanned first. You must be an administrator in the chat for this to work and must
        have the appropriate admin rights.

        Note:
            In regular groups (non-supergroups), this method will only work if the "All Members Are Admins" setting is
            off in the target group. Otherwise members may only be removed by the group's creator or by the member
            that added them.

        Parameters:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            user_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target user.
                For a contact that exists in your Telegram address book you can use his phone number (str).

            until_date (``int``, *optional*):
                Date when the user will be unbanned, unix time.
                If user is banned for more than 366 days or less than 30 seconds from the current time they are
                considered to be banned forever. Defaults to 0 (ban forever).

        Returns:
            :obj:`Message` | ``bool``: On success, a service message will be returned (when applicable), otherwise, in
            case a message object couldn't be returned, True is returned.

        Example:
            .. code-block:: python

                from time import time

                # Ban chat member forever
                app.kick_chat_member(chat_id, user_id)

                # Kick chat member and automatically unban after 24h
                app.kick_chat_member(chat_id, user_id, int(time.time() + 86400))
        """
        chat_peer = await self.resolve_peer(chat_id)
        user_peer = await self.resolve_peer(user_id)

        if isinstance(chat_peer, types.InputPeerChannel):
            r = await self.send(
                functions.channels.EditBanned(
                    channel=chat_peer,
                    user_id=user_peer,
                    banned_rights=types.ChatBannedRights(until_date=until_date,
                                                         view_messages=True,
                                                         send_messages=True,
                                                         send_media=True,
                                                         send_stickers=True,
                                                         send_gifs=True,
                                                         send_games=True,
                                                         send_inline=True,
                                                         embed_links=True)))
        else:
            r = await self.send(
                functions.messages.DeleteChatUser(chat_id=abs(chat_id),
                                                  user_id=user_peer))

        for i in r.updates:
            if isinstance(
                    i,
                (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
                return await pyrogram.Message._parse(
                    self, i.message, {i.id: i
                                      for i in r.users},
                    {i.id: i
                     for i in r.chats})
        else:
            return True
Exemple #5
0
    def restrict_chat(self,
                      chat_id: Union[int, str],
                      can_send_messages: bool = False,
                      can_send_media_messages: bool = False,
                      can_send_other_messages: bool = False,
                      can_add_web_page_previews: bool = False,
                      can_send_polls: bool = False,
                      can_change_info: bool = False,
                      can_invite_users: bool = False,
                      can_pin_messages: bool = False) -> Chat:
        """Restrict a chat.
        Pass True for all boolean parameters to lift restrictions from a chat.

        Parameters:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            can_send_messages (``bool``, *optional*):
                Pass True, if the user can send text messages, contacts, locations and venues.

            can_send_media_messages (``bool``, *optional*):
                Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes,
                implies can_send_messages.

            can_send_other_messages (``bool``, *optional*):
                Pass True, if the user can send animations, games, stickers and use inline bots,
                implies can_send_media_messages.

            can_add_web_page_previews (``bool``, *optional*):
                Pass True, if the user may add web page previews to their messages, implies can_send_media_messages.

            can_send_polls (``bool``, *optional*):
                Pass True, if the user can send polls, implies can_send_media_messages.

            can_change_info (``bool``, *optional*):
                Pass True, if the user can change the chat title, photo and other settings.

            can_invite_users (``bool``, *optional*):
                Pass True, if the user can invite new users to the chat.

            can_pin_messages (``bool``, *optional*):
                Pass True, if the user can pin messages.

        Returns:
            :obj:`Chat`: On success, a chat object is returned.

        Raises:
            RPCError: In case of a Telegram RPC error.
        """
        send_messages = True
        send_media = True
        send_stickers = True
        send_gifs = True
        send_games = True
        send_inline = True
        embed_links = True
        send_polls = True
        change_info = True
        invite_users = True
        pin_messages = True

        if can_send_messages:
            send_messages = None

        if can_send_media_messages:
            send_messages = None
            send_media = None

        if can_send_other_messages:
            send_messages = None
            send_media = None
            send_stickers = None
            send_gifs = None
            send_games = None
            send_inline = None

        if can_add_web_page_previews:
            send_messages = None
            send_media = None
            embed_links = None

        if can_send_polls:
            send_messages = None
            send_polls = None

        if can_change_info:
            change_info = None

        if can_invite_users:
            invite_users = None

        if can_pin_messages:
            pin_messages = None

        r = self.send(
            functions.messages.EditChatDefaultBannedRights(
                peer=self.resolve_peer(chat_id),
                banned_rights=types.ChatBannedRights(
                    until_date=0,
                    send_messages=send_messages,
                    send_media=send_media,
                    send_stickers=send_stickers,
                    send_gifs=send_gifs,
                    send_games=send_games,
                    send_inline=send_inline,
                    embed_links=embed_links,
                    send_polls=send_polls,
                    change_info=change_info,
                    invite_users=invite_users,
                    pin_messages=pin_messages)))

        return Chat._parse_chat(self, r.chats[0])
    async def restrict_chat_member(self,
                                   chat_id: Union[int, str],
                                   user_id: Union[int, str],
                                   until_date: int = 0,
                                   can_send_messages: bool = False,
                                   can_send_media_messages: bool = False,
                                   can_send_other_messages: bool = False,
                                   can_add_web_page_previews: bool = False,
                                   can_send_polls: bool = False,
                                   can_change_info: bool = False,
                                   can_invite_users: bool = False,
                                   can_pin_messages: bool = False) -> Chat:
        """Restrict a user in a supergroup.

        The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
        Pass True for all boolean parameters to lift restrictions from a user.

        Parameters:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            user_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target user.
                For a contact that exists in your Telegram address book you can use his phone number (str).

            until_date (``int``, *optional*):
                Date when the user will be unbanned, unix time.
                If user is banned for more than 366 days or less than 30 seconds from the current time they are
                considered to be banned forever. Defaults to 0 (ban forever).

            can_send_messages (``bool``, *optional*):
                Pass True, if the user can send text messages, contacts, locations and venues.

            can_send_media_messages (``bool``, *optional*):
                Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes,
                implies can_send_messages.

            can_send_other_messages (``bool``, *optional*):
                Pass True, if the user can send animations, games, stickers and use inline bots,
                implies can_send_media_messages.

            can_add_web_page_previews (``bool``, *optional*):
                Pass True, if the user may add web page previews to their messages, implies can_send_media_messages.

            can_send_polls (``bool``, *optional*):
                Pass True, if the user can send polls, implies can_send_media_messages.

            can_change_info (``bool``, *optional*):
                Pass True, if the user can change the chat title, photo and other settings.

            can_invite_users (``bool``, *optional*):
                Pass True, if the user can invite new users to the chat.

            can_pin_messages (``bool``, *optional*):
                Pass True, if the user can pin messages.

        Returns:
            :obj:`Chat`: On success, a chat object is returned.

        Raises:
            RPCError: In case of a Telegram RPC error.
        """
        send_messages = True
        send_media = True
        send_stickers = True
        send_gifs = True
        send_games = True
        send_inline = True
        embed_links = True
        send_polls = True
        change_info = True
        invite_users = True
        pin_messages = True

        if can_send_messages:
            send_messages = None

        if can_send_media_messages:
            send_messages = None
            send_media = None

        if can_send_other_messages:
            send_messages = None
            send_media = None
            send_stickers = None
            send_gifs = None
            send_games = None
            send_inline = None

        if can_add_web_page_previews:
            send_messages = None
            send_media = None
            embed_links = None

        if can_send_polls:
            send_messages = None
            send_polls = None

        if can_change_info:
            change_info = None

        if can_invite_users:
            invite_users = None

        if can_pin_messages:
            pin_messages = None

        r = await self.send(
            functions.channels.EditBanned(channel=await
                                          self.resolve_peer(chat_id),
                                          user_id=await
                                          self.resolve_peer(user_id),
                                          banned_rights=types.ChatBannedRights(
                                              until_date=until_date,
                                              send_messages=send_messages,
                                              send_media=send_media,
                                              send_stickers=send_stickers,
                                              send_gifs=send_gifs,
                                              send_games=send_games,
                                              send_inline=send_inline,
                                              embed_links=embed_links,
                                              send_polls=send_polls,
                                              change_info=change_info,
                                              invite_users=invite_users,
                                              pin_messages=pin_messages)))

        return Chat._parse_chat(self, r.chats[0])
    def restrict_chat_member(self,
                             chat_id: Union[int, str],
                             user_id: Union[int, str],
                             permissions: ChatPermissions,
                             until_date: int = 0) -> Chat:
        """Restrict a user in a supergroup.

        You must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
        Pass True for all permissions to lift restrictions from a user.

        Parameters:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            user_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target user.
                For a contact that exists in your Telegram address book you can use his phone number (str).

            permissions (:obj:`ChatPermissions`):
                New user permissions.

            until_date (``int``, *optional*):
                Date when the user will be unbanned, unix time.
                If user is banned for more than 366 days or less than 30 seconds from the current time they are
                considered to be banned forever. Defaults to 0 (ban forever).

        Returns:
            :obj:`Chat`: On success, a chat object is returned.

        Example:
            .. code-block:: python

                from time import time

                from pyrogram import ChatPermissions

                # Completely restrict chat member (mute) forever
                app.restrict_chat_member(chat_id, user_id, ChatPermissions())

                # Chat member muted for 24h
                app.restrict_chat_member(chat_id, user_id, ChatPermissions(), int(time.time() + 86400))

                # Chat member can only send text messages
                app.restrict_chat_member(chat_id, user_id, ChatPermissions(can_send_messages=True))
        """
        send_messages = True
        send_media = True
        send_stickers = True
        send_gifs = True
        send_games = True
        send_inline = True
        embed_links = True
        send_polls = True
        change_info = True
        invite_users = True
        pin_messages = True

        if permissions.can_send_messages:
            send_messages = None

        if permissions.can_send_media_messages:
            send_messages = None
            send_media = None

        if permissions.can_send_other_messages:
            send_messages = None
            send_stickers = None
            send_gifs = None
            send_games = None
            send_inline = None

        if permissions.can_add_web_page_previews:
            send_messages = None
            embed_links = None

        if permissions.can_send_polls:
            send_messages = None
            send_polls = None

        if permissions.can_change_info:
            change_info = None

        if permissions.can_invite_users:
            invite_users = None

        if permissions.can_pin_messages:
            pin_messages = None

        r = self.send(
            functions.channels.EditBanned(channel=self.resolve_peer(chat_id),
                                          user_id=self.resolve_peer(user_id),
                                          banned_rights=types.ChatBannedRights(
                                              until_date=until_date,
                                              send_messages=send_messages,
                                              send_media=send_media,
                                              send_stickers=send_stickers,
                                              send_gifs=send_gifs,
                                              send_games=send_games,
                                              send_inline=send_inline,
                                              embed_links=embed_links,
                                              send_polls=send_polls,
                                              change_info=change_info,
                                              invite_users=invite_users,
                                              pin_messages=pin_messages)))

        return Chat._parse_chat(self, r.chats[0])