From aa41ac5fd252146735f32f460c44fab27fcf01e7 Mon Sep 17 00:00:00 2001 From: Andrea Maugeri Date: Mon, 28 Mar 2022 11:48:15 +0200 Subject: [PATCH] Fix issue in set_bot_commands (#778) --- pyrogram/methods/bots/set_bot_commands.py | 26 ++++--- pyrogram/types/bots_and_keyboards/__init__.py | 3 +- .../types/bots_and_keyboards/bot_command.py | 74 ++++++++++++++++++- 3 files changed, 91 insertions(+), 12 deletions(-) diff --git a/pyrogram/methods/bots/set_bot_commands.py b/pyrogram/methods/bots/set_bot_commands.py index 02158c0c..769a4301 100644 --- a/pyrogram/methods/bots/set_bot_commands.py +++ b/pyrogram/methods/bots/set_bot_commands.py @@ -16,17 +16,23 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import Optional, List +from typing import List, Optional -from pyrogram import raw -from pyrogram import types +from pyrogram import raw, types from pyrogram.scaffold import Scaffold class SetBotCommands(Scaffold): - async def set_bot_commands(self, commands: Optional[List[types.BotCommand]]): + async def set_bot_commands( + self, + commands: Optional[List[types.BotCommand]], + scope: types.BotCommandScope = types.BotCommandScope( + types.BotCommandScope.DEFAULT + ), + lang_code: str = "", + ): """Set the bot commands list. - + The commands passed will overwrite any command set previously. This method can be used by the own bot only. @@ -40,20 +46,22 @@ class SetBotCommands(Scaffold): Example: .. code-block:: python - + from pyrogram.types import BotCommand - + # Set new commands app.set_bot_commands([ BotCommand("start", "Start the bot"), BotCommand("settings", "Bot settings")]) - + # Remove commands app.set_bot_commands(None) """ return await self.send( raw.functions.bots.SetBotCommands( - commands=[c.write() for c in commands or []] + commands=[c.write() for c in commands or []], + scope=scope.write(), + lang_code=lang_code, ) ) diff --git a/pyrogram/types/bots_and_keyboards/__init__.py b/pyrogram/types/bots_and_keyboards/__init__.py index ccf84993..d69c70fa 100644 --- a/pyrogram/types/bots_and_keyboards/__init__.py +++ b/pyrogram/types/bots_and_keyboards/__init__.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from .bot_command import BotCommand +from .bot_command import BotCommand, BotCommandScope from .callback_game import CallbackGame from .callback_query import CallbackQuery from .force_reply import ForceReply @@ -40,4 +40,5 @@ __all__ = [ "ReplyKeyboardRemove", "LoginUrl", "BotCommand", + "BotCommandScope", ] diff --git a/pyrogram/types/bots_and_keyboards/bot_command.py b/pyrogram/types/bots_and_keyboards/bot_command.py index 66412f09..82cdc67e 100644 --- a/pyrogram/types/bots_and_keyboards/bot_command.py +++ b/pyrogram/types/bots_and_keyboards/bot_command.py @@ -17,6 +17,7 @@ # along with Pyrogram. If not, see . from pyrogram import raw + from ..object import Object @@ -26,7 +27,7 @@ class BotCommand(Object): Parameters: command (``str``): The bot command, for example: "/start". - + description (``str``): Description of the bot command. """ @@ -40,5 +41,74 @@ class BotCommand(Object): def write(self): return raw.types.BotCommand( command=self.command, - description=self.description + description=self.description, ) + + +class BotCommandScope(Object): + """ + Represents a scope where the bot commands, specified + using bots.setBotCommands will be valid. + + Parameters: + scope (``str``): + + - DEFAULT: The commands will be valid in all chats (default value) + + - PRIVATE: The specified bot commands will only be valid in all private + chats with users. + + - GROUP: The specified bot commands will be valid in all groups and supergroups + + - GROUP_ADMINS: The specified bot commands will be valid only for chat + administrators, in all groups and supergroups. + + - PEER: The specified bot commands will be valid only in a specific dialog + + - PEER_ADMINS: The specified bot commands will be valid for all admins of the + specified group or supergroup. + + - PEER_USER: The specified bot commands will be valid only for a specific user + in the specified chat + """ + + DEFAULT = "default" + PRIVATE = "users" + GROUP = "chats" + GROUP_ADMINS = "chat_admins" + PEER = "peer" + PEER_ADMINS = "peer_admins" + PEER_USER = "peer_user" + + raw_scopes = { + DEFAULT: raw.types.BotCommandScopeDefault, + PRIVATE: raw.types.BotCommandScopeUsers, + GROUP: raw.types.BotCommandScopeChats, + GROUP_ADMINS: raw.types.BotCommandScopeChatAdmins, + PEER: lambda peer: raw.types.BotCommandScopePeer(peer), + PEER_ADMINS: lambda peer: raw.types.BotCommandScopePeerAdmins(peer), + PEER_USER: lambda peer, user_id: raw.types.BotCommandScopePeerUser( + peer, user_id + ), + } + + def __init__( + self, + scope: str, + peer: raw.types.InputPeerUser = None, + user_id: raw.types.InputUser = None, + ): + super().__init__() + self.scope = scope + self.peer = peer + self.user_id = user_id + + def write(self): + + if self.scope in ["peer", "peer_admins"]: + return self.raw_scopes[self.scope](self.peer) + + elif self.scope == "peer_user": + return self.raw_scopes[self.scopes](self.peer, self.user_id) + + return self.raw_scopes[self.scope]()