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