Skip to content

Commit 05da802

Browse files
authored
feat(block-kit): add composition object examples (#228)
1 parent b845df9 commit 05da802

21 files changed

Lines changed: 784 additions & 0 deletions

‎block-kit/README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,15 @@ Read the [docs](https://docs.slack.dev/block-kit/) to learn concepts behind thes
6262
- **[User](https://docs.slack.dev/reference/block-kit/block-elements/user-element)**: Renders as a mention of a user. [Implementation](./src/elements/user.py).
6363
- **[Usergroup](https://docs.slack.dev/reference/block-kit/block-elements/usergroup-element)**: Renders as a mention of a user group. [Implementation](./src/elements/usergroup.py).
6464
- **[Workflow button](https://docs.slack.dev/reference/block-kit/block-elements/workflow-button-element)**: Allows users to run a link trigger with customizable inputs. [Implementation](./src/elements/workflow_button.py).
65+
66+
### Composition objects
67+
68+
- **[Confirmation dialog](https://docs.slack.dev/reference/block-kit/composition-objects/confirmation-dialog-object)**: Defines a dialog that adds a confirmation step to interactive elements. [Implementation](./src/compositions/confirmation_dialog.py).
69+
- **[Conversation filter](https://docs.slack.dev/reference/block-kit/composition-objects/conversation-filter-object)**: Defines a filter for the list of options in a conversation selector menu. [Implementation](./src/compositions/conversation_filter.py).
70+
- **[Dispatch action configuration](https://docs.slack.dev/reference/block-kit/composition-objects/dispatch-action-configuration-object)**: Defines when a plain-text input element will return a `block_actions` interaction payload. [Implementation](./src/compositions/dispatch_action_configuration.py).
71+
- **[Option group](https://docs.slack.dev/reference/block-kit/composition-objects/option-group-object)**: Defines a way to group options in a menu. [Implementation](./src/compositions/option_group.py).
72+
- **[Option](https://docs.slack.dev/reference/block-kit/composition-objects/option-object)**: Defines a single item in a number of item selection elements. [Implementation](./src/compositions/option.py).
73+
- **[Slack file](https://docs.slack.dev/reference/block-kit/composition-objects/slack-file-object)**: Defines an object containing Slack file information to be used in an image block or image element. [Implementation](./src/compositions/slack_file.py).
74+
- **[Text](https://docs.slack.dev/reference/block-kit/composition-objects/text-object)**: Defines an object containing some text. [Implementation](./src/compositions/text.py).
75+
- **[Trigger](https://docs.slack.dev/reference/block-kit/composition-objects/trigger-object)**: Defines an object containing trigger information. [Implementation](./src/compositions/trigger.py).
76+
- **[Workflow](https://docs.slack.dev/reference/block-kit/composition-objects/workflow-object)**: Defines an object containing workflow information. [Implementation](./src/compositions/workflow.py).

‎block-kit/src/compositions/__init__.py‎

Whitespace-only changes.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from slack_sdk.models.blocks import ActionsBlock
2+
from slack_sdk.models.blocks.basic_components import (
3+
ConfirmObject,
4+
MarkdownTextObject,
5+
PlainTextObject,
6+
)
7+
from slack_sdk.models.blocks.block_elements import ButtonElement
8+
9+
10+
def example01() -> ActionsBlock:
11+
"""
12+
Defines a dialog that adds a confirmation step to interactive elements.
13+
https://docs.slack.dev/reference/block-kit/composition-objects/confirmation-dialog-object/
14+
15+
An actions block with a button carrying a confirmation dialog.
16+
"""
17+
block = ActionsBlock(
18+
elements=[
19+
ButtonElement(
20+
text=PlainTextObject(text="Approve", emoji=True),
21+
confirm=ConfirmObject(
22+
title=PlainTextObject(text="Are you sure?"),
23+
text=MarkdownTextObject(
24+
text="Would you not prefer a good game of _chess_?"
25+
),
26+
confirm=PlainTextObject(text="Do it"),
27+
deny=PlainTextObject(text="Stop, I changed my mind!"),
28+
),
29+
style="primary",
30+
value="click_me_123",
31+
),
32+
ButtonElement(
33+
text=PlainTextObject(text="Deny", emoji=True),
34+
style="danger",
35+
value="click_me_123",
36+
),
37+
],
38+
)
39+
return block
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from slack_sdk.models.blocks import InputBlock
2+
from slack_sdk.models.blocks.basic_components import PlainTextObject
3+
from slack_sdk.models.blocks.block_elements import (
4+
ConversationFilter,
5+
ConversationSelectElement,
6+
)
7+
from slack_sdk.models.views import View
8+
9+
10+
def example01() -> View:
11+
"""
12+
Defines a filter for the list of options in a conversation selector menu.
13+
https://docs.slack.dev/reference/block-kit/composition-objects/conversation-filter-object/
14+
15+
A modal view with a conversations select input carrying a conversation filter.
16+
"""
17+
view = View(
18+
type="modal",
19+
title=PlainTextObject(text="My App", emoji=True),
20+
submit=PlainTextObject(text="Submit", emoji=True),
21+
close=PlainTextObject(text="Cancel", emoji=True),
22+
blocks=[
23+
InputBlock(
24+
element=ConversationSelectElement(
25+
placeholder=PlainTextObject(
26+
text="Select a conversation", emoji=True
27+
),
28+
filter=ConversationFilter(
29+
include=["public", "mpim"],
30+
exclude_bot_users=True,
31+
),
32+
),
33+
label=PlainTextObject(
34+
text="Choose the conversation to publish your result to:",
35+
emoji=True,
36+
),
37+
),
38+
],
39+
)
40+
return view
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from slack_sdk.models.blocks import InputBlock
2+
from slack_sdk.models.blocks.basic_components import (
3+
DispatchActionConfig,
4+
PlainTextObject,
5+
)
6+
from slack_sdk.models.blocks.block_elements import PlainTextInputElement
7+
8+
9+
def example01() -> InputBlock:
10+
"""
11+
Defines when a plain-text input element will return a block_actions interaction payload.
12+
https://docs.slack.dev/reference/block-kit/composition-objects/dispatch-action-configuration-object/
13+
14+
An input block with a multiline plain-text input carrying a dispatch action configuration.
15+
"""
16+
block = InputBlock(
17+
dispatch_action=True,
18+
element=PlainTextInputElement(
19+
multiline=True,
20+
dispatch_action_config=DispatchActionConfig(
21+
trigger_actions_on=["on_character_entered"],
22+
),
23+
),
24+
label=PlainTextObject(text="This is a multiline plain-text input", emoji=True),
25+
)
26+
return block
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from slack_sdk.models.blocks import Block, DividerBlock, SectionBlock
2+
from slack_sdk.models.blocks.basic_components import (
3+
MarkdownTextObject,
4+
Option,
5+
PlainTextObject,
6+
)
7+
from slack_sdk.models.blocks.block_elements import StaticSelectElement
8+
9+
10+
def example01() -> Option:
11+
"""
12+
Defines a single item in a number of item selection elements.
13+
https://docs.slack.dev/reference/block-kit/composition-objects/option-object/
14+
15+
A single option object.
16+
"""
17+
option = Option(
18+
text=PlainTextObject(text="Save it", emoji=True),
19+
value="value-2",
20+
)
21+
return option
22+
23+
24+
def example02() -> list[Block]:
25+
"""
26+
A static select menu element with several option objects.
27+
"""
28+
blocks: list[Block] = [
29+
SectionBlock(
30+
text=MarkdownTextObject(text=":mag: Search results for *Cata*"),
31+
),
32+
DividerBlock(),
33+
SectionBlock(
34+
text=MarkdownTextObject(
35+
text="*<fakeLink.toYourApp.com|Use Case Catalogue>*\nUse Case Catalogue for the following departments/roles..."
36+
),
37+
accessory=StaticSelectElement(
38+
placeholder=PlainTextObject(text="Manage", emoji=True),
39+
options=[
40+
Option(
41+
text=PlainTextObject(text="Edit it", emoji=True),
42+
value="value-0",
43+
),
44+
Option(
45+
text=PlainTextObject(text="Read it", emoji=True),
46+
value="value-1",
47+
),
48+
Option(
49+
text=PlainTextObject(text="Save it", emoji=True),
50+
value="value-2",
51+
),
52+
],
53+
),
54+
),
55+
]
56+
return blocks
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from slack_sdk.models.blocks import Block, DividerBlock, SectionBlock
2+
from slack_sdk.models.blocks.basic_components import (
3+
MarkdownTextObject,
4+
Option,
5+
OptionGroup,
6+
PlainTextObject,
7+
)
8+
from slack_sdk.models.blocks.block_elements import StaticSelectElement
9+
10+
11+
def example01() -> list[Block]:
12+
"""
13+
Defines a way to group options in a menu.
14+
https://docs.slack.dev/reference/block-kit/composition-objects/option-group-object/
15+
16+
A static select menu containing the option group object.
17+
"""
18+
blocks: list[Block] = [
19+
SectionBlock(
20+
text=MarkdownTextObject(text=":mag: Search results for *Cata*"),
21+
),
22+
DividerBlock(),
23+
SectionBlock(
24+
text=MarkdownTextObject(
25+
text="*<fakeLink.toYourApp.com|Use Case Catalogue>*\nUse Case Catalogue for the following departments/roles..."
26+
),
27+
accessory=StaticSelectElement(
28+
placeholder=PlainTextObject(text="Manage", emoji=True),
29+
option_groups=[
30+
OptionGroup(
31+
label=PlainTextObject(text="Group 1"),
32+
options=[
33+
Option(
34+
text=PlainTextObject(text="*this is plain_text text*"),
35+
value="value-0",
36+
),
37+
Option(
38+
text=PlainTextObject(text="*this is plain_text text*"),
39+
value="value-1",
40+
),
41+
Option(
42+
text=PlainTextObject(text="*this is plain_text text*"),
43+
value="value-2",
44+
),
45+
],
46+
),
47+
OptionGroup(
48+
label=PlainTextObject(text="Group 2"),
49+
options=[
50+
Option(
51+
text=PlainTextObject(text="*this is plain_text text*"),
52+
value="value-3",
53+
),
54+
],
55+
),
56+
],
57+
),
58+
),
59+
]
60+
return blocks
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from slack_sdk.models.blocks import ImageBlock
2+
from slack_sdk.models.blocks.basic_components import PlainTextObject, SlackFile
3+
4+
5+
def example01() -> ImageBlock:
6+
"""
7+
Defines an object containing Slack file information to be used in an image block or image element.
8+
https://docs.slack.dev/reference/block-kit/composition-objects/slack-file-object/
9+
10+
An image block using slack_file with a url.
11+
"""
12+
block = ImageBlock(
13+
title=PlainTextObject(text="Please enjoy this photo of a kitten"),
14+
block_id="image4",
15+
slack_file=SlackFile(
16+
url="https://files.slack.com/files-pri/T0123456-F0123456/xyz.png"
17+
),
18+
alt_text="An incredibly cute kitten.",
19+
)
20+
return block
21+
22+
23+
def example02() -> ImageBlock:
24+
"""
25+
An image block using slack_file with an id.
26+
"""
27+
block = ImageBlock(
28+
title=PlainTextObject(text="Please enjoy this photo of a kitten"),
29+
block_id="image4",
30+
slack_file=SlackFile(id="F0123456"),
31+
alt_text="An incredibly cute kitten.",
32+
)
33+
return block

‎block-kit/src/compositions/text.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from slack_sdk.models.blocks import SectionBlock
2+
from slack_sdk.models.blocks.basic_components import MarkdownTextObject
3+
4+
5+
def example01() -> SectionBlock:
6+
"""
7+
Defines an object containing some text.
8+
https://docs.slack.dev/reference/block-kit/composition-objects/text-object/
9+
10+
A section block containing a text object.
11+
"""
12+
block = SectionBlock(
13+
text=MarkdownTextObject(
14+
text="A message *with some bold text* and _some italicized text_."
15+
)
16+
)
17+
return block
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from slack_sdk.models.blocks import SectionBlock
2+
from slack_sdk.models.blocks.basic_components import (
3+
MarkdownTextObject,
4+
PlainTextObject,
5+
Workflow,
6+
WorkflowTrigger,
7+
)
8+
from slack_sdk.models.blocks.block_elements import WorkflowButtonElement
9+
10+
11+
def example01() -> SectionBlock:
12+
"""
13+
Defines an object containing trigger information.
14+
https://docs.slack.dev/reference/block-kit/composition-objects/trigger-object/
15+
16+
A section block with a workflow button whose trigger carries customizable input parameters.
17+
"""
18+
block = SectionBlock(
19+
text=MarkdownTextObject(
20+
text="A message *with some bold text* and _some italicized text_."
21+
),
22+
accessory=WorkflowButtonElement(
23+
text=PlainTextObject(text="Run Workflow"),
24+
action_id="workflowbutton123",
25+
workflow=Workflow(
26+
trigger=WorkflowTrigger(
27+
url="https://slack.com/shortcuts/Ft0123ABC456/xyz...zyx",
28+
customizable_input_parameters=[
29+
{
30+
"name": "input_parameter_a",
31+
"value": "Value for input param A",
32+
},
33+
{
34+
"name": "input_parameter_b",
35+
"value": "Value for input param B",
36+
},
37+
],
38+
),
39+
),
40+
),
41+
)
42+
return block

0 commit comments

Comments
 (0)