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", "slug", "Short hunt ID (no spaces)"),
17 input_block("Hunt URL", "url", "External URL of hunt",
22 result = turb.slack_client.views_open(trigger_id=payload['trigger_id'],
25 submission_handlers[result['view']['id']] = new_hunt_submission
32 def new_hunt_submission(turb, payload):
33 """Handler for the user submitting the new hunt modal
35 This is the modal view presented to the user by the new_hunt
38 state = payload['view']['state']['values']
39 name = state['name']['name']['value']
40 slug = state['slug']['slug']['value']
41 url = state['url']['url']['value']
43 # Validate that the slug contains no invalid characters
44 if not re.match(r'[-_a-zA-Z0-9]+$', slug):
45 print("Slug field is invalid. Attmpting to return a clean error.")
49 "Content-Type": "application/json"
52 "response_action": "errors",
54 "slug": "Hunt ID can only contain letters, "
55 + "numbers, hyphens and underscores"
60 # Create a channel for the hunt
61 response = turb.slack_client.conversations_create(name=slug)
63 if not response['ok']:
64 print("Error creating channel for hunt {}: {}"
65 .format(name, str(response)))
70 user_id = payload['user']['id']
71 channel_id = response['channel']['id']
73 # Create a sheet for the channel
74 sheet = turbot.sheets.sheets_create(turb, slug)
76 # Insert the newly-created hunt into the database
77 turb.hunts_table = turb.db.Table("hunts")
78 turb.hunts_table.put_item(
80 'channel_id': channel_id,
85 "sheet_url": sheet['url']
89 # Invite the initiating user to the channel
90 turb.slack_client.conversations_invite(channel=channel_id, users=user_id)
92 # Message the channel with the URL of the sheet
93 turb.slack_client.chat_postMessage(channel=channel_id,
94 text="Sheet created for this hunt: {}"
95 .format(sheet['url']))
101 def view_submission(turb, payload):
103 """Handler for Slack interactive view submission
105 Specifically, those that have a payload type of 'view_submission'"""
107 view_id = payload['view']['private_metadata']
109 if view_id in submission_handlers:
110 return submission_handlers[view_id](turb, payload)
112 print("Error: Unknown view ID: {}".format(view_id))
123 submission_handlers = {
124 "new_hunt": new_hunt_submission