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