@@ -455,17 +455,28 @@ def __notify_update(self, new_messages):
455
455
for listener in self .update_listener :
456
456
self ._exec_task (listener , new_messages )
457
457
458
- def infinity_polling (self , timeout = 20 , long_polling_timeout = 20 , * args , ** kwargs ):
458
+ def infinity_polling (self , timeout = 20 , long_polling_timeout = 20 , logger_level = logging .ERROR , * args , ** kwargs ):
459
+ """
460
+ Wrap polling with infinite loop and exception handling to avoid bot stops polling.
461
+
462
+ :param timeout: Request connection timeout
463
+ :param long_polling_timeout: Timeout in seconds for long polling (see API docs)
464
+ :param logger_level: Custom logging level for infinity_polling logging. None/NOTSET = no error logging
465
+ """
459
466
while not self .__stop_polling .is_set ():
460
467
try :
461
468
self .polling (none_stop = True , timeout = timeout , long_polling_timeout = long_polling_timeout , * args , ** kwargs )
462
469
except Exception as e :
463
- logger .error ("Infinity polling exception: %s" , str (e ))
464
- logger .debug ("Exception traceback:\n %s" , traceback .format_exc ())
470
+ if logger_level and logger_level >= logging .ERROR :
471
+ logger .error ("Infinity polling exception: %s" , str (e ))
472
+ if logger_level and logger_level >= logging .DEBUG :
473
+ logger .error ("Exception traceback:\n %s" , traceback .format_exc ())
465
474
time .sleep (3 )
466
475
continue
467
- logger .info ("Infinity polling: polling exited" )
468
- logger .info ("Break infinity polling" )
476
+ if logger_level and logger_level >= logging .INFO :
477
+ logger .error ("Infinity polling: polling exited" )
478
+ if logger_level and logger_level >= logging .INFO :
479
+ logger .error ("Break infinity polling" )
469
480
470
481
def polling (self , none_stop = False , interval = 0 , timeout = 20 , long_polling_timeout = 20 ):
471
482
"""
@@ -475,10 +486,10 @@ def polling(self, none_stop=False, interval=0, timeout=20, long_polling_timeout=
475
486
Warning: Do not call this function more than once!
476
487
477
488
Always get updates.
478
- :param interval:
489
+ :param interval: Delay between two update retrivals
479
490
:param none_stop: Do not stop polling when an ApiException occurs.
480
- :param timeout: Integer. Request connection timeout
481
- :param long_polling_timeout. Timeout in seconds for long polling.
491
+ :param timeout: Request connection timeout
492
+ :param long_polling_timeout: Timeout in seconds for long polling (see API docs)
482
493
:return:
483
494
"""
484
495
if self .threaded :
0 commit comments