]> git.cworth.org Git - turbot/blobdiff - turbot/actions.py
Don't shove the local variable hunts_table into the turb object
[turbot] / turbot / actions.py
index daf0227bb870fb1635afc8eb2abe8bdfb0352d0d..00708b0e66f59c78d48b53836c9354708fbe34ba 100644 (file)
-def new_hunt(payload):
+from turbot.blocks import input_block
+import turbot.sheets
+import json
+import re
 
-    print("In new_hunt function")
+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