X Tutup
Skip to content

Commit 0c74b3c

Browse files
committed
bot.py + request.py: network_delay is unique for getUpdates
1 parent 0ca3ef7 commit 0c74b3c

File tree

2 files changed

+24
-124
lines changed

2 files changed

+24
-124
lines changed

telegram/bot.py

Lines changed: 23 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ def decorator(self, *args, **kwargs):
139139
data['reply_markup'] = reply_markup
140140

141141
result = request.post(url, data,
142-
timeout=kwargs.get('timeout'),
143-
network_delay=kwargs.get('network_delay'))
142+
timeout=kwargs.get('timeout'))
144143

145144
if result is True:
146145
return result
@@ -206,10 +205,6 @@ def sendMessage(self,
206205
keyboard or to force a reply from the user.
207206
timeout (Optional[float]): If this value is specified, use it as
208207
the definitive timeout (in seconds) for urlopen() operations.
209-
network_delay (Optional[float]): If using the timeout (which is
210-
a `timeout` for the Telegram servers operation),
211-
then `network_delay` as an extra delay (in seconds) to
212-
compensate for network latency. Defaults to 2.
213208
214209
Returns:
215210
:class:`telegram.Message`: On success, the sent message is
@@ -256,10 +251,6 @@ def forwardMessage(self,
256251
receive a notification with no sound.
257252
timeout (Optional[float]): If this value is specified, use it as
258253
the definitive timeout (in seconds) for urlopen() operations.
259-
network_delay (Optional[float]): If using the timeout (which is
260-
a `timeout` for the Telegram servers operation),
261-
then `network_delay` as an extra delay (in seconds) to
262-
compensate for network latency. Defaults to 2.
263254
264255
Returns:
265256
:class:`telegram.Message`: On success, instance representing the
@@ -315,10 +306,6 @@ def sendPhoto(self,
315306
keyboard or to force a reply from the user.
316307
timeout (Optional[float]): If this value is specified, use it as
317308
the definitive timeout (in seconds) for urlopen() operations.
318-
network_delay (Optional[float]): If using the timeout (which is
319-
a `timeout` for the Telegram servers operation),
320-
then `network_delay` as an extra delay (in seconds) to
321-
compensate for network latency. Defaults to 2.
322309
323310
Returns:
324311
:class:`telegram.Message`: On success, instance representing the
@@ -385,10 +372,6 @@ def sendAudio(self,
385372
keyboard or to force a reply from the user.
386373
timeout (Optional[float]): If this value is specified, use it as
387374
the definitive timeout (in seconds) for urlopen() operations.
388-
network_delay (Optional[float]): If using the timeout (which is
389-
a `timeout` for the Telegram servers operation),
390-
then `network_delay` as an extra delay (in seconds) to
391-
compensate for network latency. Defaults to 2.
392375
393376
Returns:
394377
:class:`telegram.Message`: On success, instance representing the
@@ -449,10 +432,6 @@ def sendDocument(self,
449432
keyboard or to force a reply from the user.
450433
timeout (Optional[float]): If this value is specified, use it as
451434
the definitive timeout (in seconds) for urlopen() operations.
452-
network_delay (Optional[float]): If using the timeout (which is
453-
a `timeout` for the Telegram servers operation),
454-
then `network_delay` as an extra delay (in seconds) to
455-
compensate for network latency. Defaults to 2.
456435
457436
Returns:
458437
:class:`telegram.Message`: On success, instance representing the
@@ -503,10 +482,6 @@ def sendSticker(self,
503482
keyboard or to force a reply from the user.
504483
timeout (Optional[float]): If this value is specified, use it as
505484
the definitive timeout (in seconds) for urlopen() operations.
506-
network_delay (Optional[float]): If using the timeout (which is
507-
a `timeout` for the Telegram servers operation),
508-
then `network_delay` as an extra delay (in seconds) to
509-
compensate for network latency. Defaults to 2.
510485
511486
Returns:
512487
:class:`telegram.Message`: On success, instance representing the
@@ -560,10 +535,6 @@ def sendVideo(self,
560535
keyboard or to force a reply from the user.
561536
timeout (Optional[float]): If this value is specified, use it as
562537
the definitive timeout (in seconds) for urlopen() operations.
563-
network_delay (Optional[float]): If using the timeout (which is
564-
a `timeout` for the Telegram servers operation),
565-
then `network_delay` as an extra delay (in seconds) to
566-
compensate for network latency. Defaults to 2.
567538
568539
Returns:
569540
:class:`telegram.Message`: On success, instance representing the
@@ -622,10 +593,6 @@ def sendVoice(self,
622593
keyboard or to force a reply from the user.
623594
timeout (Optional[float]): If this value is specified, use it as
624595
the definitive timeout (in seconds) for urlopen() operations.
625-
network_delay (Optional[float]): If using the timeout (which is
626-
a `timeout` for the Telegram servers operation),
627-
then `network_delay` as an extra delay (in seconds) to
628-
compensate for network latency. Defaults to 2.
629596
630597
Returns:
631598
:class:`telegram.Message`: On success, instance representing the
@@ -675,10 +642,6 @@ def sendLocation(self,
675642
keyboard or to force a reply from the user.
676643
timeout (Optional[float]): If this value is specified, use it as
677644
the definitive timeout (in seconds) for urlopen() operations.
678-
network_delay (Optional[float]): If using the timeout (which is
679-
a `timeout` for the Telegram servers operation),
680-
then `network_delay` as an extra delay (in seconds) to
681-
compensate for network latency. Defaults to 2.
682645
683646
Returns:
684647
:class:`telegram.Message`: On success, instance representing the
@@ -737,10 +700,6 @@ def sendVenue(self,
737700
keyboard or to force a reply from the user.
738701
timeout (Optional[float]): If this value is specified, use it as
739702
the definitive timeout (in seconds) for urlopen() operations.
740-
network_delay (Optional[float]): If using the timeout (which is
741-
a `timeout` for the Telegram servers operation),
742-
then `network_delay` as an extra delay (in seconds) to
743-
compensate for network latency. Defaults to 2.
744703
745704
Returns:
746705
:class:`telegram.Message`: On success, instance representing the
@@ -798,10 +757,6 @@ def sendContact(self,
798757
keyboard or to force a reply from the user.
799758
timeout (Optional[float]): If this value is specified, use it as
800759
the definitive timeout (in seconds) for urlopen() operations.
801-
network_delay (Optional[float]): If using the timeout (which is
802-
a `timeout` for the Telegram servers operation),
803-
then `network_delay` as an extra delay (in seconds) to
804-
compensate for network latency. Defaults to 2.
805760
806761
Returns:
807762
:class:`telegram.Message`: On success, instance representing the
@@ -893,10 +848,6 @@ def answerInlineQuery(self,
893848
Keyword Args:
894849
timeout (Optional[float]): If this value is specified, use it as
895850
the definitive timeout (in seconds) for urlopen() operations.
896-
network_delay (Optional[float]): If using the timeout (which is
897-
a `timeout` for the Telegram servers operation),
898-
then `network_delay` as an extra delay (in seconds) to
899-
compensate for network latency. Defaults to 2.
900851
901852
Returns:
902853
bool: On success, `True` is returned.
@@ -925,8 +876,7 @@ def answerInlineQuery(self,
925876
data['switch_pm_parameter'] = switch_pm_parameter
926877

927878
result = request.post(url, data,
928-
timeout=kwargs.get('timeout'),
929-
network_delay=kwargs.get('network_delay'))
879+
timeout=kwargs.get('timeout'))
930880

931881
return result
932882

@@ -951,10 +901,6 @@ def getUserProfilePhotos(self,
951901
Keyword Args:
952902
timeout (Optional[float]): If this value is specified, use it as
953903
the definitive timeout (in seconds) for urlopen() operations.
954-
network_delay (Optional[float]): If using the timeout (which is
955-
a `timeout` for the Telegram servers operation),
956-
then `network_delay` as an extra delay (in seconds) to
957-
compensate for network latency. Defaults to 2.
958904
959905
Returns:
960906
list[:class:`telegram.UserProfilePhotos`]: A list of
@@ -975,8 +921,7 @@ def getUserProfilePhotos(self,
975921
data['limit'] = limit
976922

977923
result = request.post(url, data,
978-
timeout=kwargs.get('timeout'),
979-
network_delay=kwargs.get('network_delay'))
924+
timeout=kwargs.get('timeout'))
980925

981926
return UserProfilePhotos.de_json(result)
982927

@@ -995,10 +940,6 @@ def getFile(self,
995940
Keyword Args:
996941
timeout (Optional[float]): If this value is specified, use it as
997942
the definitive timeout (in seconds) for urlopen() operations.
998-
network_delay (Optional[float]): If using the timeout (which is
999-
a `timeout` for the Telegram servers operation),
1000-
then `network_delay` as an extra delay (in seconds) to
1001-
compensate for network latency. Defaults to 2.
1002943
1003944
Returns:
1004945
:class:`telegram.File`: On success, a :class:`telegram.File`
@@ -1014,8 +955,7 @@ def getFile(self,
1014955
data = {'file_id': file_id}
1015956

1016957
result = request.post(url, data,
1017-
timeout=kwargs.get('timeout'),
1018-
network_delay=kwargs.get('network_delay'))
958+
timeout=kwargs.get('timeout'))
1019959

1020960
if result.get('file_path'):
1021961
result['file_path'] = '%s/%s' % (self.base_file_url,
@@ -1043,10 +983,6 @@ def kickChatMember(self,
1043983
Keyword Args:
1044984
timeout (Optional[float]): If this value is specified, use it as
1045985
the definitive timeout (in seconds) for urlopen() operations.
1046-
network_delay (Optional[float]): If using the timeout (which is
1047-
a `timeout` for the Telegram servers operation),
1048-
then `network_delay` as an extra delay (in seconds) to
1049-
compensate for network latency. Defaults to 2.
1050986
1051987
Returns:
1052988
bool: On success, `True` is returned.
@@ -1062,8 +998,7 @@ def kickChatMember(self,
1062998
'user_id': user_id}
1063999

10641000
result = request.post(url, data,
1065-
timeout=kwargs.get('timeout'),
1066-
network_delay=kwargs.get('network_delay'))
1001+
timeout=kwargs.get('timeout'))
10671002

10681003
return result
10691004

@@ -1087,10 +1022,6 @@ def unbanChatMember(self,
10871022
Keyword Args:
10881023
timeout (Optional[float]): If this value is specified, use it as
10891024
the definitive timeout (in seconds) for urlopen() operations.
1090-
network_delay (Optional[float]): If using the timeout (which is
1091-
a `timeout` for the Telegram servers operation),
1092-
then `network_delay` as an extra delay (in seconds) to
1093-
compensate for network latency. Defaults to 2.
10941025
10951026
Returns:
10961027
bool: On success, `True` is returned.
@@ -1106,8 +1037,7 @@ def unbanChatMember(self,
11061037
'user_id': user_id}
11071038

11081039
result = request.post(url, data,
1109-
timeout=kwargs.get('timeout'),
1110-
network_delay=kwargs.get('network_delay'))
1040+
timeout=kwargs.get('timeout'))
11111041

11121042
return result
11131043

@@ -1133,10 +1063,6 @@ def answerCallbackQuery(self,
11331063
Keyword Args:
11341064
timeout (Optional[float]): If this value is specified, use it as
11351065
the definitive timeout (in seconds) for urlopen() operations.
1136-
network_delay (Optional[float]): If using the timeout (which is
1137-
a `timeout` for the Telegram servers operation),
1138-
then `network_delay` as an extra delay (in seconds) to
1139-
compensate for network latency. Defaults to 2.
11401066
11411067
Returns:
11421068
bool: On success, `True` is returned.
@@ -1156,8 +1082,7 @@ def answerCallbackQuery(self,
11561082
data['show_alert'] = show_alert
11571083

11581084
result = request.post(url, data,
1159-
timeout=kwargs.get('timeout'),
1160-
network_delay=kwargs.get('network_delay'))
1085+
timeout=kwargs.get('timeout'))
11611086

11621087
return result
11631088

@@ -1198,10 +1123,6 @@ def editMessageText(self,
11981123
Keyword Args:
11991124
timeout (Optional[float]): If this value is specified, use it as
12001125
the definitive timeout (in seconds) for urlopen() operations.
1201-
network_delay (Optional[float]): If using the timeout (which is
1202-
a `timeout` for the Telegram servers operation),
1203-
then `network_delay` as an extra delay (in seconds) to
1204-
compensate for network latency. Defaults to 2.
12051126
12061127
Returns:
12071128
:class:`telegram.Message`: On success, if edited message is sent by
@@ -1234,8 +1155,7 @@ def editMessageText(self,
12341155
data['reply_markup'] = reply_markup
12351156

12361157
result = request.post(url, data,
1237-
timeout=kwargs.get('timeout'),
1238-
network_delay=kwargs.get('network_delay'))
1158+
timeout=kwargs.get('timeout'))
12391159

12401160
return Message.de_json(result)
12411161

@@ -1266,10 +1186,6 @@ def editMessageCaption(self,
12661186
A JSON-serialized object for an inline keyboard.
12671187
timeout (Optional[float]): If this value is specified, use it as
12681188
the definitive timeout (in seconds) for urlopen() operations.
1269-
network_delay (Optional[float]): If using the timeout (which is
1270-
a `timeout` for the Telegram servers operation),
1271-
then `network_delay` as an extra delay (in seconds) to
1272-
compensate for network latency. Defaults to 2.
12731189
12741190
Returns:
12751191
:class:`telegram.Message`: On success, if edited message is sent by
@@ -1321,10 +1237,6 @@ def editMessageReplyMarkup(self,
13211237
A JSON-serialized object for an inline keyboard.
13221238
timeout (Optional[float]): If this value is specified, use it as
13231239
the definitive timeout (in seconds) for urlopen() operations.
1324-
network_delay (Optional[float]): If using the timeout (which is
1325-
a `timeout` for the Telegram servers operation),
1326-
then `network_delay` as an extra delay (in seconds) to
1327-
compensate for network latency. Defaults to 2.
13281240
13291241
Returns:
13301242
:class:`telegram.Message`: On success, if edited message is sent by
@@ -1353,7 +1265,8 @@ def editMessageReplyMarkup(self,
13531265
def getUpdates(self,
13541266
offset=None,
13551267
limit=100,
1356-
**kwargs):
1268+
timeout=0,
1269+
network_delay=.2):
13571270
"""Use this method to receive incoming updates using long polling.
13581271
13591272
Args:
@@ -1366,14 +1279,14 @@ def getUpdates(self,
13661279
limit:
13671280
Limits the number of updates to be retrieved. Values between 1-100
13681281
are accepted. Defaults to 100.
1369-
1370-
Keyword Args:
1371-
timeout (Optional[float]): If this value is specified, use it as
1372-
the definitive timeout (in seconds) for urlopen() operations.
1373-
network_delay (Optional[float]): If using the timeout (which is
1374-
a `timeout` for the Telegram servers operation),
1375-
then `network_delay` as an extra delay (in seconds) to
1376-
compensate for network latency. Defaults to 2.
1282+
timeout:
1283+
Timeout in seconds for long polling. Defaults to 0, i.e. usual
1284+
short polling.
1285+
network_delay:
1286+
Additional timeout in seconds to allow the response from Telegram
1287+
to take some time when using long polling. Defaults to 2, which
1288+
should be enough for most connections. Increase it if it takes very
1289+
long for data to be transmitted from and to the Telegram servers.
13771290
13781291
Returns:
13791292
list[:class:`telegram.Message`]: A list of :class:`telegram.Update`
@@ -1386,16 +1299,16 @@ def getUpdates(self,
13861299

13871300
url = '{0}/getUpdates'.format(self.base_url)
13881301

1389-
data = {}
1302+
data = {'timeout': timeout}
13901303

13911304
if offset:
13921305
data['offset'] = offset
13931306
if limit:
13941307
data['limit'] = limit
13951308

1396-
result = request.post(url, data,
1397-
timeout=kwargs.get('timeout'),
1398-
network_delay=kwargs.get('network_delay'))
1309+
urlopen_timeout = timeout + network_delay
1310+
1311+
result = request.post(url, data, timeout=urlopen_timeout)
13991312

14001313
if result:
14011314
self.logger.debug(
@@ -1424,10 +1337,6 @@ def setWebhook(self,
14241337
Keyword Args:
14251338
timeout (Optional[float]): If this value is specified, use it as
14261339
the definitive timeout (in seconds) for urlopen() operations.
1427-
network_delay (Optional[float]): If using the timeout (which is
1428-
a `timeout` for the Telegram servers operation),
1429-
then `network_delay` as an extra delay (in seconds) to
1430-
compensate for network latency. Defaults to 2.
14311340
14321341
Returns:
14331342
bool: On success, `True` is returned.
@@ -1447,8 +1356,7 @@ def setWebhook(self,
14471356
data['certificate'] = certificate
14481357

14491358
result = request.post(url, data,
1450-
timeout=kwargs.get('timeout'),
1451-
network_delay=kwargs.get('network_delay'))
1359+
timeout=kwargs.get('timeout'))
14521360

14531361
return result
14541362

0 commit comments

Comments
 (0)
X Tutup