Add parameter network_delay for slow connections and increase default to 2 seconds

This commit is contained in:
Jannes Höke 2015-11-10 19:47:21 +01:00
parent 9191d333ca
commit 4bc03ed56a
2 changed files with 16 additions and 5 deletions

View file

@ -649,7 +649,8 @@ class Bot(TelegramObject):
def getUpdates(self, def getUpdates(self,
offset=None, offset=None,
limit=100, limit=100,
timeout=0): timeout=0,
network_delay=2):
"""Use this method to receive incoming updates using long polling. """Use this method to receive incoming updates using long polling.
Args: Args:
@ -665,6 +666,11 @@ class Bot(TelegramObject):
timeout: timeout:
Timeout in seconds for long polling. Defaults to 0, i.e. usual Timeout in seconds for long polling. Defaults to 0, i.e. usual
short polling. short polling.
network_delay:
Additional timeout in seconds to allow the response from Telegram to
take some time when using long polling. Defaults to 2, which should
be enough for most connections. Increase it if it takes very long
for data to be transmitted from and to the Telegram servers.
Returns: Returns:
A list of telegram.Update objects are returned. A list of telegram.Update objects are returned.
@ -679,6 +685,8 @@ class Bot(TelegramObject):
data['limit'] = limit data['limit'] = limit
if timeout: if timeout:
data['timeout'] = timeout data['timeout'] = timeout
if network_delay:
data['network_delay'] = network_delay
result = request.post(url, data) result = request.post(url, data)

View file

@ -79,10 +79,13 @@ def post(url,
A JSON object. A JSON object.
""" """
# Add one second to the timeout of urlopen to allow data to be transferred # Add time to the timeout of urlopen to allow data to be transferred over
# over the network. # the network.
if 'timeout' in data: if 'timeout' in data:
timeout = data['timeout'] + 1. if 'network_delay' in data:
timeout = data['timeout'] + data['network_delay']
else:
timeout = data['timeout'] + 2.
else: else:
timeout = None timeout = None
@ -108,7 +111,7 @@ def post(url,
message = _parse(error.read()) message = _parse(error.read())
raise TelegramError(message) raise TelegramError(message)
except SSLError as error: except SSLError as error:
if "The read operation timed out" == error.message: if "operation timed out" in error.message:
raise TelegramError("Timed out") raise TelegramError("Timed out")
raise TelegramError(error.message) raise TelegramError(error.message)