]> git.cworth.org Git - turbot/blob - turbot/actions.py
Gracefully handle placeholder text for hunt channel ID
[turbot] / turbot / actions.py
1 from turbot.blocks import input_block
2 import uuid
3
4 def new_hunt(turb, payload):
5     """Handler for the action of user pressing the new_hunt button"""
6
7     view = {
8         "type": "modal",
9         "private_metadata": "new_hunt",
10         "title": { "type": "plain_text", "text": "New Hunt" },
11         "submit": { "type": "plain_text", "text": "Create" },
12         "blocks": [
13             input_block("Hunt name", "name", "Name of the hunt"),
14             input_block("Hunt ID", "slug", "Short prefix for hunt (no spaces)"),
15             input_block("Hunt URL", "url", "External URL of hunt")
16         ],
17     }
18
19     result = turb.slack_client.views_open(trigger_id=payload['trigger_id'],
20                                           view=view)
21     if (result['ok']):
22         submission_handlers[result['view']['id']] = new_hunt_submission
23
24     return {
25         'statusCode': 200,
26         'body': 'OK'
27     }
28
29 def new_hunt_submission(turb, payload):
30     """Handler for the user submitting the new hunt modal
31
32     This is the modal view presented to the user by the new_hunt
33     function above."""
34
35     state = payload['view']['state']['values']
36     name = state['name']['name']['value']
37     slug = state['slug']['slug']['value']
38     url = state['url']['url']['value']
39
40     table = turb.db.Table("hunts")
41     table.put_item(
42         Item={
43             'channel_id': "placeholder-" + str(uuid.uuid4()),
44             "active": True,
45             "name": name,
46             "slug": slug,
47             "url": url
48         }
49     )
50
51     return {
52         'statusCode': 200,
53     }
54
55 def view_submission(turb, payload):
56
57     """Handler for Slack interactive view submission
58
59     Specifically, those that have a payload type of 'view_submission'"""
60
61     view_id = payload['view']['private_metadata']
62
63     if view_id in submission_handlers:
64         return submission_handlers[view_id](turb, payload)
65
66     print("Error: Unknown view ID: {}".format(view_id))
67     return {
68         'statusCode': 400
69     }
70
71 actions = {
72     "button": {
73         "new_hunt": new_hunt
74     }
75 }
76
77 submission_handlers = {
78     "new_hunt": new_hunt_submission
79 }