VOOZH about

URL: https://deepwiki.com/hypervel/notifications/6.5-slack-block-kit

⇱ Slack Block Kit | hypervel/notifications | DeepWiki


Loading...
Menu

Slack Block Kit

Purpose and Scope

This document provides a comprehensive guide to building rich Slack messages using Block Kit components within the hypervel/notifications package. Block Kit is Slack's modern UI framework that enables interactive, structured messages with buttons, images, text formatting, and contextual elements.

This page covers the architecture and composition patterns of Block Kit. For general Slack message construction, see Slack Messages. For the legacy attachment-based format, see Legacy Slack Attachments. Detailed references for specific components are available in Block Kit Contracts, Block Types, and Elements and Composites.

Block Kit Architecture

Block Kit follows a three-layer architecture that composes into rich message structures. Each layer serves a distinct purpose in the message hierarchy:

Three-Layer Component System


Sources: Architecture derived from Diagram 3 in system overview

LayerPurposeExamplesContains
BlocksTop-level structural containersActionsBlock, SectionBlock, HeaderBlockElements and text objects
ElementsInteractive or visual componentsButtonElement, ImageElement, TextObjectComposite objects (optional)
CompositesConfiguration objectsConfirmObject, PlainTextOnlyTextObjectPrimitive data

Contract System

All Block Kit components implement contract interfaces that extend Hyperf\Contract\Arrayable, enabling serialization to the array format required by Slack's API.


Sources: src/Contracts/Slack/BlockContract.php1-12 src/Contracts/Slack/ElementContract.php1-12 src/Contracts/Slack/ObjectContract.php1-12

Contract Interfaces

ContractLocationPurpose
BlockContractsrc/Contracts/Slack/BlockContract.php9Marks top-level block components
ElementContractsrc/Contracts/Slack/ElementContract.php9Marks interactive/visual elements
ObjectContractsrc/Contracts/Slack/ObjectContract.php9Marks composite configuration objects

Each contract requires implementing toArray(): array, which serializes the component into Slack's JSON format. The contracts provide no additional methods beyond Arrayable, serving primarily as type markers for the component hierarchy.

Sources: src/Contracts/Slack/BlockContract.php1-12 src/Contracts/Slack/ElementContract.php1-12 src/Contracts/Slack/ObjectContract.php1-12

Component Composition Pattern

Block Kit components compose hierarchically, with blocks at the top containing elements, which may contain composite objects. This composition pattern enables building complex interactive messages from simple, reusable components.

Message Assembly Flow


Sources: Architecture derived from Diagram 3 in system overview

The composition follows this pattern:

  1. Notification method (toSlack()) creates a SlackMessage instance
  2. SlackMessage accumulates blocks via method chaining or array assignment
  3. Blocks are instantiated with their required elements/text objects
  4. Elements may include composite objects for additional configuration
  5. Serialization occurs when the message is sent, calling toArray() recursively

Component Categories

The package provides comprehensive Block Kit support across all three layers:

Available Block Types

Block TypeClass NamePurposeKey Features
ActionsActionsBlockContainer for interactive elementsHolds buttons, selects, menus
SectionSectionBlockText content with optional accessoryMain content block, supports markdown
HeaderHeaderBlockLarge heading textPlain text only, prominent display
ImageImageBlockStandalone image displayFull-width image with optional title
ContextContextBlockSmall supplementary informationMultiple text/image elements
DividerDividerBlockVisual separator lineNo configuration needed

Sources: Derived from Diagram 3 in system overview

Available Elements

Element TypeClass NameUsed InInteractive
ButtonButtonElementActions, Section accessoryYes
ImageImageElementSection accessory, ContextNo
TextTextObjectSection, Context, HeaderNo

Sources: Derived from Diagram 3 in system overview

Available Composite Objects

Object TypeClass NamePurpose
ConfirmConfirmObjectConfirmation dialog for destructive actions
Plain TextPlainTextOnlyTextObjectPlain text wrapper with emoji flag

Sources: Derived from Diagram 3 in system overview

Serialization to Slack API Format

All Block Kit components implement the Arrayable contract, enabling seamless serialization to Slack's required JSON structure. When a notification is sent through the Slack channel, the serialization chain executes:


Sources: Architecture derived from Diagrams 1 and 2 in system overview

Serialization Chain

  1. SlackMessage::toArray() initiates serialization
  2. Each block's toArray() method serializes block-specific fields
  3. Nested elements recursively serialize through their toArray() methods
  4. Composite objects serialize to their configuration structures
  5. The resulting nested array structure matches Slack's JSON schema

This recursive serialization pattern ensures type safety at composition time while producing the flat array structure Slack's API expects.

Sources: src/Contracts/Slack/BlockContract.php7-9 src/Contracts/Slack/ElementContract.php7-9 src/Contracts/Slack/ObjectContract.php7-9

Integration with SlackMessage

Block Kit components integrate with the SlackMessage class, which serves as the container for all blocks. The SlackMessage class manages the blocks array and handles serialization to Slack's payload format.

From the architecture diagrams, we see that SlackMessage (importance 10.13) routes through SlackNotificationRouterChannel to either SlackWebApiChannel or SlackWebhookChannel. Both channels accept the serialized block structure as part of the message payload.

Sources: Diagram 3 in system overview

Next Steps

For detailed information on specific Block Kit components:

For understanding how Block Kit messages are sent:

Sources: Table of contents and Diagram 1 in system overview