From: Carl Worth Date: Mon, 12 Oct 2020 20:07:50 +0000 (-0700) Subject: turbot_lambda: Parse the command from the request X-Git-Url: https://git.cworth.org/git?p=turbot;a=commitdiff_plain;h=a377e260992b6ab2949d5dfe4b835caae4980e4f turbot_lambda: Parse the command from the request 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. --- diff --git a/turbot_lambda/turbot_lambda.py b/turbot_lambda/turbot_lambda.py index f3d28f2..d67ed0d 100644 --- a/turbot_lambda/turbot_lambda.py +++ b/turbot_lambda/turbot_lambda.py @@ -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) }