2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Add timeout to Message.click

This commit is contained in:
Dan 2019-05-09 05:55:44 +02:00
parent 6530c7e293
commit 92283d6cab
2 changed files with 58 additions and 45 deletions

View File

@ -27,7 +27,8 @@ class RequestCallbackAnswer(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
message_id: int, message_id: int,
callback_data: bytes callback_data: bytes,
timeout: int = 10
): ):
"""Use this method to request a callback answer from bots. """Use this method to request a callback answer from bots.
This is the equivalent of clicking an inline button containing callback data. This is the equivalent of clicking an inline button containing callback data.
@ -44,6 +45,9 @@ class RequestCallbackAnswer(BaseClient):
callback_data (``bytes``): callback_data (``bytes``):
Callback data associated with the inline button you want to get the answer from. Callback data associated with the inline button you want to get the answer from.
timeout (``int``, *optional*):
Timeout in seconds.
Returns: Returns:
The answer containing info useful for clients to display a notification at the top of the chat screen The answer containing info useful for clients to display a notification at the top of the chat screen
or as an alert. or as an alert.
@ -59,5 +63,5 @@ class RequestCallbackAnswer(BaseClient):
data=callback_data data=callback_data
), ),
retries=0, retries=0,
timeout=10 timeout=timeout
) )

View File

@ -21,8 +21,8 @@ from typing import List, Match, Union
import pyrogram import pyrogram
from pyrogram.api import types from pyrogram.api import types
from pyrogram.errors import MessageIdsEmpty
from pyrogram.client.types.input_media import InputMedia from pyrogram.client.types.input_media import InputMedia
from pyrogram.errors import MessageIdsEmpty
from .contact import Contact from .contact import Contact
from .location import Location from .location import Location
from .message_entity import MessageEntity from .message_entity import MessageEntity
@ -2725,10 +2725,10 @@ class Message(PyrogramType, Update):
revoke=revoke revoke=revoke
) )
def click(self, x: int or str, y: int = None, quote: bool = None): def click(self, x: int or str, y: int = None, quote: bool = None, timeout: int = 10):
"""Bound method *click* of :obj:`Message`. """Bound method *click* of :obj:`Message`.
Use as a shortcut for clicking a button attached to the message instead of. Use as a shortcut for clicking a button attached to the message instead of:
- Clicking inline buttons: - Clicking inline buttons:
@ -2773,58 +2773,67 @@ class Message(PyrogramType, Update):
If ``True``, the message will be sent as a reply to this message. If ``True``, the message will be sent as a reply to this message.
Defaults to ``True`` in group chats and ``False`` in private chats. Defaults to ``True`` in group chats and ``False`` in private chats.
timeout (``int``, *optional*):
Timeout in seconds.
Returns: Returns:
- The result of *request_callback_answer()* in case of inline callback button clicks. - The result of :meth:`request_callback_answer() <pyrogram.Client.request_callback_answer>` in case of
- The result of *reply()* in case of normal button clicks. inline callback button clicks.
- A string in case the inline button is an URL, switch_inline_query or switch_inline_query_current_chat - The result of :meth:`reply() <pyrogram.Message.reply>` in case of normal button clicks.
button. - A string in case the inline button is a URL, a *switch_inline_query* or a
*switch_inline_query_current_chat* button.
Raises: Raises:
RPCError: In case of a Telegram RPC error. RPCError: In case of a Telegram RPC error.
``ValueError``: If the provided index or position is out of range or the button label was not found ValueError: In case the provided index or position is out of range or the button label was not found.
``TimeoutError``: If, after clicking an inline button, the bot fails to answer within 10 seconds TimeoutError: In case, after clicking an inline button, the bot fails to answer within the timeout.
""" """
if isinstance(self.reply_markup, pyrogram.ReplyKeyboardMarkup): if isinstance(self.reply_markup, pyrogram.ReplyKeyboardMarkup):
return self.reply(x, quote=quote) keyboard = self.reply_markup.keyboard
is_inline = False
elif isinstance(self.reply_markup, pyrogram.InlineKeyboardMarkup): elif isinstance(self.reply_markup, pyrogram.InlineKeyboardMarkup):
keyboard = self.reply_markup.inline_keyboard
is_inline = True
else:
raise ValueError("The message doesn't contain any keyboard")
if isinstance(x, int) and y is None: if isinstance(x, int) and y is None:
try: try:
button = [ button = [
button button
for row in self.reply_markup.inline_keyboard for row in keyboard
for button in row for button in row
][x] ][x]
except IndexError: except IndexError:
raise ValueError("The button at index {} doesn't exist".format(x)) from None raise ValueError("The button at index {} doesn't exist".format(x))
elif isinstance(x, int) and isinstance(y, int): elif isinstance(x, int) and isinstance(y, int):
try: try:
button = self.reply_markup.inline_keyboard[y][x] button = keyboard[y][x]
except IndexError: except IndexError:
raise ValueError("The button at position ({}, {}) doesn't exist".format(x, y)) from None raise ValueError("The button at position ({}, {}) doesn't exist".format(x, y))
elif isinstance(x, str): elif isinstance(x, str) and y is None:
x = x.encode("utf-16", "surrogatepass").decode("utf-16") label = x.encode("utf-16", "surrogatepass").decode("utf-16")
try: try:
button = [ button = [
button button
for row in self.reply_markup.inline_keyboard for row in keyboard
for button in row for button in row
if x == button.text if label == button.text
][0] ][0]
except IndexError: except IndexError:
raise ValueError( raise ValueError("The button with label '{}' doesn't exists".format(x))
"The button with label '{}' doesn't exists".format(
x.encode("unicode_escape").decode()
)
) from None
else: else:
raise ValueError("Invalid arguments") raise ValueError("Invalid arguments")
if is_inline:
if button.callback_data: if button.callback_data:
return self._client.request_callback_answer( return self._client.request_callback_answer(
chat_id=self.chat.id, chat_id=self.chat.id,
message_id=self.message_id, message_id=self.message_id,
callback_data=button.callback_data callback_data=button.callback_data,
timeout=timeout
) )
elif button.url: elif button.url:
return button.url return button.url
@ -2835,7 +2844,7 @@ class Message(PyrogramType, Update):
else: else:
raise ValueError("This button is not supported yet") raise ValueError("This button is not supported yet")
else: else:
raise ValueError("The message doesn't contain any keyboard") self.reply(button, quote=quote)
def download( def download(
self, self,