Ejemplo n.º 1
0
 def test_options_length(self):
     with self.assertRaises(SlackObjectFormationError):
         SelectElement(
             placeholder="select",
             action_id="selector",
             options=[self.option_one] * 101,
         ).to_dict()
Ejemplo n.º 2
0
    def test_json(self):
        self.maxDiff = None
        self.assertDictEqual(
            SelectElement(
                placeholder="selectedValue",
                action_id="dropdown",
                options=self.options,
                initial_option=self.option_two,
            ).to_dict(),
            {
                "placeholder": {
                    "emoji": True,
                    "text": "selectedValue",
                    "type": "plain_text",
                },
                "action_id": "dropdown",
                "options": [o.to_dict("block") for o in self.options],
                "initial_option": self.option_two.to_dict(),
                "type": "static_select",
            },
        )

        self.assertDictEqual(
            SelectElement(
                placeholder="selectedValue",
                action_id="dropdown",
                options=self.options,
                confirm=ConfirmObject(title="title", text="text"),
            ).to_dict(),
            {
                "placeholder": {
                    "emoji": True,
                    "text": "selectedValue",
                    "type": "plain_text",
                },
                "action_id":
                "dropdown",
                "options": [o.to_dict("block") for o in self.options],
                "confirm":
                ConfirmObject(title="title", text="text").to_dict("block"),
                "type":
                "static_select",
            },
        )
Ejemplo n.º 3
0
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))