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