]> git.cworth.org Git - turbot/blob - turbot/events.py
32d061ab1bb62da91655891beb9c626d7859d6c4
[turbot] / turbot / events.py
1 from turbot.blocks import (
2     section_block, text_block, button_block, actions_block
3 )
4
5 def hunt_block(hunt):
6     name = hunt['name']
7     channel_id = hunt['channel_id']
8
9     text = "{}: ".format(name)
10
11     if (channel_id.startswith("placeholder-")):
12         text += "[Slack channel is still being created. Please wait.]"
13     else:
14         text += "<#{}>".format(channel_id)
15
16     return section_block(text_block(text))
17
18 def home(turb, user_id, body):
19     """Returns a view to be published as the turbot home tab for user_id
20
21     The body argument is a dictionary as provided by the Slack request.
22     The return value is a dictionary suitable to be published to the
23     Slack views_publish API."""
24
25     # Behave cleanly if there is not hunts table at all yet.
26     try:
27         response = turb.db.Table("hunts").scan()
28         hunts = response['Items']
29     except:
30         hunts = []
31
32     return {
33         "type": "home",
34         "blocks": [
35             section_block(text_block("*Active hunts*")),
36             *[hunt_block(hunt) for hunt in hunts if hunt['active']],
37             actions_block(button_block("New hunt", "new_hunt"))
38         ]
39     }
40
41 def app_home_opened(turb, body):
42     user_id = body['event']['user']
43     view = home(turb, user_id, body)
44     turb.slack_client.views_publish(user_id=user_id, view=view)
45     return "OK"
46
47 events = {
48     "app_home_opened": app_home_opened
49 }