]> git.cworth.org Git - turbot/blob - turbot/blocks.py
4949420ec5210a4b60d634db1733db6b6f62f254
[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 checkbox_block(label, text, name, checked=False):
33
34     element = {
35         "type": "checkboxes",
36         "options": [
37             {
38                 "value": name,
39                 "text": {
40                     "type": "plain_text",
41                     "text": text
42                 }
43             }
44         ]
45     }
46
47     if checked:
48         element["initial_options"] = [{
49             "value": name,
50             "text": {
51                 "type": "plain_text",
52                 "text": text
53             }
54         }]
55
56     return {
57         "type": "input",
58         "block_id": name,
59         "element": element,
60         "optional": True,
61         "label": {
62             "type": "plain_text",
63             "text": label
64         }
65     }
66
67 def button_block(label, name, extra=None):
68
69     block = {
70         "type": "button",
71         "text": {
72             "type": "plain_text",
73             "text": label,
74             "emoji": True
75         },
76         "value": name
77     }
78
79     if extra:
80         block['action_id'] = extra
81
82     return block
83
84 def accessory_block(main, accessory):
85     return {
86         **main,
87         "accessory": {
88             **accessory
89         }
90     }
91
92 def input_block(label, name, placeholder, initial_value=None, optional=False):
93
94     element = {
95         "type": "plain_text_input",
96         "action_id": name,
97         "placeholder": {
98             "type": "plain_text",
99             "text": placeholder,
100         }
101     }
102
103     if initial_value:
104         element["initial_value"] = initial_value
105
106     return {
107         "type": "input",
108         "block_id": name,
109         "optional": optional,
110         "element": element,
111         "label": {
112             "type": "plain_text",
113             "text": label
114         }
115     }
116
117 def multi_select_block(label, name, placeholder, options,
118                        initial_options=None):
119
120     multi_select = {
121         "action_id": name,
122         "type": "multi_static_select",
123         "placeholder": {
124             "type": "plain_text",
125             "text": placeholder
126         },
127         "options": [
128             {
129                 "text": {
130                     "type": "plain_text",
131                     "text": option
132                 },
133                 "value": option
134             } for option in options
135         ]
136     }
137
138     if initial_options:
139         multi_select["initial_options"] = [
140             {
141                 "text": {
142                     "type": "plain_text",
143                     "text": option
144                 },
145                 "value": option
146             } for option in initial_options
147         ]
148
149     return accessory_block(
150         section_block(text_block("*{}*".format(label)), block_id=name),
151         multi_select
152     )