]> git.cworth.org Git - turbot/blob - turbot/actions.py
911367e2b63d1ff6a3d3d960c2357148d81b0265
[turbot] / turbot / actions.py
1 from turbot.blocks import input_block
2 import turbot.sheets
3 import json
4 import re
5
6 def new_hunt(turb, payload):
7     """Handler for the action of user pressing the new_hunt button"""
8
9     view = {
10         "type": "modal",
11         "private_metadata": "new_hunt",
12         "title": { "type": "plain_text", "text": "New Hunt" },
13         "submit": { "type": "plain_text", "text": "Create" },
14         "blocks": [
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",
20                         optional=True)
21         ],
22     }
23
24     result = turb.slack_client.views_open(trigger_id=payload['trigger_id'],
25                                           view=view)
26     if (result['ok']):
27         submission_handlers[result['view']['id']] = new_hunt_submission
28
29     return {
30         'statusCode': 200,
31         'body': 'OK'
32     }
33
34 def new_hunt_submission(turb, payload):
35     """Handler for the user submitting the new hunt modal
36
37     This is the modal view presented to the user by the new_hunt
38     function above."""
39
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']
44
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.")
48         return {
49             'statusCode': 200,
50             'headers': {
51                 "Content-Type": "application/json"
52             },
53             'body': json.dumps({
54                 "response_action": "errors",
55                 "errors": {
56                     "hunt_id": "Hunt ID can only contain letters, "
57                     + "numbers, hyphens and underscores"
58                 }
59             })
60         }
61
62     # Create a channel for the hunt
63     response = turb.slack_client.conversations_create(name=hunt_id)
64
65     if not response['ok']:
66         print("Error creating channel for hunt {}: {}"
67               .format(name, str(response)))
68         return {
69             'statusCode': 400
70         }
71
72     user_id = payload['user']['id']
73     channel_id = response['channel']['id']
74
75     # Create a sheet for the channel
76     sheet = turbot.sheets.sheets_create(turb, hunt_id)
77
78     # Insert the newly-created hunt into the database
79     turb.hunts_table = turb.db.Table("hunts")
80     turb.hunts_table.put_item(
81         Item={
82             'channel_id': channel_id,
83             "active": True,
84             "name": name,
85             "hunt_id": hunt_id,
86             "url": url,
87             "sheet_url": sheet['url']
88         }
89     )
90
91     # Invite the initiating user to the channel
92     turb.slack_client.conversations_invite(channel=channel_id, users=user_id)
93
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']))
98
99     return {
100         'statusCode': 200,
101     }
102
103 def view_submission(turb, payload):
104
105     """Handler for Slack interactive view submission
106
107     Specifically, those that have a payload type of 'view_submission'"""
108
109     view_id = payload['view']['private_metadata']
110
111     if view_id in submission_handlers:
112         return submission_handlers[view_id](turb, payload)
113
114     print("Error: Unknown view ID: {}".format(view_id))
115     return {
116         'statusCode': 400
117     }
118
119 actions = {
120     "button": {
121         "new_hunt": new_hunt
122     }
123 }
124
125 submission_handlers = {
126     "new_hunt": new_hunt_submission
127 }