Skip to content

Commit fef9e56

Browse files
committed
telegram.py: Reattach handlers after error
1 parent 6f96f36 commit fef9e56

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

modules/telegram.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727

2828
updater = None
2929
default_chat_id = None
30+
custom_command = None
3031
custom_command_callback = None
3132

32-
def bot_start(token, chat_id, custom_command = None, command_callback = None):
33+
def bot_start(token, chat_id, command = None, command_callback = None):
3334
global updater
3435
global default_chat_id
36+
global custom_command
3537
global custom_command_callback
3638

3739
default_chat_id = chat_id
@@ -40,9 +42,11 @@ def bot_start(token, chat_id, custom_command = None, command_callback = None):
4042

4143
updater.dispatcher.add_error_handler(on_error)
4244

43-
if custom_command is not None:
44-
updater.dispatcher.add_handler(telegram.ext.CommandHandler(custom_command, on_custom_command))
45-
custom_command_callback = command_callback
45+
if command is not None:
46+
updater.dispatcher.add_handler(telegram.ext.CommandHandler(command, on_custom_command))
47+
48+
custom_command = command
49+
custom_command_callback = command_callback
4650

4751
updater.start_polling()
4852

@@ -60,6 +64,7 @@ def on_custom_command(update, context):
6064

6165
def on_error(update, context):
6266
global updater
67+
global custom_command
6368

6469
logging.exception('Error in telegram bot', exc_info = context.error)
6570

@@ -69,6 +74,12 @@ def on_error(update, context):
6974
updater.stop()
7075
time.sleep(2)
7176
updater.start_polling()
77+
78+
# Reattach handlers
79+
updater.dispatcher.add_error_handler(on_error)
80+
if custom_command is not None:
81+
updater.dispatcher.add_handler(telegram.ext.CommandHandler(custom_command, on_custom_command))
82+
7283

7384

7485
def bot_stop():

0 commit comments

Comments
 (0)