Skip to main content

Using Blocks

Section Block

Class

SectionBlock(text: Optional[TextLike] = None, block_id: Optional[str] = None, fields: Optional[Union[TextLike, List[TextLike]]] = None, accessory: Optional[Element] = None)

A section is one of the most flexible blocks available - it can be used as a simple text block, or with any of the available block elements.

Section blocks can also optionally be given an "accessory," which is typically one of the interactive Elements.

Arguments

ArgumentTypeDescription
textOptional[TextLike]text to include in the block. Can be a string or Text object (of either
mrkdwn or plaintext variety). Defaults to markdown if unspecified. One of either
text or fields must be provided.
block_idOptional[str]you can use this field to provide a deterministic identifier for the block.
fieldsOptional[Union[TextLike, List[TextLike]]]a list of text objects. One of either text or fields must be provided.
accessoryOptional[Element]an optional Element object that will take a
secondary place in the block (after or to the side of text or fields).

Errors

ErrorWhen
InvalidUsageErrorif any of the provided arguments fail validation checks.
from slackblocks import Checkboxes, Option, SectionBlock

SectionBlock(
text="This is a section block with a checkbox accessory.",
block_id="fake_block_id"
accessory=CheckboxGroup(
action_id="checkboxes-action",
options=[
Option(
text="*Your Only Option*",
value="option_one"
)
]
)
)

Rich Text Block

Class

RichTextBlock(elements: Union[RichTextObject, List[RichTextObject]], block_id: Optional[str] = None)

A RichTextBlock is used to provide easier rich text formatting than standard markdown text (e.g. in a SectionBlock) and access to text formatting features not available in traditional markdown (e.g. strikethrough). See the various rich text elements you can include here.

Arguments

ArgumentTypeDescription
elementsUnion[RichTextObject, List[RichTextObject]]a single rich text element
or a list of those elements.
block_idOptional[str]you can use this field to provide a deterministic identifier
for the block.

Errors

ErrorWhen
InvalidUsageErrorif the elements in elements are not valid rich
text elements.
from slackblock import RichTextBlock, RichTextSection, RichText

RichTextBlock(
RichTextSection(
[
RichText(
"You 'bout to witness hip-hop in its most purest",
bold=True,
),
RichText(
"Most rawest form, flow almost flawless",
strike=True,
),
RichText(
"Most hardest, most honest known artist",
italic=True,
),
]
),
block_id="fake_block_id",
)

Header Block

Class

HeaderBlock(text: Union[str, Text], block_id: Optional[str] = None)

A Header Block is a plain-text block that displays in a larger, bold font.

Arguments

ArgumentTypeDescription
textUnion[str, Text]the text that will be rendered as a heading.
block_idOptional[str]you can use this field to provide a deterministic identifier for the block.
from slackblocks import HeaderBlock

HeaderBlock(
"This is a header block",
)

Image Block

Class

ImageBlock(image_url: str, alt_text: Optional[str] = ' ', title: Optional[Union[Text, str]] = None, block_id: Optional[str] = None)

An Image Block contains a single graphic, accessed by URL.

Arguments

ArgumentTypeDescription
image_urlstrthe URL pointing to the image file you want to display.
alt_textOptional[str]alternative text for accessibility purposes and when the image fails to load.
titleOptional[Union[Text, str]]an optional text title to be presented with the image.
block_idOptional[str]you can use this field to provide a deterministic identifier for the block.

Errors

ErrorWhen
InvalidUsageErrorwhen one or more of the provided args fails validation.
from slackblocks import ImageBlock

ImageBlock(
image_url="https://api.slack.com/img/blocks/bkb_template_images/beagle.png",
alt_text="a beagle",
title="dog",
block_id="fake_block_id",
)

Input Block

Class

InputBlock(label: TextLike, element: Element, dispatch_action: bool = False, block_id: Optional[str] = None, hint: Optional[TextLike] = None, optional: bool = False)

A block that collects information from users - it can hold a plain-text input element, a checkbox element, a radio button element, a select menu element, a multi-select menu element, or a datepicker.

Arguments

ArgumentTypeDescription
labelTextLikethe name which identifies the input field.
elementElementan interactive Element (e.g. a text field).
dispatch_actionboolwhether the Element should trigger the
sending of a block_actions payload.
block_idOptional[str]you can use this field to provide a deterministic identifier for the block.
hintOptional[TextLike]an optional additional guide on what input the user should prodive.
optionalboolwhether this input field may be empty when the user submits e.g. the modal.

Errors

ErrorWhen
InvalidUsageErrorwhen any of the provided arguments fail validation.
from slackblocks import InputBlock, Text, TextType, PlainTextInput

InputBlock(
label=Text("Label", type_=TextType.PLAINTEXT, emoji=True),
hint=Text("Hint", type_=TextType.PLAINTEXT, emoji=True),
element=PlainTextInput(action_id="action"),
block_id="fake_block_id",
optional=True,
)

Divider Block

Class

DividerBlock(block_id: Optional[str] = None)

A content divider, like an <hr> in HTML, to split up different blocks inside of a message.

Arguments

ArgumentTypeDescription
block_idOptional[str]you can use this field to provide a deterministic identifier for the block.
from slackblocks import DividerBlock

DividerBlock()

File Block

Class

FileBlock(external_id: str, block_id: Optional[str], source: str = 'remote')

Displays a remote file (e.g. a PDF).

For details on how remote files are exposed to Slack, see https://api.slack.com/messaging/files#adding.

Arguments

ArgumentTypeDescription
external_idstrthe ID assigned to the remote file when it was added to Slack.
block_idOptional[str]you can use this field to provide a deterministic identifier for the block.
sourcestralways "remote" as per the Slack API (may change in the future).
from slackblocks import FileBlock

FileBlock(
external_id="external_id",
block_id="fake_block_id",
)

Context Block

Class

ContextBlock(elements: Optional[List[Union[Element, CompositionObject]]] = None, block_id: Optional[str] = None)

A ContextBlock displays contextul message info, including both images and text.

Arguments

ArgumentTypeDescription
elementsOptional[List[Union[Element, CompositionObject]]]a list of Text objects and Image elements.
block_idOptional[str]you can use this field to provide a deterministic identifier for the block.

Errors

ErrorWhen
InvalidUsageErrorwhen items in elements are not Text or Image or exceed 10 items.
from slackblocks import ContextBlock, Text

ContextBlock(
elements=[
Text("Hello, world!"),
],
block_id="fake_block_id"
)

Actions Block

Class

ActionsBlock(elements: Optional[List[Element]] = None, block_id: Optional[str] = None)

A Block that is used to hold interactive elements (normally for users to interface with).

Arguments

ArgumentTypeDescription
elementsOptional[List[Element]]a list of Elements (up to a maximum of 25).
block_idOptional[str]you can use this field to provide a deterministic identifier for the block.

Errors

ErrorWhen
InvalidUsageErrorif any of the items in elements are invalid.
ActionsBlock(
block_id="fake_block_id",
elements=CheckboxGroup(
action_id="actionId-0",
options=[
Option(text="*a*", value="a", description="*a*"),
Option(text="*b*", value="b", description="*b*"),
Option(text="*c*", value="c", description="*c*"),
],
),
)