Skip to content

Commit fd0044c

Browse files
committed
Add {get,set}_chat_menu_button
1 parent 173888f commit fd0044c

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

compiler/docs/compiler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ def get_title_list(s: str) -> list:
297297
delete_bot_commands
298298
set_bot_default_privileges
299299
get_bot_default_privileges
300+
set_chat_menu_button
301+
get_chat_menu_button
300302
""",
301303
authorization="""
302304
Authorization

pyrogram/methods/bots/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
from .delete_bot_commands import DeleteBotCommands
2222
from .get_bot_commands import GetBotCommands
2323
from .get_bot_default_privileges import GetBotDefaultPrivileges
24+
from .get_chat_menu_button import GetChatMenuButton
2425
from .get_game_high_scores import GetGameHighScores
2526
from .get_inline_bot_results import GetInlineBotResults
2627
from .request_callback_answer import RequestCallbackAnswer
2728
from .send_game import SendGame
2829
from .send_inline_bot_result import SendInlineBotResult
2930
from .set_bot_commands import SetBotCommands
3031
from .set_bot_default_privileges import SetBotDefaultPrivileges
32+
from .set_chat_menu_button import SetChatMenuButton
3133
from .set_game_score import SetGameScore
3234

3335

@@ -44,6 +46,8 @@ class Bots(
4446
GetBotCommands,
4547
DeleteBotCommands,
4648
SetBotDefaultPrivileges,
47-
GetBotDefaultPrivileges
49+
GetBotDefaultPrivileges,
50+
SetChatMenuButton,
51+
GetChatMenuButton
4852
):
4953
pass
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
from typing import Union
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
from pyrogram import types
24+
25+
26+
class GetChatMenuButton:
27+
async def get_chat_menu_button(
28+
self: "pyrogram.Client",
29+
chat_id: Union[int, str] = None,
30+
) -> "types.MenuButton":
31+
"""Get the current value of the bot's menu button in a private chat, or the default menu button.
32+
33+
chat_id (``int`` | ``str``):
34+
Unique identifier (int) or username (str) of the target chat.
35+
If not specified, default bot's menu button will be returned.
36+
"""
37+
38+
if chat_id:
39+
r = await self.invoke(
40+
raw.functions.bots.GetBotMenuButton(
41+
user_id=await self.resolve_peer(chat_id),
42+
)
43+
)
44+
else:
45+
r = (await self.invoke(
46+
raw.functions.users.GetFullUser(
47+
id=raw.types.InputUserSelf()
48+
)
49+
)).full_user.bot_info.menu_button
50+
51+
if isinstance(r, raw.types.BotMenuButtonCommands):
52+
return types.MenuButtonCommands()
53+
54+
if isinstance(r, raw.types.BotMenuButtonDefault):
55+
return types.MenuButtonDefault()
56+
57+
if isinstance(r, raw.types.BotMenuButton):
58+
return types.MenuButtonWebApp(
59+
text=r.text,
60+
web_app=types.WebAppInfo(
61+
url=r.url
62+
)
63+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
from typing import Union
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
from pyrogram import types
24+
25+
26+
class SetChatMenuButton:
27+
async def set_chat_menu_button(
28+
self: "pyrogram.Client",
29+
chat_id: Union[int, str] = None,
30+
menu_button: "types.MenuButton" = None
31+
) -> bool:
32+
"""Change the bot's menu button in a private chat, or the default menu button.
33+
34+
chat_id (``int`` | ``str``, *optional*):
35+
Unique identifier (int) or username (str) of the target chat.
36+
If not specified, default bot's menu button will be changed.
37+
38+
menu_button (:obj:`~pyrogram.types.MenuButton`, *optional*):
39+
The new bot's menu button.
40+
Defaults to :obj:`~pyrogram.types.MenuButtonDefault`.
41+
"""
42+
43+
await self.invoke(
44+
raw.functions.bots.SetBotMenuButton(
45+
user_id=await self.resolve_peer(chat_id or "me"),
46+
button=(
47+
(await menu_button.write(self)) if menu_button
48+
else (await types.MenuButtonDefault().write(self))
49+
)
50+
)
51+
)
52+
53+
return True

0 commit comments

Comments
 (0)