Skip to content

Commit fa157b5

Browse files
committed
Add support for ChosenInlineResult objects
1 parent a54cd2e commit fa157b5

File tree

10 files changed

+242
-6
lines changed

10 files changed

+242
-6
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ def get_title_list(s: str) -> list:
366366
InlineQueryResultArticle
367367
InlineQueryResultPhoto
368368
InlineQueryResultAnimation
369+
ChosenInlineResult
369370
""",
370371
input_message_content="""
371372
InputMessageContent

docs/source/api/decorators.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Index
4141
- :meth:`~Client.on_message`
4242
- :meth:`~Client.on_callback_query`
4343
- :meth:`~Client.on_inline_query`
44+
- :meth:`~Client.on_chosen_inline_result`
4445
- :meth:`~Client.on_deleted_messages`
4546
- :meth:`~Client.on_user_status`
4647
- :meth:`~Client.on_poll`
@@ -56,6 +57,7 @@ Details
5657
.. autodecorator:: pyrogram.Client.on_message()
5758
.. autodecorator:: pyrogram.Client.on_callback_query()
5859
.. autodecorator:: pyrogram.Client.on_inline_query()
60+
.. autodecorator:: pyrogram.Client.on_chosen_inline_result()
5961
.. autodecorator:: pyrogram.Client.on_deleted_messages()
6062
.. autodecorator:: pyrogram.Client.on_user_status()
6163
.. autodecorator:: pyrogram.Client.on_poll()

docs/source/api/handlers.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Index
3838
- :class:`DeletedMessagesHandler`
3939
- :class:`CallbackQueryHandler`
4040
- :class:`InlineQueryHandler`
41+
- :class:`ChosenInlineResultHandler`
4142
- :class:`UserStatusHandler`
4243
- :class:`PollHandler`
4344
- :class:`DisconnectHandler`
@@ -53,6 +54,7 @@ Details
5354
.. autoclass:: DeletedMessagesHandler()
5455
.. autoclass:: CallbackQueryHandler()
5556
.. autoclass:: InlineQueryHandler()
57+
.. autoclass:: ChosenInlineResultHandler()
5658
.. autoclass:: UserStatusHandler()
5759
.. autoclass:: PollHandler()
5860
.. autoclass:: DisconnectHandler()

pyrogram/client/ext/dispatcher.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
UpdateEditMessage, UpdateEditChannelMessage,
2929
UpdateDeleteMessages, UpdateDeleteChannelMessages,
3030
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
31-
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll
31+
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll,
32+
UpdateBotInlineSend
3233
)
3334
from . import utils
3435
from ..handlers import (
3536
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
36-
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler
37+
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler,
38+
ChosenInlineResultHandler
3739
)
3840

3941
log = logging.getLogger(__name__)
@@ -99,7 +101,11 @@ def __init__(self, client, workers: int):
99101
lambda upd, usr, cht: (pyrogram.InlineQuery._parse(self.client, upd, usr), InlineQueryHandler),
100102

101103
(UpdateMessagePoll,):
102-
lambda upd, usr, cht: (pyrogram.Poll._parse_update(self.client, upd), PollHandler)
104+
lambda upd, usr, cht: (pyrogram.Poll._parse_update(self.client, upd), PollHandler),
105+
106+
(UpdateBotInlineSend,):
107+
lambda upd, usr, cht: (pyrogram.ChosenInlineResult._parse(self.client, upd, usr),
108+
ChosenInlineResultHandler)
103109
}
104110

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

pyrogram/client/handlers/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
from .poll_handler import PollHandler
2525
from .raw_update_handler import RawUpdateHandler
2626
from .user_status_handler import UserStatusHandler
27+
from .chosen_inline_result_handler import ChosenInlineResultHandler
2728

