Skip to content

Commit a714e55

Browse files
authored
Add on_bot_purchased_paid_media and on_bot_business_connection. (#131)
1 parent 771bd60 commit a714e55

File tree

13 files changed

+333
-15
lines changed

13 files changed

+333
-15
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ def get_title_list(s: str) -> list:
644644
InputPaidMedia
645645
InputPaidMediaPhoto
646646
InputPaidMediaVideo
647+
PaidMediaPurchased
647648
""",
648649
input_message_content="""
649650
InputMessageContent

docs/source/api/decorators.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ Index
3333

3434
- :meth:`~Client.on_message`
3535
- :meth:`~Client.on_edited_message`
36-
36+
- :meth:`~Client.on_bot_business_connection`
3737
- :meth:`~Client.on_message_reaction_updated`
3838
- :meth:`~Client.on_message_reaction_count_updated`
3939
- :meth:`~Client.on_inline_query`
4040
- :meth:`~Client.on_chosen_inline_result`
4141
- :meth:`~Client.on_callback_query`
42-
43-
42+
- :meth:`~Client.on_shipping_query`
43+
- :meth:`~Client.on_pre_checkout_query`
44+
- :meth:`~Client.on_bot_purchased_paid_media`
4445
- :meth:`~Client.on_poll`
4546

46-
4747
- :meth:`~Client.on_chat_member_updated`
4848
- :meth:`~Client.on_chat_join_request`
4949

@@ -62,17 +62,17 @@ Details
6262
.. Decorators
6363
.. autodecorator:: pyrogram.Client.on_message()
6464
.. autodecorator:: pyrogram.Client.on_edited_message()
65-
65+
.. autodecorator:: pyrogram.Client.on_bot_business_connection()
6666
.. autodecorator:: pyrogram.Client.on_message_reaction_updated()
6767
.. autodecorator:: pyrogram.Client.on_message_reaction_count_updated()
6868
.. autodecorator:: pyrogram.Client.on_inline_query()
6969
.. autodecorator:: pyrogram.Client.on_chosen_inline_result()
7070
.. autodecorator:: pyrogram.Client.on_callback_query()
7171
.. autodecorator:: pyrogram.Client.on_shipping_query()
7272
.. autodecorator:: pyrogram.Client.on_pre_checkout_query()
73+
.. autodecorator:: pyrogram.Client.on_bot_purchased_paid_media()
7374
.. autodecorator:: pyrogram.Client.on_poll()
7475

75-
7676
.. autodecorator:: pyrogram.Client.on_chat_member_updated()
7777
.. autodecorator:: pyrogram.Client.on_chat_join_request()
7878

docs/source/api/handlers.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,23 @@ Index
3333

3434
- :class:`MessageHandler`
3535
- :class:`EditedMessageHandler`
36-
36+
- :class:`BusinessBotConnectionHandler`
3737
- :class:`MessageReactionUpdatedHandler`
3838
- :class:`MessageReactionCountUpdatedHandler`
3939
- :class:`InlineQueryHandler`
4040
- :class:`ChosenInlineResultHandler`
4141
- :class:`CallbackQueryHandler`
42-
43-
42+
- :class:`ShippingQueryHandler`
43+
- :class:`PreCheckoutQueryHandler`
44+
- :class:`PurchasedPaidMediaHandler`
4445
- :class:`PollHandler`
4546

46-
4747
- :class:`ChatMemberUpdatedHandler`
4848
- :class:`ChatJoinRequestHandler`
4949

5050

5151
- :class:`DeletedMessagesHandler`
5252
- :class:`UserStatusHandler`
53-
5453
- :class:`DisconnectHandler`
5554
- :class:`StoryHandler`
5655
- :class:`RawUpdateHandler`
@@ -63,17 +62,17 @@ Details
6362
.. Handlers
6463
.. autoclass:: MessageHandler()
6564
.. autoclass:: EditedMessageHandler()
66-
65+
.. autoclass:: BusinessBotConnectionHandler()
6766
.. autoclass:: MessageReactionUpdatedHandler()
6867
.. autoclass:: MessageReactionCountUpdatedHandler()
6968
.. autoclass:: InlineQueryHandler()
7069
.. autoclass:: ChosenInlineResultHandler()
7170
.. autoclass:: CallbackQueryHandler()
7271
.. autoclass:: ShippingQueryHandler()
7372
.. autoclass:: PreCheckoutQueryHandler()
73+
.. autoclass:: PurchasedPaidMediaHandler()
7474
.. autoclass:: PollHandler()
7575

76-
7776
.. autoclass:: ChatMemberUpdatedHandler()
7877
.. autoclass:: ChatJoinRequestHandler()
7978

docs/source/releases/changes-in-this-fork.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Changes in this Fork
2525
| Scheme layer used: 196 |
2626
+------------------------+
2727

28+
- Added the :meth:`~pyrogram.Client.on_bot_purchased_paid_media` and :meth:`~pyrogram.Client.on_bot_business_connection`.
2829
- Added the parameters ``can_be_upgraded``, ``was_refunded``, ``prepaid_upgrade_star_count`` in :obj:`~pyrogram.types.UserGift`.
2930
- Renamed the parameter ``only_in_channels`` to ``chat_type_filter`` in the :meth:`~pyrogram.Client.search_global_count` and :meth:`~pyrogram.Client.search_global`.
3031
- Removed ``sender_user_id`` parameter from :meth:`~pyrogram.Client.sell_gift` and :meth:`~pyrogram.Client.toggle_gift_is_saved`.

pyrogram/dispatcher.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
from pyrogram import errors, utils, raw
2626
from pyrogram.handlers import (
2727
MessageHandler, EditedMessageHandler,
28-
29-
28+
BusinessBotConnectionHandler,
3029
MessageReactionUpdatedHandler,
3130
MessageReactionCountUpdatedHandler,
3231
InlineQueryHandler,
3332
ChosenInlineResultHandler,
3433
CallbackQueryHandler,
3534
ShippingQueryHandler,
3635
PreCheckoutQueryHandler,
36+
PurchasedPaidMediaHandler,
3737
PollHandler,
3838

3939

@@ -64,6 +64,8 @@
6464
UpdateBotShippingQuery,
6565
UpdateStory,
6666
UpdateBusinessBotCallbackQuery,
67+
UpdateBotBusinessConnect,
68+
UpdateBotPurchasedPaidMedia,
6769
)
6870

6971
log = logging.getLogger(__name__)
@@ -86,6 +88,8 @@ class Dispatcher:
8688
PRE_CHECKOUT_QUERY_UPDATES = (UpdateBotPrecheckoutQuery,)
8789
SHIPPING_QUERY_UPDATES = (UpdateBotShippingQuery,)
8890
NEW_STORY_UPDATES = (UpdateStory,)
91+
BOT_BUSINESS_CONNECT_UPDATES = (UpdateBotBusinessConnect,)
92+
PURCHASED_PAID_MEDIA_UPDATES = (UpdateBotPurchasedPaidMedia,)
8993

9094
def __init__(self, client: "pyrogram.Client"):
9195
self.client = client
@@ -218,6 +222,18 @@ async def story_parser(update, users, chats):
218222
StoryHandler
219223
)
220224

