def test_document(self): input = { "type": "datepicker", "action_id": "datepicker123", "initial_date": "1990-04-28", "placeholder": { "type": "plain_text", "text": "Select a date" }, "confirm": { "title": { "type": "plain_text", "text": "Are you sure?" }, "text": { "type": "mrkdwn", "text": "Wouldn't you prefer a good game of _chess_?", }, "confirm": { "type": "plain_text", "text": "Do it" }, "deny": { "type": "plain_text", "text": "Stop, I've changed my mind!" }, }, } self.assertDictEqual(input, DatePickerElement(**input).to_dict())
def test_json(self): for month in range(1, 12): for day in range(1, 31): date = f"2020-{month:02}-{day:02}" self.assertDictEqual( { "action_id": "datepicker-action", "initial_date": date, "placeholder": { "emoji": True, "text": "Select a date", "type": "plain_text", }, "type": "datepicker", }, DatePickerElement( action_id="datepicker-action", placeholder="Select a date", initial_date=date, ).to_dict(), )
def test_issue_623(self): elem = DatePickerElement(action_id="1", placeholder=None) elem.to_dict() # no exception elem = DatePickerElement(action_id="1") elem.to_dict() # no exception with self.assertRaises(SlackObjectFormationError): elem = DatePickerElement(action_id="1", placeholder="12345" * 100) elem.to_dict()
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))