Skip to content

Commit 3f69bf0

Browse files
author
Pyrogram-Mod - Dev
committed
Add handlers, decorators and types for new Update types (layer 229)
New update handlers: - BotStarsSubscriptionHandler (updateBotStarsSubscription) - EphemeralBotCallbackQueryHandler (updateEphemeralBotCallbackQuery) - UserPhotoHandler (updateUserPhoto) New decorators: - on_bot_stars_subscription() - on_ephemeral_bot_callback_query() - on_user_photo() New high-level types: - BotStarsSubscription (canceled, payment_failed, restored, user_id, payload, qts) - EphemeralBotCallbackQuery (query_id, user, message_id, data, message, chat_id) - UserPhoto (user, date, previous, photo_id) Updated: dispatcher parsers, handlers/__init__, decorators/__init__, types/__init__, docs compiler
1 parent c6e55ff commit 3f69bf0

17 files changed

Lines changed: 644 additions & 2 deletions

compiler/docs/compiler.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ def get_title_list(s: str) -> list:
237237
on_disconnect
238238
on_chat_join_request
239239
on_guard_bot_query
240+
on_bot_stars_subscription
241+
on_ephemeral_bot_callback_query
242+
on_user_photo
240243
""",
241244
messages="""
242245
Messages
@@ -545,6 +548,7 @@ def get_title_list(s: str) -> list:
545548
EmojiStatus
546549
GuardBotQuery
547550
PeerColor
551+
UserPhoto
548552
""",
549553
messages_media="""
550554
Messages & Media
@@ -590,6 +594,7 @@ def get_title_list(s: str) -> list:
590594
WebViewResult
591595
WelcomeMessages
592596
CallbackAnswer
597+
EphemeralBotCallbackQuery
593598
""",
594599
bot_keyboards="""
595600
Bot keyboards
@@ -665,7 +670,11 @@ def get_title_list(s: str) -> list:
665670
Authorization
666671
SentCode
667672
TermsOfService
668-
"""
673+
""",
674+
payments="""
675+
Payments
676+
BotStarsSubscription
677+
""",
669678
)
670679

671680
root = PYROGRAM_API_DEST + "/types"

pyrogram/dispatcher.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler,
2929
ChosenInlineResultHandler, ChatMemberUpdatedHandler, ChatJoinRequestHandler,
3030
GuardBotQueryHandler,
31+
BotStarsSubscriptionHandler, EphemeralBotCallbackQueryHandler, UserPhotoHandler,
3132
)
3233
from pyrogram.raw.types import (
3334
UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage,
@@ -37,6 +38,7 @@
3738
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll,
3839
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant,
3940
UpdateBotChatInviteRequester, UpdateBotGuestChatQuery,
41+
UpdateBotStarsSubscription, UpdateEphemeralBotCallbackQuery, UpdateUserPhoto,
4042
)
4143

4244
log = logging.getLogger(__name__)
@@ -54,6 +56,9 @@ class Dispatcher:
5456
CHOSEN_INLINE_RESULT_UPDATES = (UpdateBotInlineSend,)
5557
CHAT_JOIN_REQUEST_UPDATES = (UpdateBotChatInviteRequester,)
5658
GUARD_BOT_QUERY_UPDATES = (UpdateBotGuestChatQuery,)
59+
BOT_STARS_SUBSCRIPTION_UPDATES = (UpdateBotStarsSubscription,)
60+
EPHEMERAL_BOT_CALLBACK_QUERY_UPDATES = (UpdateEphemeralBotCallbackQuery,)
61+
USER_PHOTO_UPDATES = (UpdateUserPhoto,)
5762

5863
def __init__(self, client: "pyrogram.Client"):
5964
self.client = client
@@ -135,6 +140,24 @@ async def guard_bot_query_parser(update, users, chats):
135140
GuardBotQueryHandler,
136141
)
137142

