Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 7bfcd5a

Browse files
committed
Revamp bot commands and bot command scopes
Closes #777
1 parent aa41ac5 commit 7bfcd5a

16 files changed

+543
-95
lines changed

compiler/docs/compiler.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ def get_title_list(s: str) -> list:
288288
set_game_score
289289
get_game_high_scores
290290
set_bot_commands
291+
get_bot_commands
292+
delete_bot_commands
291293
""",
292294
authorization="""
293295
Authorization
@@ -395,8 +397,8 @@ def get_title_list(s: str) -> list:
395397
VoiceChatEnded
396398
VoiceChatMembersInvited
397399
""",
398-
bots_keyboard="""
399-
Bots & Keyboards
400+
bot_keyboards="""
401+
Bot keyboards
400402
ReplyKeyboardMarkup
401403
KeyboardButton
402404
ReplyKeyboardRemove
@@ -407,7 +409,18 @@ def get_title_list(s: str) -> list:
407409
CallbackQuery
408410
GameHighScore
409411
CallbackGame
412+
""",
413+
bot_commands="""
414+
Bot commands
410415
BotCommand
416+
BotCommandScope
417+
BotCommandScopeDefault
418+
BotCommandScopeAllPrivateChats
419+
BotCommandScopeAllGroupChats
420+
BotCommandScopeAllChatAdministrators
421+
BotCommandScopeChat
422+
BotCommandScopeChatAdministrators
423+
BotCommandScopeChatMember
411424
""",
412425
input_media="""
413426
Input Media

compiler/docs/template/types.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,31 @@ Messages & Media
5151

5252
{messages_media}
5353

54-
Bots & Keyboards
55-
----------------
54+
Bot keyboards
55+
-------------
56+
57+
.. autosummary::
58+
:nosignatures:
59+
60+
{bot_keyboards}
61+
62+
.. toctree::
63+
:hidden:
64+
65+
{bot_keyboards}
66+
67+
Bot commands
68+
-------------
5669

5770
.. autosummary::
5871
:nosignatures:
5972

60-
{bots_keyboard}
73+
{bot_commands}
6174

6275
.. toctree::
6376
:hidden:
6477

65-
{bots_keyboard}
78+
{bot_commands}
6679

6780
Input Media
6881
-----------

pyrogram/methods/bots/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from .answer_callback_query import AnswerCallbackQuery
2020
from .answer_inline_query import AnswerInlineQuery
21+
from .delete_bot_commands import DeleteBotCommands
22+
from .get_bot_commands import GetBotCommands
2123
from .get_game_high_scores import GetGameHighScores
2224
from .get_inline_bot_results import GetInlineBotResults
2325
from .request_callback_answer import RequestCallbackAnswer
@@ -36,6 +38,8 @@ class Bots(
3638
SendGame,
3739
SetGameScore,
3840
GetGameHighScores,
39-
SetBotCommands
41+
SetBotCommands,
42+
GetBotCommands,
43+
DeleteBotCommands
4044
):
4145
pass
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <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+
import pyrogram
20+
from pyrogram import raw, types
21+
from pyrogram.scaffold import Scaffold
22+
23+
24+
class DeleteBotCommands(Scaffold):
25+
async def delete_bot_commands(
26+
self: "pyrogram.Client",
27+
scope: "types.BotCommandScope" = types.BotCommandScopeDefault(),
28+
language_code: str = "",
29+
):
30+
"""Delete the list of the bot's commands for the given scope and user language.
31+
After deletion, higher level commands will be shown to affected users.
32+
33+
The commands passed will overwrite any command set previously.
34+
This method can be used by the own bot only.
35+
36+
Parameters:
37+
scope (:obj:`~pyrogram.types.BotCommandScope`, *optional*):
38+
An object describing the scope of users for which the commands are relevant.
39+
Defaults to :obj:`~pyrogram.types.BotCommandScopeDefault`.
40+
41+
language_code (``str``, *optional*):
42+
A two-letter ISO 639-1 language code.
43+
If empty, commands will be applied to all users from the given scope, for whose language there are no
44+
dedicated commands.
45+
46+
Returns:
47+
``bool``: On success, True is returned.
48+
49+
Example:
50+
.. code-block:: python
51+
52+
# Delete commands
53+
app.delete_bot_commands()
54+
"""
55+
56+
return await self.send(
57+
raw.functions.bots.ResetBotCommands(
58+
scope=await scope.write(self),
59+
lang_code=language_code,
60+
)
61+
)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <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+
import pyrogram
20+
from pyrogram import raw, types
21+
from pyrogram.scaffold import Scaffold
22+
23+
24+
class GetBotCommands(Scaffold):
25+
async def get_bot_commands(
26+
self: "pyrogram.Client",
27+
scope: "types.BotCommandScope" = types.BotCommandScopeDefault(),
28+
language_code: str = "",
29+
):
30+
"""Get the current list of the bot's commands for the given scope and user language.
31+
Returns Array of BotCommand on success. If commands aren't set, an empty list is returned.
32+
33+
The commands passed will overwrite any command set previously.
34+
This method can be used by the own bot only.
35+
36+
Parameters:
37+
scope (:obj:`~pyrogram.types.BotCommandScope`, *optional*):
38+
An object describing the scope of users for which the commands are relevant.
39+
Defaults to :obj:`~pyrogram.types.BotCommandScopeDefault`.
40+
41+
language_code (``str``, *optional*):
42+
A two-letter ISO 639-1 language code.
43+
If empty, commands will be applied to all users from the given scope, for whose language there are no
44+
dedicated commands.
45+
46+
Returns:
47+
List of :obj:`~pyrogram.types.BotCommand`: On success, the list of bot commands is returned.
48+
49+
Example:
50+
.. code-block:: python
51+
52+
# Get commands
53+
commands = app.get_bot_commands()
54+
print(commands)
55+
"""
56+
57+
return await self.send(
58+
raw.functions.bots.GetBotCommands(
59+
scope=await scope.write(self),
60+
lang_code=language_code,
61+
)
62+
)

pyrogram/methods/bots/set_bot_commands.py

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

21+
import pyrogram
2122
from pyrogram import raw, types
2223
from pyrogram.scaffold import Scaffold
2324

2425

2526
class SetBotCommands(Scaffold):
2627
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 = "",
28+
self: "pyrogram.Client",
29+
commands: List["types.BotCommand"],
30+
scope: "types.BotCommandScope" = types.BotCommandScopeDefault(),
31+
language_code: str = "",
3332
):
34-
"""Set the bot commands list.
33+
"""Set the list of the bot's commands.
3534
3635
The commands passed will overwrite any command set previously.
3736
This method can be used by the own bot only.
3837
3938
Parameters:
4039
commands (List of :obj:`~pyrogram.types.BotCommand`):
4140
A list of bot commands.
42-
Pass None to remove all commands.
41+
At most 100 commands can be specified.
42+
43+
scope (:obj:`~pyrogram.types.BotCommandScope`, *optional*):
44+
An object describing the scope of users for which the commands are relevant.
45+
Defaults to :obj:`~pyrogram.types.BotCommandScopeDefault`.
46+
47+
language_code (``str``, *optional*):
48+
A two-letter ISO 639-1 language code.
49+
If empty, commands will be applied to all users from the given scope, for whose language there are no
50+
dedicated commands.
4351
4452
Returns:
45-
``bool``: True on success, False otherwise.
53+
``bool``: On success, True is returned.
4654
4755
Example:
4856
.. code-block:: python
@@ -53,15 +61,12 @@ async def set_bot_commands(
5361
app.set_bot_commands([
5462
BotCommand("start", "Start the bot"),
5563
BotCommand("settings", "Bot settings")])
56-
57-
# Remove commands
58-
app.set_bot_commands(None)
5964
"""
6065

