mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 17:36:34 +03:00
Properly handle bot timeouts when clicking buttons
This commit is contained in:
parent
f86f52d960
commit
aa6d3430ae
|
@ -66,23 +66,5 @@ def rpc_message_to_error(rpc_error, report_method=None):
|
||||||
capture = int(m.group(1)) if m.groups() else None
|
capture = int(m.group(1)) if m.groups() else None
|
||||||
return cls(capture=capture)
|
return cls(capture=capture)
|
||||||
|
|
||||||
if rpc_error.error_code == 400:
|
cls = base_errors.get(rpc_error.error_code, RPCError)
|
||||||
return BadRequestError(rpc_error.error_message)
|
return cls(rpc_error.error_message)
|
||||||
|
|
||||||
if rpc_error.error_code == 401:
|
|
||||||
return UnauthorizedError(rpc_error.error_message)
|
|
||||||
|
|
||||||
if rpc_error.error_code == 403:
|
|
||||||
return ForbiddenError(rpc_error.error_message)
|
|
||||||
|
|
||||||
if rpc_error.error_code == 404:
|
|
||||||
return NotFoundError(rpc_error.error_message)
|
|
||||||
|
|
||||||
if rpc_error.error_code == 406:
|
|
||||||
return AuthKeyError(rpc_error.error_message)
|
|
||||||
|
|
||||||
if rpc_error.error_code == 500:
|
|
||||||
return ServerError(rpc_error.error_message)
|
|
||||||
|
|
||||||
return RPCError('{} (code {})'.format(
|
|
||||||
rpc_error.error_message, rpc_error.error_code))
|
|
||||||
|
|
|
@ -97,6 +97,19 @@ class ServerError(RPCError):
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
|
class BotTimeout(RPCError):
|
||||||
|
"""
|
||||||
|
Clicking the inline buttons of bots that never (or take to long to)
|
||||||
|
call ``answerCallbackQuery`` will result in this "special" RPCError.
|
||||||
|
"""
|
||||||
|
code = -503
|
||||||
|
message = 'Timeout'
|
||||||
|
|
||||||
|
def __init__(self, message):
|
||||||
|
super().__init__(message)
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
class BadMessageError(Exception):
|
class BadMessageError(Exception):
|
||||||
"""Occurs when handling a bad_message_notification."""
|
"""Occurs when handling a bad_message_notification."""
|
||||||
ErrorMessages = {
|
ErrorMessages = {
|
||||||
|
@ -142,3 +155,9 @@ class BadMessageError(Exception):
|
||||||
'Unknown error code (this should not happen): {}.'.format(code)))
|
'Unknown error code (this should not happen): {}.'.format(code)))
|
||||||
|
|
||||||
self.code = code
|
self.code = code
|
||||||
|
|
||||||
|
|
||||||
|
base_errors = {x.code: x for x in (
|
||||||
|
InvalidDCError, BadRequestError, UnauthorizedError, ForbiddenError,
|
||||||
|
NotFoundError, AuthKeyError, FloodError, ServerError, BotTimeout
|
||||||
|
)}
|
||||||
|
|
|
@ -556,7 +556,7 @@ class Message:
|
||||||
if sum(int(x is not None) for x in (i, text, filter)) >= 2:
|
if sum(int(x is not None) for x in (i, text, filter)) >= 2:
|
||||||
raise ValueError('You can only set either of i, text or filter')
|
raise ValueError('You can only set either of i, text or filter')
|
||||||
|
|
||||||
if not self.buttons:
|
if not await self.buttons:
|
||||||
return # Accessing the property sets self._buttons[_flat]
|
return # Accessing the property sets self._buttons[_flat]
|
||||||
|
|
||||||
if text is not None:
|
if text is not None:
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from .. import types, functions
|
from .. import types, functions
|
||||||
|
from ...errors import BotTimeout
|
||||||
import webbrowser
|
import webbrowser
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,11 +66,13 @@ class MessageButton:
|
||||||
req = functions.messages.GetBotCallbackAnswerRequest(
|
req = functions.messages.GetBotCallbackAnswerRequest(
|
||||||
peer=self._chat, msg_id=self._msg_id, data=self.button.data
|
peer=self._chat, msg_id=self._msg_id, data=self.button.data
|
||||||
)
|
)
|
||||||
return await self._client(req, retries=1)
|
try:
|
||||||
|
return await self._client(req)
|
||||||
|
except BotTimeout:
|
||||||
|
return None
|
||||||
elif isinstance(self.button, types.KeyboardButtonSwitchInline):
|
elif isinstance(self.button, types.KeyboardButtonSwitchInline):
|
||||||
req = functions.messages.StartBotRequest(
|
return await self._client(functions.messages.StartBotRequest(
|
||||||
bot=self._from, peer=self._chat, start_param=self.button.query
|
bot=self._from, peer=self._chat, start_param=self.button.query
|
||||||
)
|
))
|
||||||
return await self._client(req, retries=1)
|
|
||||||
elif isinstance(self.button, types.KeyboardButtonUrl):
|
elif isinstance(self.button, types.KeyboardButtonUrl):
|
||||||
return webbrowser.open(self.button.url)
|
return webbrowser.open(self.button.url)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user