143+
async def bot_stars_subscription_parser(update, users, chats):
144+
return (
145+
pyrogram.types.BotStarsSubscription._parse(self.client, update),
146+
BotStarsSubscriptionHandler,
147+
)
148+
149+
async def ephemeral_bot_callback_query_parser(update, users, chats):
150+
return (
151+
pyrogram.types.EphemeralBotCallbackQuery._parse(self.client, update, users),
152+
EphemeralBotCallbackQueryHandler,
153+
)
154+
155+
async def user_photo_parser(update, users, chats):
156+
return (
157+
pyrogram.types.UserPhoto._parse(self.client, update, users),
158+
UserPhotoHandler,
159+
)
160+
138161
self.update_parsers = {
139162
Dispatcher.NEW_MESSAGE_UPDATES: message_parser,
140163
Dispatcher.EDIT_MESSAGE_UPDATES: edited_message_parser,
@@ -147,6 +170,9 @@ async def guard_bot_query_parser(update, users, chats):
147170
Dispatcher.CHAT_MEMBER_UPDATES: chat_member_updated_parser,
148171
Dispatcher.CHAT_JOIN_REQUEST_UPDATES: chat_join_request_parser,
149172
Dispatcher.GUARD_BOT_QUERY_UPDATES: guard_bot_query_parser,
173+
Dispatcher.BOT_STARS_SUBSCRIPTION_UPDATES: bot_stars_subscription_parser,
174+
Dispatcher.EPHEMERAL_BOT_CALLBACK_QUERY_UPDATES: ephemeral_bot_callback_query_parser,
175+
Dispatcher.USER_PHOTO_UPDATES: user_photo_parser,
150176
}
151177

152178
self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}

pyrogram/handlers/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@
2929
from .poll_handler import PollHandler
3030
from .raw_update_handler import RawUpdateHandler
3131
from .user_status_handler import UserStatusHandler
32+
from .bot_stars_subscription_handler import BotStarsSubscriptionHandler
33+
from .ephemeral_bot_callback_query_handler import EphemeralBotCallbackQueryHandler
34+
from .user_photo_handler import UserPhotoHandler
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 Callable
20+
21+
from .handler import Handler
22+
23+
24+
class BotStarsSubscriptionHandler(Handler):
25+
"""The BotStarsSubscription handler class. Used to handle bot stars subscription updates.
26+
27+
It is intended to be used with :meth:`~pyrogram.Client.add_handler`.
28+
29+
For a nicer way to register this handler, have a look at the
30+
:meth:`~pyrogram.Client.on_bot_stars_subscription` decorator.
31+
32+
Parameters:
33+
callback (``Callable``):
34+
Pass a function that will be called when a new bot stars subscription update arrives.
35+
It takes *(client, bot_stars_subscription)* as positional arguments.
36+
37+
filters (:obj:`Filters`):
38+
Pass one or more filters to allow only a subset of updates to be passed.
39+
40+
Other parameters:
41+
client (:obj:`~pyrogram.Client`):
42+
The Client itself.
43+
44+
bot_stars_subscription (:obj:`~pyrogram.types.BotStarsSubscription`):
45+
The bot stars subscription update.
46+
"""
47+
48+
def __init__(self, callback: Callable, filters=None):
49+
super().__init__(callback, filters)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 Callable
20+
21+
from .handler import Handler
22+
23+
24+
class EphemeralBotCallbackQueryHandler(Handler):
25+
"""The EphemeralBotCallbackQuery handler class. Used to handle ephemeral bot callback queries.
26+
27+
It is intended to be used with :meth:`~pyrogram.Client.add_handler`.
28+
29+
For a nicer way to register this handler, have a look at the
30+
:meth:`~pyrogram.Client.on_ephemeral_bot_callback_query` decorator.
31+
32+
Parameters:
33+
callback (``Callable``):
34+
Pass a function that will be called when a new ephemeral callback query arrives.
35+
It takes *(client, ephemeral_bot_callback_query)* as positional arguments.
36+
37+
filters (:obj:`Filters`):
38+
Pass one or more filters to allow only a subset of queries to be passed.
39+
40+
Other parameters:
41+
client (:obj:`~pyrogram.Client`):
42+
The Client itself.
43+
44+
ephemeral_bot_callback_query (:obj:`~pyrogram.types.EphemeralBotCallbackQuery`):
45+
The ephemeral bot callback query.
46+
"""
47+
48+
def __init__(self, callback: Callable, filters=None):
49+
super().__init__(callback, filters)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 Callable
20+
21+
from .handler import Handler
22+
23+
24+
class UserPhotoHandler(Handler):
25+
"""The UserPhoto handler class. Used to handle user photo updates.
26+
27+
It is intended to be used with :meth:`~pyrogram.Client.add_handler`.
28+
29+
For a nicer way to register this handler, have a look at the
30+
:meth:`~pyrogram.Client.on_user_photo` decorator.
31+
32+
Parameters:
33+
callback (``Callable``):
34+
Pass a function that will be called when a new user photo update arrives.
35+
It takes *(client, user_photo)* as positional arguments.
36+
37+
filters (:obj:`Filters`):
38+
Pass one or more filters to allow only a subset of updates to be passed.
39+
40+
Other parameters:
41+
client (:obj:`~pyrogram.Client`):
42+
The Client itself.
43+
44+
user_photo (:obj:`~pyrogram.types.UserPhoto`):
45+
The user photo update.
46+
"""
47+
48+
def __init__(self, callback: Callable, filters=None):
49+
super().__init__(callback, filters)

