X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=turbot%2Fblocks.py;h=cdce64a2f99aa3b83aa230418e0338f4589e9211;hb=c6ce6535d35284aad823b9f325e54ddb67ea1f3e;hp=c037b03217f0ed117b823dc2141dfe03d05a6297;hpb=4b3efe6fde2dd926f5b54eb357ed732e9e04d9e0;p=turbot diff --git a/turbot/blocks.py b/turbot/blocks.py index c037b03..cdce64a 100644 --- a/turbot/blocks.py +++ b/turbot/blocks.py @@ -11,18 +11,60 @@ def text_block(body): } } -def section_block(block): - return { +def section_block(block, block_id=None): + + block = { "type": "section", **block } + if block_id: + block['block_id'] = block_id + + return block + def actions_block(*elements): return { "type": "actions", "elements": list(elements) } +def checkbox_block(label, text, name, checked=False): + + element = { + "type": "checkboxes", + "action_id": name, + "options": [ + { + "value": name, + "text": { + "type": "plain_text", + "text": text + } + } + ] + } + + if checked: + element["initial_options"] = [{ + "value": name, + "text": { + "type": "plain_text", + "text": text + } + }] + + return { + "type": "input", + "block_id": name, + "element": element, + "optional": True, + "label": { + "type": "plain_text", + "text": label + } + } + def button_block(label, name, extra=None): block = { @@ -48,48 +90,64 @@ def accessory_block(main, accessory): } } -def input_block(label, name, placeholder, optional=False): +def input_block(label, name, placeholder, initial_value=None, optional=False): + + element = { + "type": "plain_text_input", + "action_id": name, + "placeholder": { + "type": "plain_text", + "text": placeholder, + } + } + + if initial_value: + element["initial_value"] = initial_value + return { "type": "input", "block_id": name, "optional": optional, - "element": { - "type": "plain_text_input", - "action_id": name, - "placeholder": { - "type": "plain_text", - "text": placeholder, - } - }, + "element": element, "label": { "type": "plain_text", "text": label } } -def multi_select_block(label, name, placeholder, options, default=None): - return { - "type": "section", - "block_id": name, - "text": { - "type": "mrkdwn", - "text": "*{}*".format(label) +def multi_select_block(label, name, placeholder, options, + initial_options=None): + + multi_select = { + "action_id": name, + "type": "multi_static_select", + "placeholder": { + "type": "plain_text", + "text": placeholder }, - "accessory": { - "action_id": name, - "type": "multi_static_select", - "placeholder": { - "type": "plain_text", - "text": placeholder - }, - "options": [ - { - "text": { - "type": "plain_text", - "text": option - }, - "value": option - } for option in options - ] - } + "options": [ + { + "text": { + "type": "plain_text", + "text": option + }, + "value": option + } for option in options + ] } + + if initial_options: + multi_select["initial_options"] = [ + { + "text": { + "type": "plain_text", + "text": option + }, + "value": option + } for option in initial_options + ] + + return accessory_block( + section_block(text_block("*{}*".format(label)), block_id=name), + multi_select + )