From aca4b912deb3914b5be8aeaa757e9db3644ead76 Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Fri, 2 Feb 2018 16:57:07 +0100 Subject: [PATCH] Handle WebSocketConnectionClosedException exception --- torrearchimedebot/bot/slack_controller.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/torrearchimedebot/bot/slack_controller.py b/torrearchimedebot/bot/slack_controller.py index 791df85..aa8a1f8 100644 --- a/torrearchimedebot/bot/slack_controller.py +++ b/torrearchimedebot/bot/slack_controller.py @@ -112,11 +112,19 @@ def __init__(self): SlackController.starterbot_id = \ SlackController.slack_client.api_call("auth.test")["user_id"] while True: - command, channel = SlackController.parse_bot_commands( - SlackController.slack_client.rtm_read()) - if command: - SlackController.handle_command(command, channel) - t.sleep(SlackController.RTM_READ_DELAY) + try: + command, channel = SlackController.parse_bot_commands( + SlackController.slack_client.rtm_read()) + if command: + SlackController.handle_command(command, channel) + t.sleep(SlackController.RTM_READ_DELAY) + except WebSocketConnectionClosedException: + logger.getLogger().error("Lost connection to Slack, reconnecting...") + if not sc.rtm_connect(): + logger.getLogger().info("Failed to reconnect to Slack") + time.sleep(SlackController.RTM_READ_DELAY) + else: + logger.getLogger().info("Reconnected to Slack") else: logging.getLogger().info("SLACK - Connection failed. Exception " + \ "traceback printed above.")