diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 9b9d2322..c5f8dea5 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -295,6 +295,8 @@ def pyrogram_api(): set_bot_commands get_bot_commands delete_bot_commands + set_bot_default_privileges + get_bot_default_privileges """, authorization=""" Authorization diff --git a/pyrogram/methods/bots/__init__.py b/pyrogram/methods/bots/__init__.py index 54854b8e..7ff79e84 100644 --- a/pyrogram/methods/bots/__init__.py +++ b/pyrogram/methods/bots/__init__.py @@ -20,12 +20,14 @@ from .answer_callback_query import AnswerCallbackQuery from .answer_inline_query import AnswerInlineQuery from .delete_bot_commands import DeleteBotCommands from .get_bot_commands import GetBotCommands +from .get_bot_default_privileges import GetBotDefaultPrivileges from .get_game_high_scores import GetGameHighScores from .get_inline_bot_results import GetInlineBotResults from .request_callback_answer import RequestCallbackAnswer from .send_game import SendGame from .send_inline_bot_result import SendInlineBotResult from .set_bot_commands import SetBotCommands +from .set_bot_default_privileges import SetBotDefaultPrivileges from .set_game_score import SetGameScore @@ -40,6 +42,8 @@ class Bots( GetGameHighScores, SetBotCommands, GetBotCommands, - DeleteBotCommands + DeleteBotCommands, + SetBotDefaultPrivileges, + GetBotDefaultPrivileges ): pass diff --git a/pyrogram/methods/bots/get_bot_default_privileges.py b/pyrogram/methods/bots/get_bot_default_privileges.py new file mode 100644 index 00000000..e1f70fa1 --- /dev/null +++ b/pyrogram/methods/bots/get_bot_default_privileges.py @@ -0,0 +1,57 @@ +# 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 . + +from typing import Optional + +import pyrogram +from pyrogram import raw +from pyrogram import types + + +class GetBotDefaultPrivileges: + async def get_bot_default_privileges( + self: "pyrogram.Client", + for_channels: bool = None + ) -> Optional["types.ChatPrivileges"]: + """Get the current default privileges of the bot. + + Parameters: + for_channels (``bool``, *optional*): + Pass True to get default privileges of the bot in channels. Otherwise, default privileges of the bot + for groups and supergroups will be returned. + + Returns: + ``bool``: On success, True is returned. + + Example: + .. code-block:: python + + privileges = await app.get_bot_default_privileges() + """ + + bot_info = await self.invoke( + raw.functions.users.GetFullUser( + id=raw.types.InputUserSelf() + ) + ) + + field = "bot_broadcast_admin_rights" if for_channels else "bot_group_admin_rights" + + admin_rights = getattr(bot_info.full_user, field) + + return types.ChatPrivileges._parse(admin_rights) if admin_rights else None diff --git a/pyrogram/methods/bots/set_bot_default_privileges.py b/pyrogram/methods/bots/set_bot_default_privileges.py new file mode 100644 index 00000000..52e48026 --- /dev/null +++ b/pyrogram/methods/bots/set_bot_default_privileges.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 pyrogram +from pyrogram import raw +from pyrogram import types + + +class SetBotDefaultPrivileges: + async def set_bot_default_privileges( + self: "pyrogram.Client", + privileges: "types.ChatPrivileges" = None, + for_channels: bool = None + ) -> bool: + """Change the default privileges requested by the bot when it's added as an administrator to groups or channels. + + These privileges will be suggested to users, but they are are free to modify the list before adding the bot. + + Parameters: + privileges (:obj:`~pyrogram.types.ChatPrivileges`): + New default privileges. None to clear. + Defaults to None. + + for_channels (``bool``, *optional*): + Pass True to change the default privileges of the bot in channels. Otherwise, the default privileges of + the bot for groups and supergroups will be changed. + + Returns: + ``bool``: On success, True is returned. + + Example: + .. code-block:: python + + from pyrogram.types import ChatPrivileges + + await app.set_bot_default_privileges( + ChatPrivileges( + can_delete_messages=True, + can_restrict_members=True + ) + ) + """ + + function = ( + raw.functions.bots.SetBotBroadcastDefaultAdminRights + if for_channels + else raw.functions.bots.SetBotGroupDefaultAdminRights + ) + + admin_rights = raw.types.ChatAdminRights( + change_info=privileges.can_change_info, + post_messages=privileges.can_post_messages, + edit_messages=privileges.can_edit_messages, + delete_messages=privileges.can_delete_messages, + ban_users=privileges.can_restrict_members, + invite_users=privileges.can_invite_users, + pin_messages=privileges.can_pin_messages, + add_admins=privileges.can_promote_members, + anonymous=privileges.is_anonymous, + manage_call=privileges.can_manage_video_chats, + other=privileges.can_manage_chat + ) if privileges else raw.types.ChatAdminRights() + + return await self.invoke(function(admin_rights=admin_rights))