Skip to content

Welcome to slackblocks!

slackblocks is a Python library for building Slack messages with the Block Kit API — without writing JSON by hand.

It exists because Block Kit JSON is verbose, easy to get subtly wrong, and unpleasant to maintain in source control. slackblocks gives you:

  • Typed Python classes for every block, element, and object Slack supports.
  • Validation as you build — character limits, required fields, mutually-exclusive options, and element-type restrictions are enforced at construction time, so you find out before hitting Slack's API.
  • Drop-in compatibility with both the official slack-sdk and the legacy slackclient — unpack a Message directly into client.chat_postMessage(**message).
  • Zero runtime dependencies.

Quickstart

pip install slackblocks
from slackblocks import HeaderBlock, Message, SectionBlock, DividerBlock

message = Message(
    channel="#general",
    blocks=[
        HeaderBlock("Build #482 passed :white_check_mark:"),
        SectionBlock("All 1,247 tests green in 3m 12s."),
        DividerBlock(),
        SectionBlock("*Author:* @nick  •  *Branch:* `main`"),
    ],
)

print(message.json())

See the installation guide and sending messages guide to take it from here.

Components

The Slack Block Kit API defines several resource types (all defined in JSON) that work together to build block-based messages. slackblocks mirrors that hierarchy with Python classes.

Objects

Objects (e.g. Text, Option, Confirm) are the lowest-level primitives — small composable pieces that populate Elements and Blocks.

For convenience, PlainText and Markdown are thin subclasses of Text that you can use anywhere a Text is expected — PlainText("Hi", emoji=True) reads more naturally than Text("Hi", type_=TextType.PLAINTEXT, emoji=True).

Elements

Elements are typically interactive UI controls that go inside blocks. The CheckboxGroup element, for instance, takes one or more Option items and presents a checkbox menu.

Blocks

Blocks are the core visual unit of a message. Different block classes produce different UI:

See Using Blocks for examples of all block types side-by-side with their JSON output and Slack rendering.

Messages

Messages are a convenience wrapper around blocks that can be unpacked directly into the Slack SDK's chat_postMessage (and friends).

Views

Views are an alternative usage of blocks that build custom UI surfaces in Slack — modal dialogs and the App Home tab — typically used by interactive Slack apps.

Utilities

Guides