Skip to content

Commit 03e1aef

Browse files
committed
long_polling_timeout update 1
1 parent 7a3fd30 commit 03e1aef

3 files changed

Lines changed: 24 additions & 19 deletions

File tree

‎.travis.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ python:
44
- "3.6"
55
- "3.7"
66
- "3.8"
7+
- "3.9"
78
- "pypy3"
89
install: "pip install -r requirements.txt"
910
script:

‎telebot/__init__.py‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,17 @@ def get_webhook_info(self):
252252
def remove_webhook(self):
253253
return self.set_webhook() # No params resets webhook
254254

255-
def get_updates(self, offset=None, limit=None, timeout=20, allowed_updates=None):
255+
def get_updates(self, offset=None, limit=None, timeout=20, allowed_updates=None, long_polling_timeout = 20):
256256
"""
257257
Use this method to receive incoming updates using long polling (wiki). An Array of Update objects is returned.
258258
:param allowed_updates: Array of string. List the types of updates you want your bot to receive.
259259
:param offset: Integer. Identifier of the first update to be returned.
260260
:param limit: Integer. Limits the number of updates to be retrieved.
261-
:param timeout: Integer. Timeout in seconds for long polling.
261+
:param timeout: Integer. Request connection timeout
262+
:param long_polling_timeout. Timeout in seconds for long polling.
262263
:return: array of Updates
263264
"""
264-
json_updates = apihelper.get_updates(self.token, offset, limit, timeout, allowed_updates)
265+
json_updates = apihelper.get_updates(self.token, offset, limit, timeout, allowed_updates, long_polling_timeout)
265266
ret = []
266267
for ju in json_updates:
267268
ret.append(types.Update.de_json(ju))
@@ -273,16 +274,16 @@ def __skip_updates(self):
273274
:return: total updates skipped
274275
"""
275276
total = 0
276-
updates = self.get_updates(offset=self.last_update_id, timeout=1)
277+
updates = self.get_updates(offset=self.last_update_id, long_polling_timeout=1)
277278
while updates:
278279
total += len(updates)
279280
for update in updates:
280281
if update.update_id > self.last_update_id:
281282
self.last_update_id = update.update_id
282-
updates = self.get_updates(offset=self.last_update_id + 1, timeout=1)
283+
updates = self.get_updates(offset=self.last_update_id + 1, long_polling_timeout=1)
283284
return total
284285

285-
def __retrieve_updates(self, timeout=20):
286+
def __retrieve_updates(self, timeout=20, long_polling_timeout=20):
286287
"""
287288
Retrieves any updates from the Telegram API.
288289
Registered listeners and applicable message handlers will be notified when a new message arrives.
@@ -291,7 +292,7 @@ def __retrieve_updates(self, timeout=20):
291292
if self.skip_pending:
292293
logger.debug('Skipped {0} pending messages'.format(self.__skip_updates()))
293294
self.skip_pending = False
294-
updates = self.get_updates(offset=(self.last_update_id + 1), timeout=timeout)
295+
updates = self.get_updates(offset=(self.last_update_id + 1), timeout=timeout, long_polling_timeout = long_polling_timeout)
295296
self.process_new_updates(updates)
296297

297298
def process_new_updates(self, updates):
@@ -427,16 +428,16 @@ def __notify_update(self, new_messages):
427428
for listener in self.update_listener:
428429
self._exec_task(listener, new_messages)
429430

430-
def infinity_polling(self, timeout=20, *args, **kwargs):
431+
def infinity_polling(self, timeout=20, long_polling_timeout=20, *args, **kwargs):
431432
while not self.__stop_polling.is_set():
432433
try:
433-
self.polling(timeout=timeout, *args, **kwargs)
434+
self.polling(timeout=timeout, long_polling_timeout=long_polling_timeout, *args, **kwargs)
434435
except Exception:
435436
time.sleep(timeout)
436437
pass
437438
logger.info("Break infinity polling")
438439

439-
def polling(self, none_stop=False, interval=0, timeout=20):
440+
def polling(self, none_stop=False, interval=0, timeout=20, long_polling_timeout=20):
440441
"""
441442
This function creates a new Thread that calls an internal __retrieve_updates function.
442443
This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly.
@@ -446,15 +447,16 @@ def polling(self, none_stop=False, interval=0, timeout=20):
446447
Always get updates.
447448
:param interval:
448449
:param none_stop: Do not stop polling when an ApiException occurs.
449-
:param timeout: Timeout in seconds for long polling.
450+
:param timeout: Integer. Request connection timeout
451+
:param long_polling_timeout. Timeout in seconds for long polling.
450452
:return:
451453
"""
452454
if self.threaded:
453-
self.__threaded_polling(none_stop, interval, timeout)
455+
self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout)
454456
else:
455-
self.__non_threaded_polling(none_stop, interval, timeout)
457+
self.__non_threaded_polling(none_stop, interval, timeout, long_polling_timeout)
456458

457-
def __threaded_polling(self, none_stop=False, interval=0, timeout=3):
459+
def __threaded_polling(self, none_stop=False, interval=0, timeout = None, long_polling_timeout = None):
458460
logger.info('Started polling.')
459461
self.__stop_polling.clear()
460462
error_interval = 0.25
@@ -469,7 +471,7 @@ def __threaded_polling(self, none_stop=False, interval=0, timeout=3):
469471
while not self.__stop_polling.wait(interval):
470472
or_event.clear()
471473
try:
472-
polling_thread.put(self.__retrieve_updates, timeout)
474+
polling_thread.put(self.__retrieve_updates, timeout, long_polling_timeout)
473475

474476
or_event.wait() # wait for polling thread finish, polling thread error or thread pool error
475477

@@ -517,14 +519,14 @@ def __threaded_polling(self, none_stop=False, interval=0, timeout=3):
517519
polling_thread.stop()
518520
logger.info('Stopped polling.')
519521

520-
def __non_threaded_polling(self, none_stop=False, interval=0, timeout=3):
522+
def __non_threaded_polling(self, none_stop=False, interval=0, timeout = None, long_polling_timeout = None):
521523
logger.info('Started polling.')
522524
self.__stop_polling.clear()
523525
error_interval = 0.25
524526

525527
while not self.__stop_polling.wait(interval):
526528
try:
527-
self.__retrieve_updates(timeout)
529+
self.__retrieve_updates(timeout, long_polling_timeout)
528530
error_interval = 0.25
529531
except apihelper.ApiException as e:
530532
if self.exception_handler is not None:

‎telebot/apihelper.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,17 @@ def get_webhook_info(token):
232232
return _make_request(token, method_url, params=payload)
233233

234234

235-
def get_updates(token, offset=None, limit=None, timeout=None, allowed_updates=None):
235+
def get_updates(token, offset=None, limit=None, timeout=None, allowed_updates=None, long_polling_timeout = None):
236236
method_url = r'getUpdates'
237237
payload = {}
238238
if offset:
239239
payload['offset'] = offset
240240
if limit:
241241
payload['limit'] = limit
242242
if timeout:
243-
payload['long_polling_timeout'] = timeout
243+
payload['timeout'] = timeout
244+
if long_polling_timeout:
245+
payload['long_polling_timeout'] = long_polling_timeout
244246
if allowed_updates:
245247
payload['allowed_updates'] = json.dumps(allowed_updates)
246248
return _make_request(token, method_url, params=payload)

0 commit comments

Comments
 (0)