]> git.cworth.org Git - turbot/commitdiff
turbot_lambda: Parse the command from the request
authorCarl Worth <cworth@cworth.org>
Mon, 12 Oct 2020 20:07:50 +0000 (13:07 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 12 Oct 2020 20:21:16 +0000 (13:21 -0700)
This will allow us to implement multiple slash commands within a single
Lambda function. Currently just the /rot (also its /rotlambda alias)
command is implemented, but we've got all the structure in place to do
more as necessary.

turbot_lambda/turbot_lambda.py

index f3d28f25f7abb6a0afde786d246c0c1ff29ee93c..d67ed0d2a2ead93900087eb331dae12935ee5aed 100644 (file)
@@ -4,13 +4,43 @@ from turbot.rot import rot
 def turbot_lambda(event, context):
     """Top-level entry point for our lambda function.
 
-    Currently only calls into the rot() function but may become more
-    sophisticated later on."""
+    This parses the request and arguments and farms out to supporting
+    functions to implement all supported slash commands."""
 
     body = parse_qs(event['body'])
-    result = rot(body['text'][0])
+    command = body['command'][0]
+    args = body['text'][0]
+
+    if (command == "/rotlambda" or command == "/rot"):
+        return rot_slash_command(args)
+
+    error = "Command {} not implemented.".format(command)
+
+    print("Error: {}".format(error))
+
+    return {
+        'statusCode': 404,
+        'body': error
+    }
+
+def rot_slash_command(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."""
 
     return {
         'statusCode': 200,
-        'body': result
+        'body': rot(args)
     }