Skip to content

Commit aa41ac5

Browse files
authored
Fix issue in set_bot_commands (pyrogram#778)
1 parent 65a53ae commit aa41ac5

3 files changed

Lines changed: 91 additions & 12 deletions

File tree

pyrogram/methods/bots/set_bot_commands.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@
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 typing import Optional, List
19+
from typing import List, Optional
2020

21-
from pyrogram import raw
22-
from pyrogram import types
21+
from pyrogram import raw, types
2322
from pyrogram.scaffold import Scaffold
2423

2524

2625
class SetBotCommands(Scaffold):
27-
async def set_bot_commands(self, commands: Optional[List[types.BotCommand]]):
26+
async def set_bot_commands(
27+
self,
28+
commands: Optional[List[types.BotCommand]],
29+
scope: types.BotCommandScope = types.BotCommandScope(
30+
types.BotCommandScope.DEFAULT
31+
),
32+
lang_code: str = "",
33+
):
2834
"""Set the bot commands list.
29-
35+
3036
The commands passed will overwrite any command set previously.
3137
This method can be used by the own bot only.
3238
@@ -40,20 +46,22 @@ async def set_bot_commands(self, commands: Optional[List[types.BotCommand]]):
4046
4147
Example:
4248
.. code-block:: python
43-
49+
4450
from pyrogram.types import BotCommand
45-
51+
4652
# Set new commands
4753
app.set_bot_commands([
4854
BotCommand("start", "Start the bot"),
4955
BotCommand("settings", "Bot settings")])
50-
56+
5157
# Remove commands
5258
app.set_bot_commands(None)
5359
"""
5460

5561
return await self.send(
5662
raw.functions.bots.SetBotCommands(
57-
commands=[c.write() for c in commands or []]
63+
commands=[c.write() for c in commands or []],
64+
scope=scope.write(),
65+
lang_code=lang_code,
5866
)
5967
)

pyrogram/types/bots_and_keyboards/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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 .bot_command import BotCommand
19+
from .bot_command import BotCommand, BotCommandScope
2020
from .callback_game import CallbackGame
2121
from .callback_query import CallbackQuery
2222
from .force_reply import ForceReply
@@ -40,4 +40,5 @@
4040
"ReplyKeyboardRemove",
4141
"LoginUrl",
4242
"BotCommand",
43+
"BotCommandScope",
4344
]

pyrogram/types/bots_and_keyboards/bot_command.py

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from pyrogram import raw
20+
2021
from ..object import Object
2122

2223

@@ -26,7 +27,7 @@ class BotCommand(Object):
2627
Parameters:
2728
command (``str``):
2829
The bot command, for example: "/start".
29-
30+
3031
description (``str``):
3132
Description of the bot command.
3233
"""
@@ -40,5 +41,74 @@ def __init__(self, command: str, description: str):
4041
def write(self):
4142
return raw.types.BotCommand(
4243
command=self.command,
43-
description=self.description
44+
description=self.description,
4445
)
46+
47+
48+
class BotCommandScope(Object):
49+
"""
50+
Represents a scope where the bot commands, specified
51+
using bots.setBotCommands will be valid.
52+
53+
Parameters:
54+
scope (``str``):
55+
56+
- DEFAULT: The commands will be valid in all chats (default value)
57+
58+
- PRIVATE: The specified bot commands will only be valid in all private
59+
chats with users.
60+
61+
- GROUP: The specified bot commands will be valid in all groups and supergroups
62+
63+
- GROUP_ADMINS: The specified bot commands will be valid only for chat
64+
administrators, in all groups and supergroups.
65+
66+
- PEER: The specified bot commands will be valid only in a specific dialog
67+
68+
- PEER_ADMINS: The specified bot commands will be valid for all admins of the
69+
specified group or supergroup.
70+
71+
- PEER_USER: The specified bot commands will be valid only for a specific user
72+
in the specified chat
73+
"""
74+
75+
DEFAULT = "default"
76+
PRIVATE = "users"
77+
GROUP = "chats"
78+
GROUP_ADMINS = "chat_admins"
79+
PEER = "peer"
80+
PEER_ADMINS = "peer_admins"
81+
PEER_USER = "peer_user"
82+
83+
raw_scopes = {
84+
DEFAULT: raw.types.BotCommandScopeDefault,
85+
PRIVATE: raw.types.BotCommandScopeUsers,
86+
GROUP: raw.types.BotCommandScopeChats,
87+
GROUP_ADMINS: raw.types.BotCommandScopeChatAdmins,
88+
PEER: lambda peer: raw.types.BotCommandScopePeer(peer),
89+
PEER_ADMINS: lambda peer: raw.types.BotCommandScopePeerAdmins(peer),
90+
PEER_USER: lambda peer, user_id: raw.types.BotCommandScopePeerUser(
91+
peer, user_id
92+
),
93+
}
94+
95+
def __init__(
96+
self,
97+
scope: str,
98+
peer: raw.types.InputPeerUser = None,
99+
user_id: raw.types.InputUser = None,
100+
):
101+
super().__init__()
102+
self.scope = scope
103+
self.peer = peer
104+
self.user_id = user_id
105+
106+
def write(self):
107+
108+
if self.scope in ["peer", "peer_admins"]:
109+
return self.raw_scopes[self.scope](self.peer)
110+
111+
elif self.scope == "peer_user":
112+
return self.raw_scopes[self.scopes](self.peer, self.user_id)
113+
114+
return self.raw_scopes[self.scope]()

0 commit comments

Comments
 (0)