]> git.cworth.org Git - turbot/commitdiff
Move rot function down into turbot/commands.py
authorCarl Worth <cworth@cworth.org>
Tue, 13 Oct 2020 23:31:35 +0000 (16:31 -0700)
committerCarl Worth <cworth@cworth.org>
Tue, 13 Oct 2020 23:31:35 +0000 (16:31 -0700)
Completing the bulk of the code movement out of turbot_lambda.py

Now, the code in turbot_lambda is really just about handling the
request and figuring out which type it is, (whether a Slack action,
command, event, etc.), and calling out to the appropriate code in
turbot.actions, turbot.commands, turbot.events, etc.

turbot/commands.py [new file with mode: 0644]
turbot_lambda/turbot_lambda.py

diff --git a/turbot/commands.py b/turbot/commands.py
new file mode 100644 (file)
index 0000000..2a1f3e5
--- /dev/null
@@ -0,0 +1,37 @@
+import requests
+import turbot.rot
+
+def rot(slack_client, body, args):
+    """Implementation of the /rot command
+
+    The args string should be as follows:
+
+        [count|*] String to be rotated
+
+    That is, the first word of the string is an optional number (or
+    the character '*'). If this is a number it indicates an amount to
+    rotate each character in the string. If the count is '*' or is not
+    present, then the string will be rotated through all possible 25
+    values.
+
+    The result of the rotation is returned (with Slack formatting) in
+    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 = turbot.rot.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': ""
+    }
index d3db66f8f8b587f7cee6fba99143dcd7febb6c83..be8e099b5b2c25de64d052e77d8f9a6fa80ff739 100644 (file)
@@ -4,10 +4,11 @@ import boto3
 import requests
 import json
 import os
-from turbot.rot import rot
-import turbot.views
+
 import turbot.actions
+import turbot.commands
 import turbot.events
+import turbot.views
 
 ssm = boto3.client('ssm')
 
@@ -163,41 +164,6 @@ def turbot_slash_command(body):
     args = body['text'][0]
 
     if (command == "/rotlambda" or command == "/rot"):
-        return rot_slash_command(body, args)
+        return turbot.commands.rot(slack_client, body, args)
 
     return error("Command {} not implemented".format(command))
-
-def rot_slash_command(body, args):
-    """Implementation of the /rot command
-
-    The args string should be as follows:
-
-        [count|*] String to be rotated
-
-    That is, the first word of the string is an optional number (or
-    the character '*'). If this is a number it indicates an amount to
-    rotate each character in the string. If the count is '*' or is not
-    present, then the string will be rotated through all possible 25
-    values.
-
-    The result of the rotation is returned (with Slack formatting) in
-    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': ""
-    }