Skip to content

Commit 293e852

Browse files
Add new method set_bot_commands (pyrogram#657)
* a new method set_bot_commands * Delete bot_commands_list.py * Update set_bot_commands.py * Update __init__.py * Update set_bot_commands.py * Update set_bot_commands.py * Update bot_command.py * Update set_bot_commands.py * Update set_bot_commands.py * Update compiler.py Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com>
1 parent ed3576e commit 293e852

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ def get_title_list(s: str) -> list:
394394
CallbackQuery
395395
GameHighScore
396396
CallbackGame
397+
BotCommand
397398
""",
398399
input_media="""
399400
Input Media

pyrogram/methods/bots/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .send_game import SendGame
2525
from .send_inline_bot_result import SendInlineBotResult
2626
from .set_game_score import SetGameScore
27+
from .set_bot_commands import SetBotCommands
2728

2829

2930
class Bots(
@@ -34,6 +35,7 @@ class Bots(
3435
SendInlineBotResult,
3536
SendGame,
3637
SetGameScore,
37-
GetGameHighScores
38+
GetGameHighScores,
39+
SetBotCommands
3840
):
3941
pass
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 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+
from typing import Optional, List
20+
21+
from pyrogram import raw
22+
from pyrogram import types
23+
from pyrogram.scaffold import Scaffold
24+
25+
26+
class SetBotCommands(Scaffold):
27+
async def set_bot_commands(self, commands: Optional[List[types.BotCommand]]):
28+
"""Set the bot commands list.
29+
30+
The commands passed will overwrite any command set previously.
31+
This method can be used by the own bot only.
32+
33+
Parameters:
34+
commands (List of :obj:`~pyrogram.types.BotCommand`):
35+
A list of bot commands.
36+
Pass None to remove all commands.
37+
38+
Returns:
39+
``bool``: True on success, False otherwise.
40+
41+
Example:
42+
.. code-block:: python
43+
44+
from pyrogram.types import BotCommand
45+
46+
# Set new commands
47+
app.set_bot_commands([
48+
BotCommand("start", "Start the bot"),
49+
BotCommand("settings", "Bot settings")])
50+
51+
# Remove commands
52+
app.set_bot_commands(None)
53+
"""
54+
55+
return await self.send(
56+
raw.functions.bots.SetBotCommands(
57+
commands=[c.write() for c in commands or []]
58+
)
59+
)

pyrogram/types/bots_and_keyboards/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .login_url import LoginUrl
2727
from .reply_keyboard_markup import ReplyKeyboardMarkup
2828
from .reply_keyboard_remove import ReplyKeyboardRemove
29+
from .bot_command import BotCommand
2930

3031
__all__ = [
3132
"CallbackGame",
@@ -37,5 +38,6 @@
3738
"KeyboardButton",
3839
"ReplyKeyboardMarkup",
3940
"ReplyKeyboardRemove",
40-
"LoginUrl"
41+
"LoginUrl",
42+
"BotCommand",
4143
]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 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+
from pyrogram import raw
20+
from ..object import Object
21+
22+
23+
class BotCommand(Object):
24+
"""A bot command with the standard slash "/" prefix.
25+
26+
Parameters:
27+
command (``str``):
28+
The bot command, for example: "/start".
29+
30+
description (``str``):
31+
Description of the bot command.
32+
"""
33+
34+
def __init__(self, command: str, description: str):
35+
super().__init__()
36+
37+
self.command = command
38+
self.description = description
39+
40+
def write(self):
41+
return raw.types.BotCommand(
42+
command=self.command,
43+
description=self.description
44+
)

0 commit comments

Comments
 (0)