]> git.cworth.org Git - turbot/blob - turbot/blocks.py
Modify multi_select_block to use accessory_block, section_block, and text_block
[turbot] / turbot / blocks.py
1 def divider_block():
2     return {
3         "type": "divider"
4     }
5
6 def text_block(body):
7     return {
8         "text": {
9             "type": "mrkdwn",
10             "text": body
11         }
12     }
13
14 def section_block(block, block_id=None):
15
16     block = {
17         "type": "section",
18         **block
19     }
20
21     if block_id:
22         block['block_id'] = block_id
23
24     return block
25
26 def actions_block(*elements):
27     return {
28         "type": "actions",
29         "elements": list(elements)
30     }
31
32 def button_block(label, name, extra=None):
33
34     block = {
35         "type": "button",
36         "text": {
37             "type": "plain_text",
38             "text": label,
39             "emoji": True
40         },
41         "value": name
42     }
43
44     if extra:
45         block['action_id'] = extra
46
47     return block
48
49 def accessory_block(main, accessory):
50     return {
51         **main,
52         "accessory": {
53             **accessory
54         }
55     }
56
57 def input_block(label, name, placeholder, optional=False):
58     return {
59         "type": "input",
60         "block_id": name,
61         "optional": optional,
62         "element": {
63             "type": "plain_text_input",
64             "action_id": name,
65             "placeholder": {
66                 "type": "plain_text",
67                 "text": placeholder,
68             }
69         },
70         "label": {
71             "type": "plain_text",
72             "text": label
73         }
74     }
75
76 def multi_select_block(label, name, placeholder, options, default=None):
77
78     multi_select =  {
79         "action_id": name,
80         "type": "multi_static_select",
81         "placeholder": {
82             "type": "plain_text",
83             "text": placeholder
84         },
85         "options": [
86             {
87                 "text": {
88                     "type": "plain_text",
89                     "text": option
90                 },
91                 "value": option
92             } for option in options
93         ]
94     }
95
96     return accessory_block(
97         section_block(text_block("*{}*".format(label)), block_id=name),
98         multi_select
99     )