2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Fix issue in set_bot_commands (#778)

This commit is contained in:
Andrea Maugeri 2022-03-28 11:48:15 +02:00 committed by GitHub
parent 65a53aeeb3
commit aa41ac5fd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 12 deletions

View File

@ -16,17 +16,23 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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,
)
)

View File

@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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",
]

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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]()