Skip to content

Commit 4fb8de8

Browse files
author
Pyrogram-Mod - Dev
committed
Implement types for Layer 224 features.
Add KeyboardButtonStyle for button styling (bg colors, icons). Add StarGift and StarGiftUnique types for star gift handling. Add new MessageServiceType entries: STAR_GIFT, STAR_GIFT_UNIQUE, NEW_CREATOR_PENDING, CHANGE_CREATOR. Update KeyboardButton to support style parameter. Update Message parser to handle new service message actions.
1 parent 27de5ba commit 4fb8de8

7 files changed

Lines changed: 368 additions & 10 deletions

File tree

pyrogram/enums/message_service_type.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,15 @@ class MessageServiceType(AutoName):
7171

7272
WEB_APP_DATA = auto()
7373
"Web app data"
74+
75+
STAR_GIFT = auto()
76+
"Star gift received"
77+
78+
STAR_GIFT_UNIQUE = auto()
79+
"Unique/collectible star gift"
80+
81+
NEW_CREATOR_PENDING = auto()
82+
"New creator transfer pending"
83+
84+
CHANGE_CREATOR = auto()
85+
"Creator changed"

pyrogram/types/bots_and_keyboards/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from .inline_keyboard_button import InlineKeyboardButton
3333
from .inline_keyboard_markup import InlineKeyboardMarkup
3434
from .keyboard_button import KeyboardButton
35+
from .keyboard_button_style import KeyboardButtonStyle
3536
from .login_url import LoginUrl
3637
from .menu_button import MenuButton
3738
from .menu_button_commands import MenuButtonCommands
@@ -50,6 +51,7 @@
5051
"InlineKeyboardButton",
5152
"InlineKeyboardMarkup",
5253
"KeyboardButton",
54+
"KeyboardButtonStyle",
5355
"ReplyKeyboardMarkup",
5456
"ReplyKeyboardRemove",
5557
"LoginUrl",

