Skip to content

Commit 92283d6

Browse files
committed
Add timeout to Message.click
1 parent 6530c7e commit 92283d6

2 files changed

Lines changed: 58 additions & 45 deletions

File tree

pyrogram/client/methods/bots/request_callback_answer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def request_callback_answer(
2727
self,
2828
chat_id: Union[int, str],
2929
message_id: int,
30-
callback_data: bytes
30+
callback_data: bytes,
31+
timeout: int = 10
3132
):
3233
"""Use this method to request a callback answer from bots.
3334
This is the equivalent of clicking an inline button containing callback data.
@@ -44,6 +45,9 @@ def request_callback_answer(
4445
callback_data (``bytes``):
4546
Callback data associated with the inline button you want to get the answer from.
4647
48+
timeout (``int``, *optional*):
49+
Timeout in seconds.
50+
4751
Returns:
4852
The answer containing info useful for clients to display a notification at the top of the chat screen
4953
or as an alert.
@@ -59,5 +63,5 @@ def request_callback_answer(
5963
data=callback_data
6064
),
6165
retries=0,
62-
timeout=10
66+
timeout=timeout
6367
)

pyrogram/client/types/messages_and_media/message.py

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
import pyrogram
2323
from pyrogram.api import types
24-
from pyrogram.errors import MessageIdsEmpty
2524
from pyrogram.client.types.input_media import InputMedia
25+
from pyrogram.errors import MessageIdsEmpty
2626
from .contact import Contact
2727
from .location import Location
2828
from .message_entity import MessageEntity
@@ -2725,10 +2725,10 @@ def delete(self, revoke: bool = True):
27252725
revoke=revoke
27262726
)
27272727

2728-
def click(self, x: int or str, y: int = None, quote: bool = None):
2728+
def click(self, x: int or str, y: int = None, quote: bool = None, timeout: int = 10):
27292729
"""Bound method *click* of :obj:`Message`.
27302730
2731-
Use as a shortcut for clicking a button attached to the message instead of.
2731+
Use as a shortcut for clicking a button attached to the message instead of:
27322732
27332733
- Clicking inline buttons:
27342734
@@ -2773,58 +2773,67 @@ def click(self, x: int or str, y: int = None, quote: bool = None):
27732773
If ``True``, the message will be sent as a reply to this message.
27742774
Defaults to ``True`` in group chats and ``False`` in private chats.
27752775
2776+
timeout (``int``, *optional*):
2777+
Timeout in seconds.
2778+
27762779
Returns:
2777-
- The result of *request_callback_answer()* in case of inline callback button clicks.
2778-
- The result of *reply()* in case of normal button clicks.
2779-
- A string in case the inline button is an URL, switch_inline_query or switch_inline_query_current_chat
2780-
button.
2780+
- The result of :meth:`request_callback_answer() <pyrogram.Client.request_callback_answer>` in case of
2781+
inline callback button clicks.
2782+
- The result of :meth:`reply() <pyrogram.Message.reply>` in case of normal button clicks.
2783+
- A string in case the inline button is a URL, a *switch_inline_query* or a
2784+
*switch_inline_query_current_chat* button.
27812785
27822786
Raises:
27832787
RPCError: In case of a Telegram RPC error.
2784-
``ValueError``: If the provided index or position is out of range or the button label was not found
2785-
``TimeoutError``: If, after clicking an inline button, the bot fails to answer within 10 seconds
2788+
ValueError: In case the provided index or position is out of range or the button label was not found.
2789+
TimeoutError: In case, after clicking an inline button, the bot fails to answer within the timeout.
27862790
"""
2791+
27872792
if isinstance(self.reply_markup, pyrogram.ReplyKeyboardMarkup):
2788-
return self.reply(x, quote=quote)
2793+
keyboard = self.reply_markup.keyboard
2794+
is_inline = False
27892795
elif isinstance(self.reply_markup, pyrogram.InlineKeyboardMarkup):
2790-
if isinstance(x, int) and y is None:
2791-
try:
2792-
button = [
2793-
button
2794-
for row in self.reply_markup.inline_keyboard
2795-
for button in row
2796-
][x]
2797-
except IndexError:
2798-
raise ValueError("The button at index {} doesn't exist".format(x)) from None
2799-
elif isinstance(x, int) and isinstance(y, int):
2800-
try:
2801-
button = self.reply_markup.inline_keyboard[y][x]
2802-
except IndexError:
2803-
raise ValueError("The button at position ({}, {}) doesn't exist".format(x, y)) from None
2804-
elif isinstance(x, str):
2805-
x = x.encode("utf-16", "surrogatepass").decode("utf-16")
2796+
keyboard = self.reply_markup.inline_keyboard
2797+
is_inline = True
2798+
else:
2799+
raise ValueError("The message doesn't contain any keyboard")
28062800

2807-
try:
2808-
button = [
2809-
button
2810-
for row in self.reply_markup.inline_keyboard
2811-
for button in row
2812-
if x == button.text
2813-
][0]
2814-
except IndexError:
2815-
raise ValueError(
2816-
"The button with label '{}' doesn't exists".format(
2817-
x.encode("unicode_escape").decode()
2818-
)
2819-
) from None
2820-
else:
2821-
raise ValueError("Invalid arguments")
2801+
if isinstance(x, int) and y is None:
2802+
try:
2803+
button = [
2804+
button
2805+
for row in keyboard
2806+
for button in row
2807+
][x]
2808+
except IndexError:
2809+
raise ValueError("The button at index {} doesn't exist".format(x))
2810+
elif isinstance(x, int) and isinstance(y, int):
2811+
try:
2812+
button = keyboard[y][x]
2813+
except IndexError:
2814+
raise ValueError("The button at position ({}, {}) doesn't exist".format(x, y))
2815+
elif isinstance(x, str) and y is None:
2816+
label = x.encode("utf-16", "surrogatepass").decode("utf-16")
2817+
2818+
try:
2819+
button = [
2820+
button
2821+
for row in keyboard
2822+
for button in row
2823+
if label == button.text
2824+
][0]
2825+
except IndexError:
2826+
raise ValueError("The button with label '{}' doesn't exists".format(x))
2827+
else:
2828+
raise ValueError("Invalid arguments")
28222829

2830+
if is_inline:
28232831
if button.callback_data:
28242832
return self._client.request_callback_answer(
28252833
chat_id=self.chat.id,
28262834
message_id=self.message_id,
2827-
callback_data=button.callback_data
2835+
callback_data=button.callback_data,
2836+
timeout=timeout
28282837
)
28292838
elif button.url:
28302839
return button.url
@@ -2835,7 +2844,7 @@ def click(self, x: int or str, y: int = None, quote: bool = None):
28352844
else:
28362845
raise ValueError("This button is not supported yet")
28372846
else:
2838-
raise ValueError("The message doesn't contain any keyboard")
2847+
self.reply(button, quote=quote)
28392848

28402849
def download(
28412850
self,

0 commit comments

Comments
 (0)