def test_json(self): self.assertDictEqual( { "text": {"emoji": True, "text": "button text", "type": "plain_text"}, "action_id": "some_button", "value": "button_123", "type": "button", }, ButtonElement( text="button text", action_id="some_button", value="button_123" ).to_dict(), ) confirm = ConfirmObject(title="really?", text="are you sure?") self.assertDictEqual( { "text": {"emoji": True, "text": "button text", "type": "plain_text"}, "action_id": "some_button", "value": "button_123", "type": "button", "style": "primary", "confirm": confirm.to_dict(), }, ButtonElement( text="button text", action_id="some_button", value="button_123", style="primary", confirm=confirm, ).to_dict(), )
def test_home_tab_construction(self): home_tab_view = View( type="home", blocks=[ SectionBlock( text=MarkdownTextObject( text="*Here's what you can do with Project Tracker:*" ), ), ActionsBlock( elements=[ ButtonElement( text=PlainTextObject(text="Create New Task", emoji=True), style="primary", value="create_task", ), ButtonElement( text=PlainTextObject(text="Create New Project", emoji=True), value="create_project", ), ButtonElement( text=PlainTextObject(text="Help", emoji=True), value="help", ), ], ), ContextBlock( elements=[ ImageElement( image_url="https://api.slack.com/img/blocks/bkb_template_images/placeholder.png", alt_text="placeholder", ), ], ), SectionBlock( text=MarkdownTextObject(text="*Your Configurations*"), ), DividerBlock(), SectionBlock( text=MarkdownTextObject( text="*#public-relations*\n<fakelink.toUrl.com|PR Strategy 2019> posts new tasks, comments, and project updates to <fakelink.toChannel.com|#public-relations>" ), accessory=ButtonElement( text=PlainTextObject(text="Edit", emoji=True), value="public-relations", ), ), ], ) home_tab_view.validate_json()
def linked_labeled_button_block( url: str, label_text: str, button_text: str, value: str, action_id: str, ) -> dict[str, str]: """ Generate labeled button block. Linked labeled buttons have the link label text on the left and the button on the right. Args: url (str): URL for label. label_text (str): Label text to display. button_text (str): Button text to display. value (str): Hidden value to assign to the button. action_id (str): Action identifier to assign to the button. Returns: dict: View block with desired button. """ return SectionBlock( text=MarkdownTextObject.from_link(link=Link( url=url, text=label_text, )), accessory=ButtonElement( text=button_text, value=value, action_id=action_id, ), ).to_dict()
def labeled_button_block( label_text: str, button_text: str, value: str, action_id: str, ) -> dict[str, str]: """ Generate labeled button block. Labeled buttons have the label text on the left and the button on the right. Args: label_text (str): Label text to display. button_text (str): Button text to display. value (str): Hidden value to assign to the button. action_id (str): Action identifier to assign to the button. Returns: dict: View block with desired button. """ return SectionBlock( text=label_text, accessory=ButtonElement( text=button_text, value=value, action_id=action_id, ), ).to_dict()
def button_block( text: str, value: str, action_id: str, style: Optional[str] = None, ) -> dict[str, str]: """ Generate UI button block. UI buttons are the ones in the body of the view. Args: text (str): Button text to display. value (str): Hidden value to assign to the button. action_id (str): Action identifier to assign to the button. style (str): One of "primary" or "danger". Returns: dict: View block with desired button. """ return ActionsBlock(elements=[ ButtonElement( text=text, value=value, action_id=action_id, style=style, ) ]).to_dict()
def test_document_3(self): input = { "type": "button", "text": {"type": "plain_text", "text": "Link Button"}, "url": "https://api.slack.com/block-kit", } self.assertDictEqual(input, ButtonElement(**input).to_dict()) self.assertDictEqual(input, LinkButtonElement(**input).to_dict())
def test_document_1(self): input = { "type": "button", "text": {"type": "plain_text", "text": "Click Me"}, "value": "click_me_123", "action_id": "button", } self.assertDictEqual(input, ButtonElement(**input).to_dict())
def test_accessibility_label_length(self): with self.assertRaises(SlackObjectFormationError): ButtonElement( text="Hi there!", action_id="button", value="click_me", accessibility_label=("1234567890" * 8), ).to_dict()
def test_document_4(self): input = { "type": "button", "text": { "type": "plain_text", "text": "Save" }, "style": "primary", "value": "click_me_123", "action_id": "button", "accessibility_label": "This label will be read out by screen readers", } self.assertDictEqual(input, ButtonElement(**input).to_dict())
def test_json(self): self.elements = [ ButtonElement(text="Click me", action_id="reg_button", value="1"), LinkButtonElement(text="URL Button", url="http://google.com"), ] self.dict_elements = [] for e in self.elements: self.dict_elements.append(e.to_dict()) self.assertDictEqual( {"elements": self.dict_elements, "type": "actions"}, ActionsBlock(elements=self.elements).to_dict(), ) with self.assertRaises(SlackObjectFormationError): ActionsBlock(elements=self.elements * 3).to_dict()
def test_element_parsing(self): elements = [ ButtonElement(text="Click me", action_id="reg_button", value="1"), StaticSelectElement(options=[Option(value="SelectOption")]), ImageElement(image_url="url", alt_text="alt-text"), OverflowMenuElement( options=[Option(value="MenuOption1"), Option(value="MenuOption2")] ), ] input = { "type": "actions", "block_id": "actionblock789", "elements": [e.to_dict() for e in elements], } parsed_elements = ActionsBlock(**input).elements self.assertEqual(len(elements), len(parsed_elements)) for original, parsed in zip(elements, parsed_elements): self.assertEqual(type(original), type(parsed)) self.assertDictEqual(original.to_dict(), parsed.to_dict())
def button_group_block(buttons: list[dict[str, str]]) -> dict[str, str]: """ Generate action button group block. Args: buttons (list): List of button dictionaries containing button_block params. Returns: dict: Action block with buttons. """ return ActionsBlock(elements=[ ButtonElement( text=button["text"], value=button["value"], action_id=button["action_id"], style=button.get("style"), ) for button in buttons ]).to_dict()
def handle_command(body: dict, ack: Ack, respond: Respond, client: WebClient, logger: Logger) -> None: logger.info(body) ack( text="Accepted!", blocks=[ SectionBlock( block_id="b", text=MarkdownTextObject(text=":white_check_mark: Accepted!"), ) ], ) respond(blocks=[ SectionBlock( block_id="b", text=MarkdownTextObject( text="You can add a button alongside text in your message. "), accessory=ButtonElement( action_id="a", text=PlainTextObject(text="Button"), value="click_me_123", ), ), ]) res = client.views_open( trigger_id=body["trigger_id"], view=View( type="modal", callback_id="view-id", title=PlainTextObject(text="My App"), submit=PlainTextObject(text="Submit"), close=PlainTextObject(text="Cancel"), blocks=[ InputBlock( element=PlainTextInputElement(action_id="text"), label=PlainTextObject(text="Label"), ), InputBlock( block_id="es_b", element=ExternalDataSelectElement( action_id="es_a", placeholder=PlainTextObject(text="Select an item"), min_query_length=0, ), label=PlainTextObject(text="Search"), ), InputBlock( block_id="mes_b", element=ExternalDataMultiSelectElement( action_id="mes_a", placeholder=PlainTextObject(text="Select an item"), min_query_length=0, ), label=PlainTextObject(text="Search (multi)"), ), ], ), ) logger.info(res)
def test_action_id(self): with self.assertRaises(SlackObjectFormationError): ButtonElement(text="click me!", action_id=STRING_301_CHARS, value="clickable button").to_dict()
def test_invalid_style(self): with self.assertRaises(SlackObjectFormationError): ButtonElement(text="Button", action_id="button", value="button", style="invalid").to_dict()
def test_value_length(self): with self.assertRaises(SlackObjectFormationError): ButtonElement(text="Button", action_id="button", value=STRING_3001_CHARS).to_dict()
def test_text_length(self): with self.assertRaises(SlackObjectFormationError): ButtonElement(text=STRING_301_CHARS, action_id="button", value="click_me").to_dict()
def test_action_id_length(self): with self.assertRaises(SlackObjectFormationError): ButtonElement(text="test", action_id="1234567890" * 26, value="click_me").to_dict()