From: Carl Worth Date: Fri, 9 Oct 2020 22:41:11 +0000 (-0700) Subject: Use a thread for creating the Google Sheet when a new channel is created X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=3498d36fd61689e078c6eb46f1b103272f524e36;p=turbot Use a thread for creating the Google Sheet when a new channel is created This allows us to return quickly so that Slack doesn't retry sending the event. As implemented in this commit, this code is a bit cheesy in that it disables the debug mode and error handling in slack_send_message, (since the prior implementation of that depended on having a current Flask app, but now that we are calling this function from a thread we don't have a current app context). --- diff --git a/turbot/slack.py b/turbot/slack.py index 6218100..34fe44d 100644 --- a/turbot/slack.py +++ b/turbot/slack.py @@ -56,18 +56,7 @@ def slack_send_reply(request, text): slack_send_message(channel, text) def slack_send_message(channel, text): - """Send a Slack message to a specified channel. + """Send a Slack message to a specified channel.""" - Note: If flask is in debug mode, this function will just print the - text to stdout.""" - - app = current_app - - if (app.debug): - print("Sending message to channel '{}': {}".format(channel, text)) - return + slack_client.chat_postMessage(channel=channel, text=text) - try: - slack_client.chat_postMessage(channel=channel, text=text) - except SlackApiError as e: - app.logger.error("Slack API error: " + e.response["error"]) diff --git a/turbot/turbot.py b/turbot/turbot.py index a558d07..df7d51e 100755 --- a/turbot/turbot.py +++ b/turbot/turbot.py @@ -3,6 +3,7 @@ from flask import Flask from slackeventsapi import SlackEventAdapter import os +import threading from turbot.rot import rot_route from turbot.slack import slack_send_message from turbot.sheets import sheets_create @@ -15,12 +16,17 @@ slack_events = SlackEventAdapter(slack_signing_secret, "/slack/events", app) @slack_events.on("channel_created") def handle_channel_created(event_data): + def later(channel): + sheet_url = sheets_create(channel["name"]) + slack_send_message(channel["id"], + "Auto-created a sheet for this channel: {}" + .format(sheet_url)) + event = event_data["event"] channel = event["channel"] - sheet_url = sheets_create(channel["name"]) - slack_send_message(channel["id"], - "Auto-created a sheet for this channel: {}" - .format(sheet_url)) + thread = threading.Thread(target=later, kwargs={'channel': channel}) + thread.start() + return @slack_events.on("error") def handle_error(error):