]> git.cworth.org Git - turbot/blob - turbot_lambda/turbot_lambda.py
d67ed0d2a2ead93900087eb331dae12935ee5aed
[turbot] / turbot_lambda / turbot_lambda.py
1 from urllib.parse import parse_qs
2 from turbot.rot import rot
3
4 def turbot_lambda(event, context):
5     """Top-level entry point for our lambda function.
6
7     This parses the request and arguments and farms out to supporting
8     functions to implement all supported slash commands."""
9
10     body = parse_qs(event['body'])
11     command = body['command'][0]
12     args = body['text'][0]
13
14     if (command == "/rotlambda" or command == "/rot"):
15         return rot_slash_command(args)
16
17     error = "Command {} not implemented.".format(command)
18
19     print("Error: {}".format(error))
20
21     return {
22         'statusCode': 404,
23         'body': error
24     }
25
26 def rot_slash_command(args):
27     """Implementation of the /rot command
28
29     The args string should be as follows:
30
31         [count|*] String to be rotated
32
33     That is, the first word of the string is an optional number (or
34     the character '*'). If this is a number it indicates an amount to
35     rotate each character in the string. If the count is '*' or is not
36     present, then the string will be rotated through all possible 25
37     values.
38
39     The result of the rotation is returned (with Slack formatting) in
40     the body of the response so that Slack will provide it as a reply
41     to the user who submitted the slash command."""
42
43     return {
44         'statusCode': 200,
45         'body': rot(args)
46     }