1 from turbot.blocks import input_block
6 def new_hunt(turb, payload):
7 """Handler for the action of user pressing the new_hunt button"""
11 "private_metadata": "new_hunt",
12 "title": { "type": "plain_text", "text": "New Hunt" },
13 "submit": { "type": "plain_text", "text": "Create" },
15 input_block("Hunt name", "name", "Name of the hunt"),
16 input_block("Hunt ID", "hunt_id",
17 "Used as puzzle channel prefix "
18 + "(no spaces nor punctuation)"),
19 input_block("Hunt URL", "url", "External URL of hunt",
24 result = turb.slack_client.views_open(trigger_id=payload['trigger_id'],
27 submission_handlers[result['view']['id']] = new_hunt_submission
34 def new_hunt_submission(turb, payload):
35 """Handler for the user submitting the new hunt modal
37 This is the modal view presented to the user by the new_hunt
40 state = payload['view']['state']['values']
41 name = state['name']['name']['value']
42 hunt_id = state['hunt_id']['hunt_id']['value']
43 url = state['url']['url']['value']
45 # Validate that the hunt_id contains no invalid characters
46 if not re.match(r'[-_a-zA-Z0-9]+$', hunt_id):
47 print("Hunt ID field is invalid. Attmpting to return a clean error.")
51 "Content-Type": "application/json"
54 "response_action": "errors",
56 "hunt_id": "Hunt ID can only contain letters, "
57 + "numbers, hyphens and underscores"
62 # Create a channel for the hunt
63 response = turb.slack_client.conversations_create(name=hunt_id)
65 if not response['ok']:
66 print("Error creating channel for hunt {}: {}"
67 .format(name, str(response)))
72 user_id = payload['user']['id']
73 channel_id = response['channel']['id']
75 # Create a sheet for the channel
76 sheet = turbot.sheets.sheets_create(turb, hunt_id)
78 # Insert the newly-created hunt into the database
79 turb.hunts_table = turb.db.Table("hunts")
80 turb.hunts_table.put_item(
82 'channel_id': channel_id,
87 "sheet_url": sheet['url']
91 # Invite the initiating user to the channel
92 turb.slack_client.conversations_invite(channel=channel_id, users=user_id)
94 # Message the channel with the URL of the sheet
95 turb.slack_client.chat_postMessage(channel=channel_id,
96 text="Sheet created for this hunt: {}"
97 .format(sheet['url']))
103 def view_submission(turb, payload):
105 """Handler for Slack interactive view submission
107 Specifically, those that have a payload type of 'view_submission'"""
109 view_id = payload['view']['private_metadata']
111 if view_id in submission_handlers:
112 return submission_handlers[view_id](turb, payload)
114 print("Error: Unknown view ID: {}".format(view_id))
125 submission_handlers = {
126 "new_hunt": new_hunt_submission