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_document_1(self): input = { "type": "actions", "block_id": "actions1", "elements": [ { "type": "static_select", "placeholder": { "type": "plain_text", "text": "Which witch is the witchiest witch?", }, "action_id": "select_2", "options": [ { "text": { "type": "plain_text", "text": "Matilda" }, "value": "matilda", }, { "text": { "type": "plain_text", "text": "Glinda" }, "value": "glinda", }, { "text": { "type": "plain_text", "text": "Granny Weatherwax" }, "value": "grannyWeatherwax", }, { "text": { "type": "plain_text", "text": "Hermione" }, "value": "hermione", }, ], }, { "type": "button", "text": { "type": "plain_text", "text": "Cancel" }, "value": "cancel", "action_id": "button_1", }, ], } self.assertDictEqual(input, ActionsBlock(**input).to_dict()) self.assertDictEqual(input, Block.parse(input).to_dict())
def test_json(self): block = ActionsBlock(elements=self.elements).to_dict() hard = { "type": "actions", "elements": [e.to_dict() for e in self.elements] } self.assertDictEqual(block, hard)
def test_json(self): self.assertDictEqual( ActionsBlock(elements=self.elements).to_dict(), { "elements": [e.to_dict() for e in self.elements], "type": "actions" }, )
def main(rqst: Union[InteractiveMessageRequest, CommandRequest]) -> None: block_id = cmd.prog + '.main.button' # create a Slack message that will be used to respond to the User's # interaction which was the invocation of the /demo command. resp = Response(rqst) # ------------------------------------------------------------------------- # define the button callback handler to send a response back to the # User telling the time when they pressed the button # ------------------------------------------------------------------------- @rqst.app.ic.block_action.on(block_id) def on_button(btn_rqst: BlockActionRequest, btn_action: ActionEvent): btn_resp = Response(btn_rqst) btn_resp.send_response(text=( f"At timestamp `{btn_action.data['action_ts']}`, " f"you pressed: *{btn_action.value.title()}*") ) # ------------------------------------------------------------------------- # create a message to send to the User that has two buttons; and when # they click either one, the above callback will be executed. # ------------------------------------------------------------------------- user_id = rqst.user_id resp['blocks'] = extract_json([ SectionBlock(text=MarkdownTextObject(text=f'Hi there <@{user_id}>!')), DividerBlock(), ActionsBlock( block_id=block_id, elements=[ ButtonElement( text='Press for Bad', style='danger', action_id=f'{block_id}.bad', value='bad'), ButtonElement( text='Press for Good', style="primary", action_id=f'{block_id}.good', value='good') ] ), DividerBlock() ]) resp.send()
def actions(self, *, elements: List[InteractiveElement], block_id: Optional[str] = None): """A block that is used to hold interactive elements. https://api.slack.com/reference/block-kit/blocks#actions Args: elements: Up to 5 InteractiveElement objects - buttons, date pickers, etc block_id: ID to be used for this block - autogenerated if left blank. Cannot exceed 255 characters. """ self._blocks.append(ActionsBlock(elements=elements, block_id=block_id)) return self
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()
async def test_with_blocks(self): url = os.environ[SLACK_SDK_TEST_INCOMING_WEBHOOK_URL] webhook = AsyncWebhookClient(url) response = await webhook.send( text="fallback", blocks=[ 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(), 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", ), ], ), ]) self.assertEqual(200, response.status_code) self.assertEqual("ok", response.body)
def test_document_2(self): input = { "type": "actions", "block_id": "actionblock789", "elements": [{ "type": "datepicker", "action_id": "datepicker123", "initial_date": "1990-04-28", "placeholder": { "type": "plain_text", "text": "Select a date" } }, { "type": "overflow", "options": [{ "text": { "type": "plain_text", "text": "*this is plain_text text*" }, "value": "value-0" }, { "text": { "type": "plain_text", "text": "*this is plain_text text*" }, "value": "value-1" }, { "text": { "type": "plain_text", "text": "*this is plain_text text*" }, "value": "value-2" }, { "text": { "type": "plain_text", "text": "*this is plain_text text*" }, "value": "value-3" }, { "text": { "type": "plain_text", "text": "*this is plain_text text*" }, "value": "value-4" }], "action_id": "overflow" }, { "type": "button", "text": { "type": "plain_text", "text": "Click Me" }, "value": "click_me_123", "action_id": "button" }] } self.assertDictEqual(input, ActionsBlock(**input).to_dict())
def test_elements_length(self): with self.assertRaises(SlackObjectFormationError): ActionsBlock(elements=self.elements * 3).to_dict()