6166
return await self.send(
6267
raw.functions.bots.SetBotCommands(
63-
commands=[c.write() for c in commands or []],
64-
scope=scope.write(),
65-
lang_code=lang_code,
68+
commands=[c.write() for c in commands],
69+
scope=await scope.write(self),
70+
lang_code=language_code,
6671
)
6772
)

pyrogram/types/bots_and_keyboards/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
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, BotCommandScope
19+
from .bot_command import BotCommand
20+
from .bot_command_scope import BotCommandScope
21+
from .bot_command_scope_all_chat_administrators import BotCommandScopeAllChatAdministrators
22+
from .bot_command_scope_all_group_chats import BotCommandScopeAllGroupChats
23+
from .bot_command_scope_all_private_chats import BotCommandScopeAllPrivateChats
24+
from .bot_command_scope_chat import BotCommandScopeChat
25+
from .bot_command_scope_chat_administrators import BotCommandScopeChatAdministrators
26+
from .bot_command_scope_chat_member import BotCommandScopeChatMember
27+
from .bot_command_scope_default import BotCommandScopeDefault
2028
from .callback_game import CallbackGame
2129
from .callback_query import CallbackQuery
2230
from .force_reply import ForceReply
@@ -41,4 +49,11 @@
4149
"LoginUrl",
4250
"BotCommand",
4351
"BotCommandScope",
52+
"BotCommandScopeAllChatAdministrators",
53+
"BotCommandScopeAllGroupChats",
54+
"BotCommandScopeAllPrivateChats",
55+
"BotCommandScopeChat",
56+
"BotCommandScopeChatAdministrators",
57+
"BotCommandScopeChatMember",
58+
"BotCommandScopeDefault",
4459
]

0 commit comments

Comments
 (0)