]> git.cworth.org Git - turbot/blob - turbot/actions.py
b586d39048dab8942429d79bf3e5a6035b034f5c
[turbot] / turbot / actions.py
1 from turbot.blocks import input_block
2 import uuid
3 import turbot.sheets
4
5 def new_hunt(turb, payload):
6     """Handler for the action of user pressing the new_hunt button"""
7
8     view = {
9         "type": "modal",
10         "private_metadata": "new_hunt",
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     # Create a channel for the hunt
42     response = turb.slack_client.conversations_create(name=slug)
43
44     if not response['ok']:
45         print("Error creating channel for hunt {}: {}"
46               .format(name, str(response)))
47         return {
48             'statusCode': 400
49         }
50
51     user_id = payload['user']['id']
52     channel_id = response['channel']['id']
53
54     # Create a sheet for the channel
55     sheet = turbot.sheets.sheets_create(turb, slug)
56
57     # Insert the newly-created hunt into the database
58     turb.hunts_table = turb.db.Table("hunts")
59     turb.hunts_table.put_item(
60         Item={
61             'channel_id': channel_id,
62             "active": True,
63             "name": name,
64             "slug": slug,
65             "url": url
66         }
67     )
68
69     # Invite the initiating user to the channel
70     turb.slack_client.conversations_invite(channel=channel_id, users=user_id)
71
72     return {
73         'statusCode': 200,
74     }
75
76 def view_submission(turb, payload):
77
78     """Handler for Slack interactive view submission
79
80     Specifically, those that have a payload type of 'view_submission'"""
81
82     view_id = payload['view']['private_metadata']
83
84     if view_id in submission_handlers:
85         return submission_handlers[view_id](turb, payload)
86
87     print("Error: Unknown view ID: {}".format(view_id))
88     return {
89         'statusCode': 400
90     }
91
92 actions = {
93     "button": {
94         "new_hunt": new_hunt
95     }
96 }
97
98 submission_handlers = {
99     "new_hunt": new_hunt_submission
100 }