225+
async def bot_business_connect_parser(update, users, chats):
226+
return (
227+
await pyrogram.types.BusinessConnection._parse(self.client, update, users, chats),
228+
BusinessBotConnectionHandler
229+
)
230+
231+
async def purchased_paid_media_parser(update, users, chats):
232+
return (
233+
pyrogram.types.PaidMediaPurchased._parse(self.client, update, users),
234+
PurchasedPaidMediaHandler
235+
)
236+
221237
self.update_parsers = {
222238
Dispatcher.NEW_MESSAGE_UPDATES: message_parser,
223239
Dispatcher.EDIT_MESSAGE_UPDATES: edited_message_parser,
@@ -235,6 +251,8 @@ async def story_parser(update, users, chats):
235251
Dispatcher.SHIPPING_QUERY_UPDATES: shipping_query_parser,
236252
Dispatcher.PRE_CHECKOUT_QUERY_UPDATES: pre_checkout_query_parser,
237253
Dispatcher.NEW_STORY_UPDATES: story_parser,
254+
Dispatcher.BOT_BUSINESS_CONNECT_UPDATES: bot_business_connect_parser,
255+
Dispatcher.PURCHASED_PAID_MEDIA_UPDATES: purchased_paid_media_parser,
238256
}
239257

240258
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from .business_bot_connection_handler import BusinessBotConnectionHandler
1920
from .callback_query_handler import CallbackQueryHandler
2021
from .chat_join_request_handler import ChatJoinRequestHandler
2122
from .chat_member_updated_handler import ChatMemberUpdatedHandler
@@ -31,5 +32,6 @@
3132
from .message_reaction_updated_handler import MessageReactionUpdatedHandler
3233
from .message_reaction_count_updated_handler import MessageReactionCountUpdatedHandler
3334
from .pre_checkout_query_handler import PreCheckoutQueryHandler
35+
from .purchased_paid_media_handler import PurchasedPaidMediaHandler
3436
from .shipping_query_handler import ShippingQueryHandler
3537
from .story_handler import StoryHandler
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 BusinessBotConnectionHandler(Handler):
25+
"""The Bot Business Connection handler class. Used to handle new bot business connection.
26+
It is intended to be used with :meth:`~pyrogram.Client.add_handler`
27+
28+
For a nicer way to register this handler, have a look at the
29+
:meth:`~pyrogram.Client.on_bot_business_connection` decorator.
30+
31+
Parameters:
32+
callback (``Callable``):
33+
Pass a function that will be called when a new BusinessConnection arrives. It takes *(client, business_connection)*
34+
as positional arguments (look at the section below for a detailed description).
35+
36+
filters (:obj:`Filters`):
37+
Pass one or more filters to allow only a subset of callback queries to be passed
38+
in your callback function.
39+
40+
Other parameters:
41+
client (:obj:`~pyrogram.Client`):
42+
The Client itself, useful when you want to call other API methods inside the message handler.
43+
44+
business_connection (:obj:`~pyrogram.types.BusinessConnection`):
45+
The bot was connected or disconnected from a business account, or a user edited
46+
an existing connection with the bot.
47+
48+
"""
49+
50+
def __init__(self, callback: Callable, filters=None):
51+
super().__init__(callback, filters)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 PurchasedPaidMediaHandler(Handler):
25+
"""The Bot Business Connection handler class. Used to handle new bot business connection.
26+
It is intended to be used with :meth:`~pyrogram.Client.add_handler`
27+
28+
For a nicer way to register this handler, have a look at the
29+
:meth:`~pyrogram.Client.on_bot_purchased_paid_media` decorator.
30+
31+
Parameters:
32+
callback (``Callable``):
33+
Pass a function that will be called when a new PaidMediaPurchased arrives. It takes *(client, purchased_paid_media)*
34+
as positional arguments (look at the section below for a detailed description).
35+
36+
filters (:obj:`Filters`):
37+
Pass one or more filters to allow only a subset of callback queries to be passed
38+
in your callback function.
39+
40+
Other parameters:
41+
client (:obj:`~pyrogram.Client`):
42+
The Client itself, useful when you want to call other API methods inside the message handler.
43+
44+
purchased_paid_media (:obj:`~pyrogram.types.PaidMediaPurchased`):
45+
A user purchased paid media with a non-empty payload sent by the bot in a non-channel chat.
46+
47+
"""
48+
49+
def __init__(self, callback: Callable, filters=None):
50+
super().__init__(callback, filters)

