mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Add timeout para for polling method.
This commit is contained in:
parent
9cd28b88be
commit
1c53955d5a
@ -8,6 +8,7 @@ import sys
|
||||
import six
|
||||
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger('TeleBot')
|
||||
formatter = logging.Formatter('%(asctime)s (%(filename)s:%(lineno)d) %(levelname)s - %(name)s: "%(message)s"')
|
||||
|
||||
@ -22,6 +23,8 @@ from telebot import apihelper, types, util
|
||||
"""
|
||||
Module : telebot
|
||||
"""
|
||||
|
||||
|
||||
class TeleBot:
|
||||
""" This is TeleBot Class
|
||||
Methods:
|
||||
@ -79,13 +82,13 @@ class TeleBot:
|
||||
ret.append(types.Update.de_json(ju))
|
||||
return ret
|
||||
|
||||
def get_update(self):
|
||||
def get_update(self, timeout=20):
|
||||
"""
|
||||
Retrieves any updates from the Telegram API.
|
||||
Registered listeners and applicable message handlers will be notified when a new message arrives.
|
||||
:raises ApiException when a call has failed.
|
||||
"""
|
||||
updates = self.get_updates(offset=(self.last_update_id + 1), timeout=3)
|
||||
updates = self.get_updates(offset=(self.last_update_id + 1), timeout=timeout)
|
||||
new_messages = []
|
||||
for update in updates:
|
||||
if update.update_id > self.last_update_id:
|
||||
@ -108,7 +111,7 @@ class TeleBot:
|
||||
else:
|
||||
listener(new_messages)
|
||||
|
||||
def polling(self, none_stop=False, interval=0, block=True):
|
||||
def polling(self, none_stop=False, interval=0, block=True, timeout=20):
|
||||
"""
|
||||
This function creates a new Thread that calls an internal __polling function.
|
||||
This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly.
|
||||
@ -117,13 +120,14 @@ class TeleBot:
|
||||
|
||||
Always get updates.
|
||||
:param none_stop: Do not stop polling when Exception occur.
|
||||
:param timeout: Timeout in seconds for long polling.
|
||||
:return:
|
||||
"""
|
||||
self.__stop_polling.set()
|
||||
if self.polling_thread:
|
||||
self.polling_thread.join() # wait thread stop.
|
||||
self.__stop_polling.clear()
|
||||
self.polling_thread = threading.Thread(target=self.__polling, args=([none_stop, interval]))
|
||||
self.polling_thread = threading.Thread(target=self.__polling, args=([none_stop, interval, timeout]))
|
||||
self.polling_thread.daemon = True
|
||||
self.polling_thread.start()
|
||||
|
||||
@ -137,13 +141,13 @@ class TeleBot:
|
||||
self.polling_thread.join()
|
||||
break
|
||||
|
||||
def __polling(self, none_stop, interval):
|
||||
def __polling(self, none_stop, interval, timeout):
|
||||
logger.info('Started polling.')
|
||||
|
||||
error_interval = .25
|
||||
while not self.__stop_polling.wait(interval):
|
||||
try:
|
||||
self.get_update()
|
||||
self.get_update(timeout)
|
||||
error_interval = .25
|
||||
except apihelper.ApiException as e:
|
||||
if not none_stop:
|
||||
@ -410,6 +414,7 @@ class TeleBot:
|
||||
:param func: Optional lambda function. The lambda receives the message to test as the first parameter. It must return True if the command should handle the message.
|
||||
:param content_types: This commands' supported content types. Must be a list. Defaults to ['text'].
|
||||
"""
|
||||
|
||||
def decorator(fn):
|
||||
handler_dict = {'function': fn}
|
||||
filters = {'content_types': content_types}
|
||||
|
Loading…
Reference in New Issue
Block a user