Using Block Kit helpers in the Python Slack SDK
Slack really wants Block Kit to be used for building interactive messages, but the raw JSON representation can be unwieldy. Block Kit Builder unfortunately only emits JSON, but once you’ve sketched something out you want, you are likely going to need to make some elements in code dynamically anyway.
Asking an AI assistant did not give me the best answer for building a Python app, unfortunately:
What is a good library to use Slack Block Kit in python?
A great library to use for building Slack Block Kit interfaces in Python is Blockkit. It allows you to compose Slack app interfaces using Python classes, making your code more maintainable and reducing the amount of typing you need to do. Blockkit also performs runtime validation and provides user-friendly errors when data is invalid.
Another good option is PyBlock Builder. It’s a lightweight library designed to make it easier to create Slack apps with fewer lines of code. It’s optimized for use with Slack’s Bolt for Python SDK and offers a declarative syntax with method chaining.
Both libraries are available on PyPI and can be installed using
pip
. Do you have a specific project in mind for using Block Kit?
While these libraries look completely fine, there is already a bunch of built-in, but not highly documented set of classes in the Python Slack SDK. I think they are a bit harder to discover because they aren’t called out explicitly in the Python Slack SDK documentation, but if you dive into the API-level documentation, you’ll find specific docs for the slack_sdk.models.blocks
package.
# blockkit package
from blockkit import (
Actions, Button, Divider, Header, Home, PlainOption, Section, StaticSelect,
)
# pyblock-builder package
from pyblock_builder.surfaces import Message
from pyblock_builder.blocks import Section, Actions
from pyblock_builder.elements import Button
# Python Slack SDK built-ins
from slack_sdk.models.blocks import (
ActionsBlock, ButtonElement, DividerBlock, HeaderBlock, Option, SectionBlock, StaticSelectElement,
)
You can see there isn’t a whole lot of difference between the first and third options here.
Related Posts
- Using Slack Bolt in a Python Litestar App
- Using Python asyncio in a standalone script
- Improved Related Alert Matching
🚌 Ensure your commute is smooth and hassle-free with Mobility Bot. Install it now in your Slack workspace!
🚇 Stay informed with real-time transit alerts tailored to your specific routes and schedule.