def open_modal(trigger_id: str): try: 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-1", label=PlainTextObject(text="Input label"), element=ConversationSelectElement( action_id="a", default_to_current_conversation=True, )), InputBlock(block_id="b-id-2", label=PlainTextObject(text="Input label"), element=ConversationMultiSelectElement( action_id="a", max_selected_items=2, default_to_current_conversation=True, )), ]) api_response = client.views_open(trigger_id=trigger_id, view=view) return make_response("", 200) except SlackApiError as e: code = e.response["error"] return make_response(f"Failed to open a modal due to {code}", 200)
def open_modal(command: SlashCommandInteractiveEvent): title = PlainTextObject(text="ID, password 入力フォーム") color_input_blocks = [InputBlock(label=PlainTextObject(text="esc-id"), element=PlainTextInputElement(placeholder="a020....", action_id='esc_id'), block_id='esc_id'), InputBlock(label=PlainTextObject(text="Password"), element=PlainTextInputElement(placeholder="password", action_id='password'), block_id='password')] modal = views.View(type="modal", title=title, blocks=color_input_blocks, submit="Submit", callback_id='login') slack_bot_client.views_open(trigger_id=command.trigger_id, view=modal.to_dict())
def open_modal(command: SlashCommandInteractiveEvent): title = PlainTextObject(text="Color Survey") color_input_blocks = [ InputBlock(label=PlainTextObject(text="What is your favorite color?"), element=PlainTextInputElement(placeholder="Green")), InputBlock( label=PlainTextObject(text="Why is that your favorite color?"), element=PlainTextInputElement( placeholder="It reminds me of my childhood home"), optional=True) ] modal = views.View(type="modal", title=title, blocks=color_input_blocks, submit="Submit") slack_client.views_open(trigger_id=command.trigger_id, view=modal.to_dict())
def test_input_blocks_in_home_tab(self): modal_view = View( type="home", callback_id="home-tab-id", blocks=[ InputBlock(block_id="b-id", label=PlainTextObject(text="Input label"), element=PlainTextInputElement(action_id="a-id")), ]) with self.assertRaises(SlackObjectFormationError): modal_view.validate_json()
def test_invalid_type_value(self): modal_view = View( type="modallll", 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")), ]) with self.assertRaises(SlackObjectFormationError): modal_view.validate_json()
def slack_app(): if not signature_verifier.is_valid_request(request.get_data(), request.headers): return make_response("invalid request", 403) if "payload" in request.form: payload = json.loads(request.form["payload"]) if payload["type"] == "shortcut" \ and payload["callback_id"] == "open-modal-shortcut": # Open a new modal by a global shortcut try: 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") ) ] ) api_response = client.views_open( trigger_id=payload["trigger_id"], view=view, ) return make_response("", 200) except SlackApiError as e: code = e.response["error"] return make_response(f"Failed to open a modal due to {code}", 200) if payload["type"] == "view_submission" \ and payload["view"]["callback_id"] == "modal-id": # Handle a data submission request from the modal submitted_data = payload["view"]["state"]["values"] print(submitted_data) # {'b-id': {'a-id': {'type': 'plain_text_input', 'value': 'your input'}}} return make_response("", 200) return make_response("", 404)
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 test_document(self): blocks = [{ "type": "input", "element": { "type": "plain_text_input" }, "label": { "type": "plain_text", "text": "Label", "emoji": True } }, { "type": "input", "element": { "type": "plain_text_input", "multiline": True }, "label": { "type": "plain_text", "text": "Label", "emoji": True } }, { "type": "input", "element": { "type": "datepicker", "initial_date": "1990-04-28", "placeholder": { "type": "plain_text", "text": "Select a date", "emoji": True } }, "label": { "type": "plain_text", "text": "Label", "emoji": True } }, { "type": "input", "element": { "type": "channels_select", "placeholder": { "type": "plain_text", "text": "Select a channel", "emoji": True } }, "label": { "type": "plain_text", "text": "Label", "emoji": True } }, { "type": "input", "element": { "type": "multi_users_select", "placeholder": { "type": "plain_text", "text": "Select users", "emoji": True } }, "label": { "type": "plain_text", "text": "Label", "emoji": True } }, { "type": "input", "element": { "type": "checkboxes", "options": [{ "text": { "type": "plain_text", "text": "*this is plain_text text*", "emoji": True }, "value": "value-0" }, { "text": { "type": "plain_text", "text": "*this is plain_text text*", "emoji": True }, "value": "value-1" }, { "text": { "type": "plain_text", "text": "*this is plain_text text*", "emoji": True }, "value": "value-2" }] }, "label": { "type": "plain_text", "text": "Label", "emoji": True } }] for input in blocks: self.assertDictEqual(input, InputBlock(**input).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))