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 six
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger('TeleBot')
|
logger = logging.getLogger('TeleBot')
|
||||||
formatter = logging.Formatter('%(asctime)s (%(filename)s:%(lineno)d) %(levelname)s - %(name)s: "%(message)s"')
|
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
|
Module : telebot
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class TeleBot:
|
class TeleBot:
|
||||||
""" This is TeleBot Class
|
""" This is TeleBot Class
|
||||||
Methods:
|
Methods:
|
||||||
@ -79,13 +82,13 @@ class TeleBot:
|
|||||||
ret.append(types.Update.de_json(ju))
|
ret.append(types.Update.de_json(ju))
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def get_update(self):
|
def get_update(self, timeout=20):
|
||||||
"""
|
"""
|
||||||
Retrieves any updates from the Telegram API.
|
Retrieves any updates from the Telegram API.
|
||||||
Registered listeners and applicable message handlers will be notified when a new message arrives.
|
Registered listeners and applicable message handlers will be notified when a new message arrives.
|
||||||
:raises ApiException when a call has failed.
|
: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 = []
|
new_messages = []
|
||||||
for update in updates:
|
for update in updates:
|
||||||
if update.update_id > self.last_update_id:
|
if update.update_id > self.last_update_id:
|
||||||
@ -108,7 +111,7 @@ class TeleBot:
|
|||||||
else:
|
else:
|
||||||
listener(new_messages)
|
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 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.
|
This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly.
|
||||||
@ -117,13 +120,14 @@ class TeleBot:
|
|||||||
|
|
||||||
Always get updates.
|
Always get updates.
|
||||||
:param none_stop: Do not stop polling when Exception occur.
|
:param none_stop: Do not stop polling when Exception occur.
|
||||||
|
:param timeout: Timeout in seconds for long polling.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.__stop_polling.set()
|
self.__stop_polling.set()
|
||||||
if self.polling_thread:
|
if self.polling_thread:
|
||||||
self.polling_thread.join() # wait thread stop.
|
self.polling_thread.join() # wait thread stop.
|
||||||
self.__stop_polling.clear()
|
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.daemon = True
|
||||||
self.polling_thread.start()
|
self.polling_thread.start()
|
||||||
|
|
||||||
@ -137,13 +141,13 @@ class TeleBot:
|
|||||||
self.polling_thread.join()
|
self.polling_thread.join()
|
||||||
break
|
break
|
||||||
|
|
||||||
def __polling(self, none_stop, interval):
|
def __polling(self, none_stop, interval, timeout):
|
||||||
logger.info('Started polling.')
|
logger.info('Started polling.')
|
||||||
|
|
||||||
error_interval = .25
|
error_interval = .25
|
||||||
while not self.__stop_polling.wait(interval):
|
while not self.__stop_polling.wait(interval):
|
||||||
try:
|
try:
|
||||||
self.get_update()
|
self.get_update(timeout)
|
||||||
error_interval = .25
|
error_interval = .25
|
||||||
except apihelper.ApiException as e:
|
except apihelper.ApiException as e:
|
||||||
if not none_stop:
|
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 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'].
|
:param content_types: This commands' supported content types. Must be a list. Defaults to ['text'].
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def decorator(fn):
|
def decorator(fn):
|
||||||
handler_dict = {'function': fn}
|
handler_dict = {'function': fn}
|
||||||
filters = {'content_types': content_types}
|
filters = {'content_types': content_types}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user