Skip to content

Commit 4bc03ed

Browse files
committed
Add parameter network_delay for slow connections and increase default to 2 seconds
1 parent 9191d33 commit 4bc03ed

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

telegram/bot.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,8 @@ def getFile(self,
649649
def getUpdates(self,
650650
offset=None,
651651
limit=100,
652-
timeout=0):
652+
timeout=0,
653+
network_delay=2):
653654
"""Use this method to receive incoming updates using long polling.
654655
655656
Args:
@@ -665,6 +666,11 @@ def getUpdates(self,
665666
timeout:
666667
Timeout in seconds for long polling. Defaults to 0, i.e. usual
667668
short polling.
669+
network_delay:
670+
Additional timeout in seconds to allow the response from Telegram to
671+
take some time when using long polling. Defaults to 2, which should
672+
be enough for most connections. Increase it if it takes very long
673+
for data to be transmitted from and to the Telegram servers.
668674
669675
Returns:
670676
A list of telegram.Update objects are returned.
@@ -679,6 +685,8 @@ def getUpdates(self,
679685
data['limit'] = limit
680686
if timeout:
681687
data['timeout'] = timeout
688+
if network_delay:
689+
data['network_delay'] = network_delay
682690

683691
result = request.post(url, data)
684692

telegram/utils/request.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ def post(url,
7979
A JSON object.
8080
"""
8181

82-
# Add one second to the timeout of urlopen to allow data to be transferred
83-
# over the network.
82+
# Add time to the timeout of urlopen to allow data to be transferred over
83+
# the network.
8484
if 'timeout' in data:
85-
timeout = data['timeout'] + 1.
85+
if 'network_delay' in data:
86+
timeout = data['timeout'] + data['network_delay']
87+
else:
88+
timeout = data['timeout'] + 2.
8689
else:
8790
timeout = None
8891

@@ -108,7 +111,7 @@ def post(url,
108111
message = _parse(error.read())
109112
raise TelegramError(message)
110113
except SSLError as error:
111-
if "The read operation timed out" == error.message:
114+
if "operation timed out" in error.message:
112115
raise TelegramError("Timed out")
113116

114117
raise TelegramError(error.message)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy