From 4bc03ed56a5246917ec3d76f242c6bae94de63af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Tue, 10 Nov 2015 19:47:21 +0100 Subject: [PATCH] Add parameter network_delay for slow connections and increase default to 2 seconds --- telegram/bot.py | 10 +++++++++- telegram/utils/request.py | 11 +++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index 3805335be..52904d3a8 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -649,7 +649,8 @@ class Bot(TelegramObject): def getUpdates(self, offset=None, limit=100, - timeout=0): + timeout=0, + network_delay=2): """Use this method to receive incoming updates using long polling. Args: @@ -665,6 +666,11 @@ class Bot(TelegramObject): timeout: Timeout in seconds for long polling. Defaults to 0, i.e. usual 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: A list of telegram.Update objects are returned. @@ -679,6 +685,8 @@ class Bot(TelegramObject): data['limit'] = limit if timeout: data['timeout'] = timeout + if network_delay: + data['network_delay'] = network_delay result = request.post(url, data) diff --git a/telegram/utils/request.py b/telegram/utils/request.py index eaed476eb..0b6e0d2db 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -79,10 +79,13 @@ def post(url, A JSON object. """ - # Add one second to the timeout of urlopen to allow data to be transferred - # over the network. + # Add time to the timeout of urlopen to allow data to be transferred over + # the network. 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: timeout = None @@ -108,7 +111,7 @@ def post(url, message = _parse(error.read()) raise TelegramError(message) 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(error.message)