1 from turbot.blocks import input_block
4 def new_hunt(turb, payload):
5 """Handler for the action of user pressing the new_hunt button"""
9 "private_metadata": "new_hunt",
10 "title": { "type": "plain_text", "text": "New Hunt" },
11 "submit": { "type": "plain_text", "text": "Create" },
13 input_block("Hunt name", "name", "Name of the hunt"),
14 input_block("Hunt ID", "slug", "Short hunt prefix (no spaces)"),
15 input_block("Hunt URL", "url", "External URL of hunt",
20 result = turb.slack_client.views_open(trigger_id=payload['trigger_id'],
23 submission_handlers[result['view']['id']] = new_hunt_submission
30 def new_hunt_submission(turb, payload):
31 """Handler for the user submitting the new hunt modal
33 This is the modal view presented to the user by the new_hunt
36 state = payload['view']['state']['values']
37 name = state['name']['name']['value']
38 slug = state['slug']['slug']['value']
39 url = state['url']['url']['value']
41 # Create a channel for the hunt
42 response = turb.slack_client.conversations_create(name=slug)
44 if not response['ok']:
45 print("Error creating channel for hunt {}: {}"
46 .format(name, str(response)))
51 user_id = payload['user']['id']
52 channel_id = response['channel']['id']
54 # Create a sheet for the channel
55 sheet = turbot.sheets.sheets_create(turb, slug)
57 # Insert the newly-created hunt into the database
58 turb.hunts_table = turb.db.Table("hunts")
59 turb.hunts_table.put_item(
61 'channel_id': channel_id,
66 "sheet_url": sheet['url']
70 # Invite the initiating user to the channel
71 turb.slack_client.conversations_invite(channel=channel_id, users=user_id)
73 # Message the channel with the URL of the sheet
74 turb.slack_client.chat_postMessage(channel=channel_id,
75 text="Sheet created for this hunt: {}"
76 .format(sheet['url']))
82 def view_submission(turb, payload):
84 """Handler for Slack interactive view submission
86 Specifically, those that have a payload type of 'view_submission'"""
88 view_id = payload['view']['private_metadata']
90 if view_id in submission_handlers:
91 return submission_handlers[view_id](turb, payload)
93 print("Error: Unknown view ID: {}".format(view_id))
104 submission_handlers = {
105 "new_hunt": new_hunt_submission