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"""
"submit": { "type": "plain_text", "text": "Create" },
"blocks": [
input_block("Hunt name", "name", "Name of the hunt"),
- input_block("Hunt ID", "slug", "Short hunt prefix (no spaces)"),
+ 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)
],
state = payload['view']['state']['values']
name = state['name']['name']['value']
- slug = state['slug']['slug']['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=slug)
+ response = turb.slack_client.conversations_create(name=hunt_id)
if not response['ok']:
print("Error creating channel for hunt {}: {}"
channel_id = response['channel']['id']
# Create a sheet for the channel
- sheet = turbot.sheets.sheets_create(turb, slug)
+ sheet = turbot.sheets.sheets_create(turb, hunt_id)
# Insert the newly-created hunt into the database
- turb.hunts_table = turb.db.Table("hunts")
- turb.hunts_table.put_item(
+ hunts_table = turb.db.Table("hunts")
+ hunts_table.put_item(
Item={
'channel_id': channel_id,
"active": True,
"name": name,
- "slug": slug,
+ "hunt_id": hunt_id,
"url": url,
"sheet_url": sheet['url']
}
"new_hunt": new_hunt
}
}
-
-submission_handlers = {
- "new_hunt": new_hunt_submission
-}