Skip to content

Commit 185b3e0

Browse files
committed
Polling interval support
issue eternnoir#38
1 parent aeeb94d commit 185b3e0

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def __notify_update(self, new_messages):
125125
else:
126126
listener(new_messages)
127127

128-
def polling(self, none_stop=False):
128+
def polling(self, none_stop=False, interval=0):
129129
"""
130130
This function creates a new Thread that calls an internal __polling function.
131131
This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly.
@@ -136,12 +136,15 @@ def polling(self, none_stop=False):
136136
:param none_stop: Do not stop polling when Exception occur.
137137
:return:
138138
"""
139+
self.__stop_polling = True
140+
if self.polling_thread:
141+
self.polling_thread.join() # wait thread stop.
139142
self.__stop_polling = False
140-
self.polling_thread = threading.Thread(target=self.__polling, args=([none_stop]))
143+
self.polling_thread = threading.Thread(target=self.__polling, args=([none_stop, interval]))
141144
self.polling_thread.daemon = True
142145
self.polling_thread.start()
143146

144-
def __polling(self, none_stop):
147+
def __polling(self, none_stop, interval):
145148
print('TeleBot: Started polling.')
146149
while not self.__stop_polling:
147150
try:
@@ -151,6 +154,7 @@ def __polling(self, none_stop):
151154
self.__stop_polling = True
152155
print("TeleBot: Exception occurred. Stopping.")
153156
print(e)
157+
time.sleep(interval)
154158

155159
print('TeleBot: Stopped polling.')
156160

0 commit comments

Comments
 (0)