From 293e852afd622f996596c59378ac6a31e56742cf Mon Sep 17 00:00:00 2001 From: Jonathan <49692607+jonatan1609@users.noreply.github.com> Date: Tue, 11 May 2021 11:22:17 +0300 Subject: [PATCH] Add new method set_bot_commands (#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> --- compiler/docs/compiler.py | 1 + pyrogram/methods/bots/__init__.py | 4 +- pyrogram/methods/bots/set_bot_commands.py | 59 +++++++++++++++++++ pyrogram/types/bots_and_keyboards/__init__.py | 4 +- .../types/bots_and_keyboards/bot_command.py | 44 ++++++++++++++ 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 pyrogram/methods/bots/set_bot_commands.py create mode 100644 pyrogram/types/bots_and_keyboards/bot_command.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 9513899a..a5ce87e4 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -394,6 +394,7 @@ def pyrogram_api(): CallbackQuery GameHighScore CallbackGame + BotCommand """, input_media=""" Input Media diff --git a/pyrogram/methods/bots/__init__.py b/pyrogram/methods/bots/__init__.py index 2c528a15..8cc7dd30 100644 --- a/pyrogram/methods/bots/__init__.py +++ b/pyrogram/methods/bots/__init__.py @@ -24,6 +24,7 @@ from .request_callback_answer import RequestCallbackAnswer from .send_game import SendGame from .send_inline_bot_result import SendInlineBotResult from .set_game_score import SetGameScore +from .set_bot_commands import SetBotCommands class Bots( @@ -34,6 +35,7 @@ class Bots( SendInlineBotResult, SendGame, SetGameScore, - GetGameHighScores + GetGameHighScores, + SetBotCommands ): pass diff --git a/pyrogram/methods/bots/set_bot_commands.py b/pyrogram/methods/bots/set_bot_commands.py new file mode 100644 index 00000000..3a3c8f54 --- /dev/null +++ b/pyrogram/methods/bots/set_bot_commands.py @@ -0,0 +1,59 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 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, List + +from pyrogram import raw +from pyrogram import types +from pyrogram.scaffold import Scaffold + + +class SetBotCommands(Scaffold): + async def set_bot_commands(self, commands: Optional[List[types.BotCommand]]): + """Set the bot commands list. + + The commands passed will overwrite any command set previously. + This method can be used by the own bot only. + + Parameters: + commands (List of :obj:`~pyrogram.types.BotCommand`): + A list of bot commands. + Pass None to remove all commands. + + Returns: + ``bool``: True on success, False otherwise. + + 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 []] + ) + ) diff --git a/pyrogram/types/bots_and_keyboards/__init__.py b/pyrogram/types/bots_and_keyboards/__init__.py index 9e486cb5..a1f3a662 100644 --- a/pyrogram/types/bots_and_keyboards/__init__.py +++ b/pyrogram/types/bots_and_keyboards/__init__.py @@ -26,6 +26,7 @@ from .keyboard_button import KeyboardButton from .login_url import LoginUrl from .reply_keyboard_markup import ReplyKeyboardMarkup from .reply_keyboard_remove import ReplyKeyboardRemove +from .bot_command import BotCommand __all__ = [ "CallbackGame", @@ -37,5 +38,6 @@ __all__ = [ "KeyboardButton", "ReplyKeyboardMarkup", "ReplyKeyboardRemove", - "LoginUrl" + "LoginUrl", + "BotCommand", ] diff --git a/pyrogram/types/bots_and_keyboards/bot_command.py b/pyrogram/types/bots_and_keyboards/bot_command.py new file mode 100644 index 00000000..f2db126a --- /dev/null +++ b/pyrogram/types/bots_and_keyboards/bot_command.py @@ -0,0 +1,44 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 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 pyrogram import raw +from ..object import Object + + +class BotCommand(Object): + """A bot command with the standard slash "/" prefix. + + Parameters: + command (``str``): + The bot command, for example: "/start". + + description (``str``): + Description of the bot command. + """ + + def __init__(self, command: str, description: str): + super().__init__() + + self.command = command + self.description = description + + def write(self): + return raw.types.BotCommand( + command=self.command, + description=self.description + )