Utilities
This page documents top-level helper utilities shipped with slackblocks.
Block Kit Builder URL
block_kit_builder_url(payload, team_id=None) builds a Slack Block Kit Builder URL containing the supplied payload, so you can preview a message, view, or block list in the browser.
Accepted payload shapes:
- A single
Block— wrapped in{"blocks": [block]}automatically. - A list of
Block— also wrapped. - Anything with a
_resolve()method (Message,WebhookMessage,MessageResponse,View,ModalView,HomeTabView). - A raw
dict— used verbatim. This is the escape hatch for callers building payloads outside the type hierarchy.
For a team_id-specific Builder URL (e.g. https://app.slack.com/block-kit-builder/T0123#…), pass team_id="T0123ABCD".
Utilities for working with Slack's Block Kit Builder.
The Builder accepts a URL of the form https://app.slack.com/block-kit-builder/#<JSON>
where <JSON> is a URL-encoded JSON payload (typically {"blocks": [...]}).
block_kit_builder_url constructs such a URL from any of the shapes the
rest of this library renders.
block_kit_builder_url
block_kit_builder_url(payload, team_id=None)
Build a URL that opens payload in Slack's Block Kit Builder.
Use this to preview a message, view, or list of blocks in the browser without manually copying JSON into the Builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
Block | list[Block] | Resolvable | dict[str, Any]
|
any of:
|
required |
team_id
|
str | None
|
optional Slack team ID. When supplied, produces a
workspace-specific Builder URL of the form
|
None
|
Returns:
| Type | Description |
|---|---|
str
|
A fully-formed Block Kit Builder URL. |
Examples:
Single block::
from slackblocks import SectionBlock, block_kit_builder_url
url = block_kit_builder_url(SectionBlock("Hi"))
Multiple blocks::
url = block_kit_builder_url([
SectionBlock("Heading"),
DividerBlock(),
])
Existing message::
url = block_kit_builder_url(my_message)
Targeting a specific workspace::
url = block_kit_builder_url(my_message, team_id="T0123ABCD")
Source code in slackblocks/builder.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
Validation helpers
Lower-level helper functions used across slackblocks for validation and coercion. Most users won't need to call these directly, but they're documented here for completeness and for anyone extending slackblocks with custom block or element types.
This module collects various utility functions used for validating
the input to Messages, Blocks, Elements and Objects.
coerce_to_list
coerce_to_list(
object_or_objects,
class_,
allow_none=False,
min_size=None,
max_size=None,
)
Takes and object or list of objects and validates its contents, ensuring that the resulting object is a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_or_objects
|
T | list[T] | None
|
the Python object or objects to validate and convert to a list. |
required |
class_
|
Any | list[Any]
|
the Python type (or class) of objects expected in the list. |
required |
allow_none
|
bool
|
whether or not None is a valid input (and thus output) option. |
False
|
min_size
|
int | None
|
if provided, the length of |
None
|
max_size
|
int | None
|
if provided, the length of |
None
|
Returns:
| Type | Description |
|---|---|
list[T] | None
|
|
Throws
InvalidUsageError: if any of the validation checks fail.
Source code in slackblocks/utils.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
coerce_to_list_nonnull
coerce_to_list_nonnull(
object_or_objects, class_, min_size=None, max_size=None
)
Takes an object or list of objects and validates its contents, ensuring that the resulting object is a list. This version does not handle None values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_or_objects
|
T | list[T]
|
the Python object or objects to validate and convert to a list. |
required |
class_
|
Any | list[Any]
|
the Python type (or class) of objects expected in the list. |
required |
min_size
|
int | None
|
if provided, the length of |
None
|
max_size
|
int | None
|
if provided, the length of |
None
|
Returns:
| Type | Description |
|---|---|
list[T]
|
|
Throws
InvalidUsageError: if any of the validation checks fail.
Source code in slackblocks/utils.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
is_hex
is_hex(string)
Determines whether a given string is a valid hexadecimal number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
the string to examine for hex characters. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in slackblocks/utils.py
106 107 108 109 110 111 112 113 114 115 116 | |
validate_action_id
validate_action_id(action_id, allow_none=False)
Action IDs are used in the handing of user interactivity within Slack blocks.
This function checks that a given action_id is valid as per the requirements
imposed by the Slack API.
See: https://api.slack.com/interactivity/handling
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action_id
|
str | None
|
the action_id string to validate for correctness as per the Slack API. |
required |
allow_none
|
bool
|
whether to accept |
False
|
Returns:
| Type | Description |
|---|---|
str | None
|
The original value |
Throws
InvalidUsageError if any of the validation checks fail.
Source code in slackblocks/utils.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
validate_int
validate_int(
num, min_value=None, max_value=None, allow_none=False
)
Performs basic validation checks against a given integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num
|
int | None
|
the number to validate. |
required |
min_value
|
int | None
|
if |
None
|
max_value
|
int | None
|
if |
None
|
allow_none
|
bool
|
whether |
False
|
Returns:
| Type | Description |
|---|---|
int | None
|
The original value of |
Throws
InvalidUsageError: if any of the validation checks fail.
Source code in slackblocks/utils.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
validate_string
validate_string(
string,
field_name,
max_length=None,
min_length=None,
allow_none=False,
)
Performs basic validation actions (e.g. length checking) on a given string based on the provided criteria.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str | None
|
the string to validate |
required |
field_name
|
str
|
the name of the field the string belongs to (for error reporting purposes). |
required |
min_length
|
int | None
|
if the string is less than this length, an error will be raised. |
None
|
max_length
|
int | None
|
if the string is greated than this length, an error will be raised. |
None
|
allow_none
|
bool
|
whether |
False
|
Returns:
| Type | Description |
|---|---|
str | None
|
The original string if it deemed to be valid (i.e. no errors are thrown). |
Throws
InvalidUsageError: if any of the validation checks (length, None) fail.
Source code in slackblocks/utils.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |