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