pyrogram/types/bots_and_keyboards/keyboard_button.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,56 +40,70 @@ class KeyboardButton(Object):
4040
4141
web_app (:obj:`~pyrogram.types.WebAppInfo`, *optional*):
4242
If specified, the described `Web App <https://core.telegram.org/bots/webapps>`_ will be launched when the
43-
button is pressed. The Web App will be able to send a web_app_data service message. Available in private
43+
button is pressed. The Web App will be able to send a "web_app_data" service message. Available in private
4444
chats only.
4545
46+
style (:obj:`~pyrogram.types.KeyboardButtonStyle`, *optional*):
47+
Style configuration for the button (background color, icon).
48+
4649
"""
4750

4851
def __init__(
4952
self,
5053
text: str,
5154
request_contact: bool = None,
5255
request_location: bool = None,
53-
web_app: "types.WebAppInfo" = None
56+
web_app: "types.WebAppInfo" = None,
57+
style: "types.KeyboardButtonStyle" = None
5458
):
5559
super().__init__()
5660

5761
self.text = str(text)
5862
self.request_contact = request_contact
5963
self.request_location = request_location
6064
self.web_app = web_app
65+
self.style = style
6166

6267
@staticmethod
6368
def read(b):
69+
style = types.KeyboardButtonStyle.read(getattr(b, "style", None))
70+
6471
if isinstance(b, raw.types.KeyboardButton):
72+
if style:
73+
return KeyboardButton(text=b.text, style=style)
6574
return b.text
6675

6776
if isinstance(b, raw.types.KeyboardButtonRequestPhone):
6877
return KeyboardButton(
6978
text=b.text,
70-
request_contact=True
79+
request_contact=True,
80+
style=style
7181
)
7282

7383
if isinstance(b, raw.types.KeyboardButtonRequestGeoLocation):
7484
return KeyboardButton(
7585
text=b.text,
76-
request_location=True
86+
request_location=True,
87+
style=style
7788
)
7889

7990
if isinstance(b, raw.types.KeyboardButtonSimpleWebView):
8091
return KeyboardButton(
8192
text=b.text,
8293
web_app=types.WebAppInfo(
8394
url=b.url
84-
)
95+
),
96+
style=style
8597
)
8698

8799
def write(self):
100+
style = self.style.write() if self.style else None
101+
88102
if self.request_contact:
89-
return raw.types.KeyboardButtonRequestPhone(text=self.text)
103+
return raw.types.KeyboardButtonRequestPhone(text=self.text, style=style)
90104
elif self.request_location:
91-
return raw.types.KeyboardButtonRequestGeoLocation(text=self.text)
105+
return raw.types.KeyboardButtonRequestGeoLocation(text=self.text, style=style)
92106
elif self.web_app:
93-
return raw.types.KeyboardButtonSimpleWebView(text=self.text, url=self.web_app.url)
107+
return raw.types.KeyboardButtonSimpleWebView(text=self.text, url=self.web_app.url, style=style)
94108
else:
95-
return raw.types.KeyboardButton(text=self.text)
109+
return raw.types.KeyboardButton(text=self.text, style=style)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 pyrogram import raw
20+
from ..object import Object
21+
22+
23+
class KeyboardButtonStyle(Object):
24+
"""Style configuration for keyboard buttons.
25+
26+
Parameters:
27+
bg_primary (``bool``, *optional*):
28+
Use primary background color for the button.
29+
30+
bg_danger (``bool``, *optional*):
31+
Use danger/red background color for the button.
32+
33+
bg_success (``bool``, *optional*):
34+
Use success/green background color for the button.
35+
36+
icon (``int``, *optional*):
37+
Custom emoji ID to use as button icon.
38+
"""
39+
40+
def __init__(
41+
self,
42+
*,
43+
bg_primary: bool = None,
44+
bg_danger: bool = None,
45+
bg_success: bool = None,
46+
icon: int = None
47+
):
48+
super().__init__()
49+
50+
self.bg_primary = bg_primary
51+
self.bg_danger = bg_danger
52+
self.bg_success = bg_success
53+
self.icon = icon
54+
55+
@staticmethod
56+
def read(style: "raw.types.KeyboardButtonStyle") -> "KeyboardButtonStyle":
57+
if style is None:
58+
return None
59+
60+
return KeyboardButtonStyle(
61+
bg_primary=style.bg_primary or None,
62+
bg_danger=style.bg_danger or None,
63+
bg_success=style.bg_success or None,
64+
icon=style.icon
65+
)
66+
67+
def write(self) -> "raw.types.KeyboardButtonStyle":
68+
return raw.types.KeyboardButtonStyle(
69+
bg_primary=self.bg_primary,
70+
bg_danger=self.bg_danger,
71+
bg_success=self.bg_success,
72+
icon=self.icon
73+
)

pyrogram/types/messages_and_media/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@
4242
from .web_app_data import WebAppData
4343
from .web_page import WebPage
4444
from .message_reactions import MessageReactions
45+
from .star_gift import StarGift, StarGiftUnique
4546

4647
__all__ = [
4748
"Animation", "Audio", "Contact", "Document", "Game", "Location", "Message", "MessageEntity", "Photo", "Thumbnail",
4849
"StrippedThumbnail", "Poll", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice", "WebPage", "Dice",
49-
"Reaction", "WebAppData", "MessageReactions", "Story", "Giveaway", "AlternativeVideo"
50+
"Reaction", "WebAppData", "MessageReactions", "Story", "Giveaway", "AlternativeVideo", "StarGift", "StarGiftUnique"
5051
]

pyrogram/types/messages_and_media/message.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,14 @@ async def _parse(
573573
elif isinstance(action, raw.types.MessageActionWebViewDataSentMe):
574574
web_app_data = types.WebAppData._parse(action)
575575
service_type = enums.MessageServiceType.WEB_APP_DATA
576+
elif isinstance(action, raw.types.MessageActionStarGift):
577+
service_type = enums.MessageServiceType.STAR_GIFT
578+
elif isinstance(action, raw.types.MessageActionStarGiftUnique):
579+
service_type = enums.MessageServiceType.STAR_GIFT_UNIQUE
580+
elif isinstance(action, raw.types.MessageActionNewCreatorPending):
581+
service_type = enums.MessageServiceType.NEW_CREATOR_PENDING
582+
elif isinstance(action, raw.types.MessageActionChangeCreator):
583+
service_type = enums.MessageServiceType.CHANGE_CREATOR
576584

577585
from_user = types.User._parse(client, users.get(user_id, None))
578586
sender_chat = types.Chat._parse(client, message, users, chats, is_chat=False) if not from_user else None

0 commit comments

Comments
 (0)