def test_document(self): input = { "type": "checkboxes", "action_id": "this_is_an_action_id", "initial_options": [{ "value": "A1", "text": { "type": "plain_text", "text": "Checkbox 1" } }], "options": [ { "value": "A1", "text": { "type": "plain_text", "text": "Checkbox 1" } }, { "value": "A2", "text": { "type": "plain_text", "text": "Checkbox 2" } }, ], } self.assertDictEqual(input, CheckboxesElement(**input).to_dict())
def test_valid_construction(self): modal_view = View( type="modal", callback_id="modal-id", title=PlainTextObject(text="Awesome Modal"), submit=PlainTextObject(text="Submit"), close=PlainTextObject(text="Cancel"), blocks=[ InputBlock( block_id="b-id", label=PlainTextObject(text="Input label"), element=PlainTextInputElement(action_id="a-id"), ), InputBlock( block_id="cb-id", label=PlainTextObject(text="Label"), element=CheckboxesElement( action_id="a-cb-id", options=[ Option( text=PlainTextObject( text="*this is plain_text text*"), value="v1", ), Option( text=MarkdownTextObject( text="*this is mrkdwn text*"), value="v2", ), ], ), ), SectionBlock( block_id="sb-id", text=MarkdownTextObject( text="This is a mrkdwn text section block."), fields=[ PlainTextObject(text="*this is plain_text text*", emoji=True), MarkdownTextObject(text="*this is mrkdwn text*"), PlainTextObject(text="*this is plain_text text*", emoji=True), ], ), DividerBlock(), SectionBlock( block_id="rb-id", text=MarkdownTextObject( text= "This is a section block with radio button accessory"), accessory=RadioButtonsElement( initial_option=Option( text=PlainTextObject(text="Option 1"), value="option 1", description=PlainTextObject( text="Description for option 1"), ), options=[ Option( text=PlainTextObject(text="Option 1"), value="option 1", description=PlainTextObject( text="Description for option 1"), ), Option( text=PlainTextObject(text="Option 2"), value="option 2", description=PlainTextObject( text="Description for option 2"), ), ], ), ), ], ) modal_view.validate_json()
def main(rqst): app: SlackApp = rqst.app params = session[SESSION_KEY]['params'] # define the event ID for when the User clicks the Submit button on the # Modal. bind that event to the code handler that will process the data. event_id = cmd.prog + ".view1" app.ic.view.on(event_id, on_main_modal_submit) priv_data = { 'name': 'Jeremy', 'state': "NC" } # create a Modal instace, which will also defined a View when one is not # provided. Tie the submit callback ID to the envent_id value modal = Modal(rqst) view = modal.view = View( type="modal", title=PlainTextObject(text="Awesome Modal"), callback_id=event_id, close=PlainTextObject(text="Cancel"), submit=PlainTextObject(text="Next"), private_metadata=str(priv_data)) # ------------------------------------------------------------------------- # Create a button block: # Each time the User clicks it a counter will be incremented by 1. # The button click count is stored in the session params. # ------------------------------------------------------------------------- button1 = view.add_block(SectionBlock( text=PlainTextObject(text="It's Block Kit...but _in a modal_"), block_id=event_id + ".button1")) button1.accessory = ButtonElement( text='Click me', value='0', action_id=button1.block_id, style='danger' ) params['clicks'] = 0 # noinspection PyUnusedLocal @app.ic.block_action.on(button1.block_id) def remember_button(btn_rqst: BlockActionRequest): session[SESSION_KEY]['params']['clicks'] += 1 # ------------------------------------------------------------------------- # Create a Checkboxes block: # When the User checks/unchecks the items, they are stored to the session. # ------------------------------------------------------------------------- checkbox_options = [ Option(label='Box 1', value='A1'), Option(label='Box 2', value='B2') ] params['checkboxes'] = checkbox_options[0].value checkbox = view.add_block(SectionBlock( text=PlainTextObject(text='Nifty checkboxes'), block_id=event_id + ".checkbox")) checkbox.accessory = CheckboxesElement( action_id=checkbox.block_id, options=checkbox_options, initial_options=[checkbox_options[0]] ) @app.ic.block_action.on(checkbox.block_id) def remember_check(cb_rqst: BlockActionRequest, action: ActionEvent): session[SESSION_KEY]['params']['checkboxes'] = action.value # ------------------------------------------------------------------------- # Create an Input block: # Required single line of text. # ------------------------------------------------------------------------- view.add_block(InputBlock( label=PlainTextObject(text='First input'), element=PlainTextInputElement( action_id=event_id + ".text1", placeholder='Type in here' ) )) # ------------------------------------------------------------------------- # Create an Input block: # Optional multi-line text area, maximum 500 characters. # ------------------------------------------------------------------------- host_selector = view.add_block(InputBlock( label=PlainTextObject(text='Next input selector ... start typing'), optional=True, block_id=event_id + ".ext1", element=ExternalDataSelectElement( placeholder='hosts ..', action_id=event_id + ".ext1",) )) @app.ic.select.on(host_selector.element.action_id) def select_host_from_dynamic_list(_rqst): return { 'options': extract_json([ Option(label=val, value=val) for val in ('lx5e1234', 'lx5w1234', 'lx5e4552') ]) } # ------------------------------------------------------------------------- # Create an Input Datepicker block # ------------------------------------------------------------------------- view.add_block(InputBlock( label=PlainTextObject(text="Pick a date"), element=DatePickerElement( action_id=event_id + ".datepicker", placeholder='A date' ) )) # ------------------------------------------------------------------------- # Create an Input to select from static list, optional. # ------------------------------------------------------------------------- view.add_block(InputBlock( label=PlainTextObject(text="Select one option"), optional=True, element=SelectElement( placeholder='Select one of ...', action_id=event_id + ".select_1", options=[ Option(label='this', value='this'), Option(label='that', value='that') ] ) )) # ------------------------------------------------------------------------- # Create an Input to allow the User to select multiple items # from a static list. # ------------------------------------------------------------------------- view.add_block(InputBlock( label=PlainTextObject(text="Select many option"), element=StaticMultiSelectElement( placeholder=PlainTextObject(text='Select any of ...'), action_id=event_id + ".select_N", options=[ Option(label='cat', value='cat'), Option(label='dog', value='dog'), Option(label='monkey', value='monkey') ] ) )) res = modal.open(callback=on_main_modal_submit) if not res.get('ok'): app.log.error(json.dumps(res, indent=3))