pyrogram/methods/decorators/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from .on_bot_business_connection import OnBotBusinessConnection
20+
from .on_bot_purchased_paid_media import OnBotPurchasedPaidMedia
1921
from .on_callback_query import OnCallbackQuery
2022
from .on_chat_join_request import OnChatJoinRequest
2123
from .on_chat_member_updated import OnChatMemberUpdated
@@ -39,13 +41,15 @@ class Decorators(
3941
OnMessage,
4042
OnEditedMessage,
4143
OnDeletedMessages,
44+
OnBotBusinessConnection,
4245
OnMessageReactionUpdated,
4346
OnMessageReactionCountUpdated,
4447
OnInlineQuery,
4548
OnChosenInlineResult,
4649
OnCallbackQuery,
4750
OnShippingQuery,
4851
OnPreCheckoutQuery,
52+
OnBotPurchasedPaidMedia,
4953
OnPoll,
5054

5155
OnChatMemberUpdated,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 OnBotBusinessConnection:
26+
def on_bot_business_connection(
27+
self=None,
28+
filters=None,
29+
group: int = 0
30+
) -> Callable:
31+
"""Decorator for handling bot business connection.
32+
33+
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the
34+
:obj:`~pyrogram.handlers.BusinessBotConnectionHandler`.
35+
36+
.. include:: /_includes/usable-by/bots.rst
37+
38+
Parameters:
39+
filters (:obj:`~pyrogram.filters`, *optional*):
40+
Pass one or more filters to allow only a subset of callback queries to be passed
41+
in your function.
42+
43+
group (``int``, *optional*):
44+
The group identifier, defaults to 0.
45+
46+
"""
47+
48+
def decorator(func: Callable) -> Callable:
49+
if isinstance(self, pyrogram.Client):
50+
self.add_handler(pyrogram.handlers.BusinessBotConnectionHandler(func, filters), group)
51+
elif isinstance(self, Filter) or self is None:
52+
if not hasattr(func, "handlers"):
53+
func.handlers = []
54+
55+
func.handlers.append(
56+
(
57+
pyrogram.handlers.BusinessBotConnectionHandler(func, self),
58+
group if filters is None else filters
59+
)
60+
)
61+
62+
return func
63+
64+
return decorator

0 commit comments

Comments
 (0)