]> git.cworth.org Git - turbot/commitdiff
turbot_lambda: Inject /rot response into the same channel as the command
authorCarl Worth <cworth@cworth.org>
Mon, 12 Oct 2020 21:13:24 +0000 (14:13 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 12 Oct 2020 21:45:05 +0000 (14:45 -0700)
Prior to this commit, we were just including the rot result in the
response to the POST request. That was sufficient for Slack to take
the result and present it privately to the user that issued the
command. But that would not allow any other user in the same channel
to see the same result.

Here, instead, we use the Slack WebClient API to inject the result
into the user's channel as a new message. This takes advantage of a
new, encrypted SSM parameter SLACK_BOT_TOKEN to authenticate our
bot. It also requires bundling all of the virtualenv dependencies into
the zip file uploaded to AWS for the lambda, so that's what the
Makefile modifications do here.

Makefile
turbot_lambda/turbot_lambda.py

index 9c4f334e04b8f85b08b0889236ce647329ce6b41..f9c987d6076f0a359eca326412b6dc6002bfdbf7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -76,6 +76,9 @@ deploy-flask:
 deploy-lambda:
        rm -rf .deploy-lambda-source
        git clone . .deploy-lambda-source
+       make -C .deploy-lambda-source bootstrap
+       (cd .deploy-lambda-source; . env/bin/activate; make reqs)
+       (cd .deploy-lambda-source/env/lib/python3.8/site-packages; zip -r ../../../../turbot.zip .)
        (cd .deploy-lambda-source/turbot_lambda; zip ../turbot.zip turbot_lambda.py)
        (cd .deploy-lambda-source; zip turbot.zip $$(git ls-files -- turbot))
        (cd .deploy-lambda-source; \
index d67ed0d2a2ead93900087eb331dae12935ee5aed..f52ac4fa73ad257221cef0e7cf4a5260bace4c0d 100644 (file)
@@ -1,5 +1,13 @@
 from urllib.parse import parse_qs
 from turbot.rot import rot
+from slack import WebClient
+import boto3
+import requests
+
+ssm = boto3.client('ssm')
+response = ssm.get_parameter(Name='SLACK_BOT_TOKEN', WithDecryption=True)
+slack_bot_token = response['Parameter']['Value']
+slack_client = WebClient(slack_bot_token)
 
 def turbot_lambda(event, context):
     """Top-level entry point for our lambda function.
@@ -12,7 +20,7 @@ def turbot_lambda(event, context):
     args = body['text'][0]
 
     if (command == "/rotlambda" or command == "/rot"):
-        return rot_slash_command(args)
+        return rot_slash_command(body, args)
 
     error = "Command {} not implemented.".format(command)
 
@@ -23,7 +31,7 @@ def turbot_lambda(event, context):
         'body': error
     }
 
-def rot_slash_command(args):
+def rot_slash_command(body, args):
     """Implementation of the /rot command
 
     The args string should be as follows:
@@ -40,7 +48,20 @@ def rot_slash_command(args):
     the body of the response so that Slack will provide it as a reply
     to the user who submitted the slash command."""
 
+    channel_name = body['channel_name'][0]
+    response_url = body['response_url'][0]
+    channel_id = body['channel_id'][0]
+
+    result = rot(args)
+
+    if (channel_name == "directmessage"):
+        requests.post(response_url,
+                      json = {"text": result},
+                      headers = {"Content-type": "application/json"})
+    else:
+        slack_client.chat_postMessage(channel=channel_id, text=result)
+
     return {
         'statusCode': 200,
-        'body': rot(args)
+        'body': ""
     }