2829
__all__ = [
2930
"MessageHandler", "DeletedMessagesHandler", "CallbackQueryHandler", "RawUpdateHandler", "DisconnectHandler",
30-
"UserStatusHandler", "InlineQueryHandler", "PollHandler"
31+
"UserStatusHandler", "InlineQueryHandler", "PollHandler", "ChosenInlineResultHandler"
3132
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2020 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 .handler import Handler
20+
21+
22+
class ChosenInlineResultHandler(Handler):
23+
"""The ChosenInlineResultHandler handler class. Used to handle chosen inline results coming from inline queries.
24+
It is intended to be used with :meth:`~Client.add_handler`
25+
26+
For a nicer way to register this handler, have a look at the
27+
:meth:`~Client.on_chosen_inline_query` decorator.
28+
29+
Parameters:
30+
callback (``callable``):
31+
Pass a function that will be called when a new chosen inline result arrives.
32+
It takes *(client, chosen_inline_result)* as positional arguments (look at the section below for a
33+
detailed description).
34+
35+
filters (:obj:`Filters`):
36+
Pass one or more filters to allow only a subset of chosen inline results to be passed
37+
in your callback function.
38+
39+
Other parameters:
40+
client (:obj:`Client`):
41+
The Client itself, useful when you want to call other API methods inside the message handler.
42+
43+
chosen_inline_result (:obj:`ChosenInlineResult`):
44+
The received chosen inline result.
45+
"""
46+
47+
def __init__(self, callback: callable, filters=None):
48+
super().__init__(callback, filters)

pyrogram/client/methods/decorators/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .on_poll import OnPoll
2525
from .on_raw_update import OnRawUpdate
2626
from .on_user_status import OnUserStatus
27+
from .on_chosen_inline_result import OnChosenInlineResult
2728

2829

2930
class Decorators(
@@ -34,6 +35,7 @@ class Decorators(
3435
OnDisconnect,
3536
OnUserStatus,
3637
OnInlineQuery,
37-
OnPoll
38+
OnPoll,
39+
OnChosenInlineResult
3840
):
3941
pass
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2020 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.client.filters.filter import Filter
23+
from ...ext import BaseClient
24+
25+
26+
class OnChosenInlineResult(BaseClient):
27+
def on_chosen_inline_result(
28+
self=None,
29+
filters=None,
30+
group: int = 0
31+
) -> callable:
32+
"""Decorator for handling chosen inline results.
33+
34+
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the :obj:`~pyrogram.ChosenInlineResult`.
35+
36+
Parameters:
37+
filters (:obj:`~pyrogram.Filters`, *optional*):
38+
Pass one or more filters to allow only a subset of chosen inline results to be passed
39+
in your function.
40+
41+
group (``int``, *optional*):
42+
The group identifier, defaults to 0.
43+
"""
44+
45+
def decorator(func: Callable) -> Callable:
46+
if isinstance(self, pyrogram.Client):
47+
self.add_handler(pyrogram.ChosenInlineResultHandler(func, filters), group)
48+
elif isinstance(self, Filter) or self is None:
49+
func.handler = (
50+
pyrogram.ChosenInlineResultHandler(func, self),
51+
group if filters is None else filters
52+
)
53+
54+
return func
55+
56+
return decorator

pyrogram/client/types/inline_mode/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
from .inline_query_result_animation import InlineQueryResultAnimation
2222
from .inline_query_result_article import InlineQueryResultArticle
2323
from .inline_query_result_photo import InlineQueryResultPhoto
24+
from .chosen_inline_result import ChosenInlineResult
2425

2526
__all__ = [
2627
"InlineQuery", "InlineQueryResult", "InlineQueryResultArticle", "InlineQueryResultPhoto",
27-
"InlineQueryResultAnimation"
28+
"InlineQueryResultAnimation", "ChosenInlineResult"
2829
]
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2020 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+
# This file is part of Pyrogram.
20+
#
21+
# Pyrogram is free software: you can redistribute it and/or modify
22+
# it under the terms of the GNU Lesser General Public License as published
23+
# by the Free Software Foundation, either version 3 of the License, or
24+
# (at your option) any later version.
25+
#
26+
# Pyrogram is distributed in the hope that it will be useful,
27+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
28+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29+
# GNU Lesser General Public License for more details.
30+
#
31+
# You should have received a copy of the GNU Lesser General Public License
32+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
33+
34+
from base64 import b64encode
35+
from struct import pack
36+
from typing import Union
37+
38+
import pyrogram
39+
from pyrogram.api import types
40+
from pyrogram.client.types.object import Object
41+
from pyrogram.client.types.update import Update
42+
from pyrogram.client.types.user_and_chats import User
43+
from pyrogram.client.types.messages_and_media import Location
44+
from pyrogram.client.ext import utils
45+
46+
47+
class ChosenInlineResult(Object, Update):
48+
"""A :doc:`result <InlineQueryResult>` of an inline query chosen by the user and sent to their chat partner.
49+
50+
Parameters:
51+
result_id (``str``):
52+
The unique identifier for the result that was chosen.
53+
54+
from_user (:obj:`User`):
55+
The user that chose the result.
56+
57+
query (``str``):
58+
The query that was used to obtain the result.
59+
60+
location (:obj:`Location`, *optional*):
61+
Sender location, only for bots that require user location.
62+
63+
inline_message_id (``str``, *optional*):
64+
Identifier of the sent inline message.
65+
Available only if there is an :doc:`inline keyboard <InlineKeyboardMarkup>` attached to the message.
66+
Will be also received in :doc:`callback queries <CallbackQuery>` and can be used to edit the message.
67+
68+
.. note::
69+
70+
It is necessary to enable inline feedback via `@Botfather <https://t.me/botfather>`_ in order to receive these
71+
objects in updates.
72+
"""
73+
74+
def __init__(
75+
self,
76+
*,
77+
client: "pyrogram.BaseClient" = None,
78+
result_id: str,
79+
from_user: User,
80+
query: str,
81+
location: "pyrogram.Location" = None,
82+
inline_message_id: str = None,
83+
):
84+
super().__init__(client)
85+
86+
self.result_id = result_id
87+
self.from_user = from_user
88+
self.query = query
89+
self.location = location
90+
self.inline_message_id = inline_message_id
91+
92+
@staticmethod
93+
def _parse(client, chosen_inline_result: types.UpdateBotInlineSend, users) -> "ChosenInlineResult":
94+
inline_message_id = None
95+
96+
if isinstance(chosen_inline_result.msg_id, types.InputBotInlineMessageID):
97+
inline_message_id = b64encode(
98+
pack(
99+
"<iqq",
100+
chosen_inline_result.msg_id.dc_id,
101+
chosen_inline_result.msg_id.id,
102+
chosen_inline_result.msg_id.access_hash
103+
),
104+
b"-_"
105+
).decode().rstrip("=")
106+
107+
return ChosenInlineResult(
108+
result_id=str(chosen_inline_result.id),
109+
from_user=User._parse(client, users[chosen_inline_result.user_id]),
110+
query=chosen_inline_result.query,
111+
location=Location(
112+
longitude=chosen_inline_result.geo.long,
113+
latitude=chosen_inline_result.geo.lat,
114+
client=client
115+
) if chosen_inline_result.geo else None,
116+
inline_message_id=inline_message_id
117+
)

0 commit comments

Comments
 (0)