pyrogram/methods/decorators/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
from .on_poll import OnPoll
3030
from .on_raw_update import OnRawUpdate
3131
from .on_user_status import OnUserStatus
32+
from .on_bot_stars_subscription import OnBotStarsSubscription
33+
from .on_ephemeral_bot_callback_query import OnEphemeralBotCallbackQuery
34+
from .on_user_photo import OnUserPhoto
3235

3336

3437
class Decorators(
@@ -45,5 +48,8 @@ class Decorators(
4548
OnChatMemberUpdated,
4649
OnChatJoinRequest,
4750
OnGuardBotQuery,
51+
OnBotStarsSubscription,
52+
OnEphemeralBotCallbackQuery,
53+
OnUserPhoto,
4854
):
4955
pass
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 Callable
20+
21+
import pyrogram
22+
from pyrogram.filters import Filter
23+
24+
25+
class OnBotStarsSubscription:
26+
def on_bot_stars_subscription(
27+
self=None,
28+
filters=None,
29+
group: int = 0
30+
) -> Callable:
31+
"""Decorator for handling bot stars subscription updates.
32+
33+
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the
34+
:obj:`~pyrogram.handlers.BotStarsSubscriptionHandler`.
35+
36+
Parameters:
37+
filters (:obj:`~pyrogram.filters`, *optional*):
38+
Pass one or more filters to allow only a subset of updates to be passed.
39+
40+
group (``int``, *optional*):
41+
The group identifier, defaults to 0.
42+
"""
43+
44+
def decorator(func: Callable) -> Callable:
45+
if isinstance(self, pyrogram.Client):
46+
self.add_handler(pyrogram.handlers.BotStarsSubscriptionHandler(func, filters), group)
47+
elif isinstance(self, Filter) or self is None:
48+
if not hasattr(func, "handlers"):
49+
func.handlers = []
50+
51+
func.handlers.append(
52+
(
53+
pyrogram.handlers.BotStarsSubscriptionHandler(func, self),
54+
group if filters is None else filters
55+
)
56+
)
57+
58+
return func
59+
60+
return decorator
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 Callable
20+
21+
import pyrogram
22+
from pyrogram.filters import Filter
23+
24+
25+
class OnEphemeralBotCallbackQuery:
26+
def on_ephemeral_bot_callback_query(
27+
self=None,
28+
filters=None,
29+
group: int = 0
30+
) -> Callable:
31+
"""Decorator for handling ephemeral bot callback queries.
32+
33+
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the
34+
:obj:`~pyrogram.handlers.EphemeralBotCallbackQueryHandler`.
35+
36+
Parameters:
37+
filters (:obj:`~pyrogram.filters`, *optional*):
38+
Pass one or more filters to allow only a subset of queries to be passed.
39+
40+
group (``int``, *optional*):
41+
The group identifier, defaults to 0.
42+
"""
43+
44+
def decorator(func: Callable) -> Callable:
45+
if isinstance(self, pyrogram.Client):
46+
self.add_handler(pyrogram.handlers.EphemeralBotCallbackQueryHandler(func, filters), group)
47+
elif isinstance(self, Filter) or self is None:
48+
if not hasattr(func, "handlers"):
49+
func.handlers = []
50+
51+
func.handlers.append(
52+
(
53+
pyrogram.handlers.EphemeralBotCallbackQueryHandler(func, self),
54+
group if filters is None else filters
55+
)
56+
)
57+
58+
return func
59+
60+
return decorator

0 commit comments

Comments
 (0)