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