]> git.cworth.org Git - turbot/commitdiff
Combine actions.py and commands.py into interaction.py
authorCarl Worth <cworth@cworth.org>
Wed, 21 Oct 2020 23:00:20 +0000 (16:00 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 21 Oct 2020 23:00:20 +0000 (16:00 -0700)
We're about to create a new slash command that fires up a modal, and
the response to that modal will be an action. So we want the
implementation of that slash command (which creates the view for the
modal) to be in the same file as the handler for the action.

So we combine these together for now.

If this file become unwieldy later, we can re-separate things based on
functional groups, (interactions related to hunts, puzzles, word-play
utilities, etc.), but always keeping the view creators and the
view-submission handlers together for any given view.

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

diff --git a/turbot/actions.py b/turbot/actions.py
deleted file mode 100644 (file)
index 00708b0..0000000
+++ /dev/null
@@ -1,123 +0,0 @@
-from turbot.blocks import input_block
-import turbot.sheets
-import json
-import re
-
-def new_hunt(turb, payload):
-    """Handler for the action of user pressing the new_hunt button"""
-
-    view = {
-        "type": "modal",
-        "private_metadata": "new_hunt",
-        "title": { "type": "plain_text", "text": "New Hunt" },
-        "submit": { "type": "plain_text", "text": "Create" },
-        "blocks": [
-            input_block("Hunt name", "name", "Name of the hunt"),
-            input_block("Hunt ID", "hunt_id",
-                        "Used as puzzle channel prefix "
-                        + "(no spaces nor punctuation)"),
-            input_block("Hunt URL", "url", "External URL of hunt",
-                        optional=True)
-        ],
-    }
-
-    result = turb.slack_client.views_open(trigger_id=payload['trigger_id'],
-                                          view=view)
-    if (result['ok']):
-        submission_handlers[result['view']['id']] = new_hunt_submission
-
-    return {
-        'statusCode': 200,
-        'body': 'OK'
-    }
-
-def new_hunt_submission(turb, payload):
-    """Handler for the user submitting the new hunt modal
-
-    This is the modal view presented to the user by the new_hunt
-    function above."""
-
-    state = payload['view']['state']['values']
-    name = state['name']['name']['value']
-    hunt_id = state['hunt_id']['hunt_id']['value']
-    url = state['url']['url']['value']
-
-    # Validate that the hunt_id contains no invalid characters
-    if not re.match(r'[_a-zA-Z0-9]+$', hunt_id):
-        print("Hunt ID field is invalid. Attmpting to return a clean error.")
-        return {
-            'statusCode': 200,
-            'headers': {
-                "Content-Type": "application/json"
-            },
-            'body': json.dumps({
-                "response_action": "errors",
-                "errors": {
-                    "hunt_id": "Hunt ID can only contain letters, "
-                    + "numbers, and underscores"
-                }
-            })
-        }
-
-    # Create a channel for the hunt
-    response = turb.slack_client.conversations_create(name=hunt_id)
-
-    if not response['ok']:
-        print("Error creating channel for hunt {}: {}"
-              .format(name, str(response)))
-        return {
-            'statusCode': 400
-        }
-
-    user_id = payload['user']['id']
-    channel_id = response['channel']['id']
-
-    # Create a sheet for the channel
-    sheet = turbot.sheets.sheets_create(turb, hunt_id)
-
-    # Insert the newly-created hunt into the database
-    hunts_table = turb.db.Table("hunts")
-    hunts_table.put_item(
-        Item={
-            'channel_id': channel_id,
-            "active": True,
-            "name": name,
-            "hunt_id": hunt_id,
-            "url": url,
-            "sheet_url": sheet['url']
-        }
-    )
-
-    # Invite the initiating user to the channel
-    turb.slack_client.conversations_invite(channel=channel_id, users=user_id)
-
-    # Message the channel with the URL of the sheet
-    turb.slack_client.chat_postMessage(channel=channel_id,
-                                       text="Sheet created for this hunt: {}"
-                                       .format(sheet['url']))
-
-    return {
-        'statusCode': 200,
-    }
-
-def view_submission(turb, payload):
-
-    """Handler for Slack interactive view submission
-
-    Specifically, those that have a payload type of 'view_submission'"""
-
-    view_id = payload['view']['private_metadata']
-
-    if view_id in submission_handlers:
-        return submission_handlers[view_id](turb, payload)
-
-    print("Error: Unknown view ID: {}".format(view_id))
-    return {
-        'statusCode': 400
-    }
-
-actions = {
-    "button": {
-        "new_hunt": new_hunt
-    }
-}
diff --git a/turbot/commands.py b/turbot/commands.py
deleted file mode 100644 (file)
index 68f470a..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-import requests
-import turbot.rot
-
-def rot(turb, 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:
-        turb.slack_client.chat_postMessage(channel=channel_id, text=result)
-
-    return {
-        'statusCode': 200,
-        'body': ""
-    }
-
-commands = {
-    "/rot": rot
-}
diff --git a/turbot/interaction.py b/turbot/interaction.py
new file mode 100644 (file)
index 0000000..4606449
--- /dev/null
@@ -0,0 +1,163 @@
+from turbot.blocks import input_block
+import turbot.sheets
+import json
+import re
+import requests
+import turbot.rot
+
+def new_hunt(turb, payload):
+    """Handler for the action of user pressing the new_hunt button"""
+
+    view = {
+        "type": "modal",
+        "private_metadata": "new_hunt",
+        "title": { "type": "plain_text", "text": "New Hunt" },
+        "submit": { "type": "plain_text", "text": "Create" },
+        "blocks": [
+            input_block("Hunt name", "name", "Name of the hunt"),
+            input_block("Hunt ID", "hunt_id",
+                        "Used as puzzle channel prefix "
+                        + "(no spaces nor punctuation)"),
+            input_block("Hunt URL", "url", "External URL of hunt",
+                        optional=True)
+        ],
+    }
+
+    result = turb.slack_client.views_open(trigger_id=payload['trigger_id'],
+                                          view=view)
+    if (result['ok']):
+        submission_handlers[result['view']['id']] = new_hunt_submission
+
+    return {
+        'statusCode': 200,
+        'body': 'OK'
+    }
+
+def new_hunt_submission(turb, payload):
+    """Handler for the user submitting the new hunt modal
+
+    This is the modal view presented to the user by the new_hunt
+    function above."""
+
+    state = payload['view']['state']['values']
+    name = state['name']['name']['value']
+    hunt_id = state['hunt_id']['hunt_id']['value']
+    url = state['url']['url']['value']
+
+    # Validate that the hunt_id contains no invalid characters
+    if not re.match(r'[_a-zA-Z0-9]+$', hunt_id):
+        print("Hunt ID field is invalid. Attmpting to return a clean error.")
+        return {
+            'statusCode': 200,
+            'headers': {
+                "Content-Type": "application/json"
+            },
+            'body': json.dumps({
+                "response_action": "errors",
+                "errors": {
+                    "hunt_id": "Hunt ID can only contain letters, "
+                    + "numbers, and underscores"
+                }
+            })
+        }
+
+    # Create a channel for the hunt
+    response = turb.slack_client.conversations_create(name=hunt_id)
+
+    if not response['ok']:
+        print("Error creating channel for hunt {}: {}"
+              .format(name, str(response)))
+        return {
+            'statusCode': 400
+        }
+
+    user_id = payload['user']['id']
+    channel_id = response['channel']['id']
+
+    # Create a sheet for the channel
+    sheet = turbot.sheets.sheets_create(turb, hunt_id)
+
+    # Insert the newly-created hunt into the database
+    hunts_table = turb.db.Table("hunts")
+    hunts_table.put_item(
+        Item={
+            'channel_id': channel_id,
+            "active": True,
+            "name": name,
+            "hunt_id": hunt_id,
+            "url": url,
+            "sheet_url": sheet['url']
+        }
+    )
+
+    # Invite the initiating user to the channel
+    turb.slack_client.conversations_invite(channel=channel_id, users=user_id)
+
+    # Message the channel with the URL of the sheet
+    turb.slack_client.chat_postMessage(channel=channel_id,
+                                       text="Sheet created for this hunt: {}"
+                                       .format(sheet['url']))
+
+    return {
+        'statusCode': 200,
+    }
+
+def view_submission(turb, payload):
+    """Handler for Slack interactive view submission
+
+    Specifically, those that have a payload type of 'view_submission'"""
+
+    view_id = payload['view']['private_metadata']
+
+    if view_id in submission_handlers:
+        return submission_handlers[view_id](turb, payload)
+
+    print("Error: Unknown view ID: {}".format(view_id))
+    return {
+        'statusCode': 400
+    }
+
+def rot(turb, 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:
+        turb.slack_client.chat_postMessage(channel=channel_id, text=result)
+
+    return {
+        'statusCode': 200,
+        'body': ""
+    }
+
+actions = {
+    "button": {
+        "new_hunt": new_hunt
+    }
+}
+
+commands = {
+    "/rot": rot
+}
index 59312c8b2e1e19e7b573b7e108cc2458384cb286..8365739fa24e5e7881d282a79ddd4b0d47663ee1 100644 (file)
@@ -10,8 +10,7 @@ from types import SimpleNamespace
 from google.auth.transport.requests import Request
 from googleapiclient.discovery import build
 
-import turbot.actions
-import turbot.commands
+import turbot.interaction
 import turbot.events
 
 ssm = boto3.client('ssm')
@@ -166,7 +165,7 @@ def turbot_interactive(turb, payload):
     if type == 'block_actions':
         return turbot_block_action(turb, payload)
     if type == 'view_submission':
-        return turbot.actions.view_submission(turb, payload)
+        return turbot.interaction.view_submission(turb, payload)
     if type == 'shortcut':
         return turbot_shortcut(turb, payload);
     return error("Unrecognized interactive type: {}".format(type))
@@ -188,10 +187,10 @@ def turbot_block_action(turb, payload):
     avalue = action['value']
 
     if (
-            atype in turbot.actions.actions
-            and avalue in turbot.actions.actions[atype]
+            atype in turbot.interaction.actions
+            and avalue in turbot.interaction.actions[atype]
     ):
-        return turbot.actions.actions[atype][avalue](turb, payload)
+        return turbot.interaction.actions[atype][avalue](turb, payload)
     return error("Unknown action of type/value: {}/{}".format(atype, avalue))
 
 def turbot_shortcut(turb, payload):
@@ -216,7 +215,7 @@ def turbot_slash_command(turb, body):
     else:
         args = ''
 
-    if command in turbot.commands.commands:
-        return turbot.commands.commands[command](turb, body, args)
+    if command in turbot.interaction.commands:
+        return turbot.interation.commands[command](turb, body, args)
 
     return error("Command {} not implemented".format(command))