]> git.cworth.org Git - turbot/blobdiff - turbot_lambda/turbot_lambda.py
Give turbot/events.py the same dispatch-table treatment
[turbot] / turbot_lambda / turbot_lambda.py
index ece58d62f577ee3553926115e79f146adcd3dbd6..b6f4a36049360a4e154ac6bf1e6ca3217df8af69 100644 (file)
@@ -4,9 +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')
 
@@ -96,16 +98,10 @@ def url_verification_handler(body):
 def event_callback_handler(body):
     type = body['event']['type']
 
-    if type == 'app_home_opened':
-        return app_home_opened_handler(body)
+    if type in turbot.events.events:
+        return turbot.events.events[type](slack_client, body)
     return error("Unknown event type: {}".format(type))
 
-def app_home_opened_handler(body):
-    user_id = body['event']['user']
-    view = turbot.views.home(user_id, body)
-    slack_client.views_publish(user_id=user_id, view=view)
-    return "OK"
-
 def turbot_interactive_or_slash_command(event, context):
     """Handler for Slack interactive things (buttons, shortcuts, etc.)
     as well as slash commands.
@@ -153,8 +149,11 @@ def turbot_block_action(payload):
     atype = action['type']
     avalue = action['value']
 
-    if atype == 'button' and avalue == 'new_hunt':
-        return turbot.actions.new_hunt(payload)
+    if (
+            atype in turbot.actions.actions
+            and avalue in turbot.actions.actions[atype]
+    ):
+        return turbot.actions.actions[atype][avalue](payload)
     return error("Unknown action of type/value: {}/{}".format(atype, avalue))
 
 def turbot_slash_command(body):
@@ -167,42 +166,7 @@ def turbot_slash_command(body):
     command = body['command'][0]
     args = body['text'][0]
 
-    if (command == "/rotlambda" or command == "/rot"):
-        return rot_slash_command(body, args)
+    if command in turbot.commands.commands:
+        return turbot.commands.commands[command](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': ""
-    }