]> git.cworth.org Git - turbot/commitdiff
Use a thread for creating the Google Sheet when a new channel is created
authorCarl Worth <cworth@cworth.org>
Fri, 9 Oct 2020 22:41:11 +0000 (15:41 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 9 Oct 2020 22:47:44 +0000 (15:47 -0700)
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).

turbot/slack.py
turbot/turbot.py

index 62181005bc301aafe458cdb6ce560af19b9d877c..34fe44d0a6cf543ff89a02763747d358b5cd9047 100644 (file)
@@ -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"])
index a558d0789603a3eee0efd04db18cf52e68500bac..df7d51e0195cce4c3ed097816db5aac4864b5d08 100755 (executable)
@@ -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):