VOOZH about

URL: https://deepwiki.com/hypervel/notifications/6.3-slack-messages

⇱ Slack Messages | hypervel/notifications | DeepWiki


Loading...
Menu

Slack Messages

Purpose and Scope

This page documents the SlackMessage class, which is used to construct modern Slack notifications using Block Kit. The SlackMessage provides a fluent interface for building rich, interactive messages with blocks, buttons, images, and other UI components.

For information about the legacy attachment-based Slack message format, see Legacy Slack Attachments. For detailed documentation of individual Block Kit components (blocks, elements, composites), see Slack Block Kit. For information about how Slack messages are routed and delivered, see Slack Channels.


Class Overview

The SlackMessage class serves as the primary message builder for Slack notifications. It converts notification content into the JSON payload format expected by Slack's API endpoints.


Class Location and Structure

Sources: src/Slack/SlackMessage.php1-336


Core Properties

The SlackMessage class manages several categories of properties that control message appearance and behavior:

Property CategoryPropertiesPurpose
Contenttext, blocksMessage content (text fallback and Block Kit blocks)
Targetingchannel, threadTs, broadcastReplyWhere the message is sent
Appearanceusername, icon, imageBot identity customization
Behaviormrkdwn, unfurlLinks, unfurlMediaContent rendering options
AdvancedmetaDataStructured metadata for app functionality

Sources: src/Slack/SlackMessage.php26-86


Channel Targeting

Setting the Channel

The to() method specifies which Slack channel receives the message:


The channel value is stored in the channel property and included in the final JSON payload.

Sources: src/Slack/SlackMessage.php90-95


Message Content

Text Content

The text() method sets the message's text content, which serves as:

  • Fallback text for notifications
  • Content when no blocks are present
  • Text displayed in notification banners

At least one of text or blocks must be present, or toArray() will throw a LogicException.

Sources: src/Slack/SlackMessage.php100-105 src/Slack/SlackMessage.php298-300

Block Kit Blocks

The SlackMessage class provides convenience methods for adding Block Kit blocks. Each method accepts a closure that receives a block instance for configuration:










































MethodBlock TypePurpose
actionsBlock()ActionsBlockInteractive buttons and selects
contextBlock()ContextBlockContextual information (text + images)
dividerBlock()DividerBlockVisual separator line
headerBlock()HeaderBlockLarge heading text
imageBlock()ImageBlockFull-width image display
sectionBlock()SectionBlockText content with optional accessory

Example Usage:


The blocks are stored in the blocks array property. When converted to an array, each block that implements BlockContract has its toArray() method called.

Sources: src/Slack/SlackMessage.php107-180 src/Slack/SlackMessage.php308


Message Appearance

Bot Username

The username() method customizes the bot name displayed in Slack:


This overrides the default bot name configured in the Slack app settings.

Sources: src/Slack/SlackMessage.php246-252

Bot Icon

Two methods control the bot's avatar:

Emoji Icon:


Image Icon:


Note that these methods are mutually exclusive. Setting an emoji clears the image, and vice versa.


Sources: src/Slack/SlackMessage.php185-202


Markdown and Unfurling

Markdown Parsing

By default, Slack parses markdown in message text. This can be disabled:


This sets the mrkdwn property to false, which is included in the JSON payload.

Sources: src/Slack/SlackMessage.php217-222

Link Unfurling

Control how Slack handles links in the message:

Unfurl Links:


Unfurl Media:


These correspond to Slack's unfurl_links and unfurl_media API parameters.

Sources: src/Slack/SlackMessage.php225-242


Threading and Replies

Thread Replies

To send a message as a thread reply, provide the parent message's timestamp:


The threadTs property identifies the parent message in Slack's threading system.

Broadcast Replies

When replying to a thread, optionally broadcast a reference to the parent channel:


This shows a "replied in thread" notification in the main channel.


Sources: src/Slack/SlackMessage.php257-272


Metadata

The metadata() method attaches structured metadata to messages:


This creates an EventMetadata instance with an event type and payload. The metadata can be used by Slack apps for custom functionality like filtering, routing, or triggering workflows.

The metadata is serialized to JSON using the metadata field in the Slack API.

Sources: src/Slack/SlackMessage.php207-212 src/Slack/SlackMessage.php311


Advanced Features

Raw Block Kit Templates

The usingBlockKitTemplate() method imports a complete Block Kit JSON structure:


Requirements:

  • The JSON must be valid (throws JsonException if not)
  • The JSON must contain a top-level blocks key (throws LogicException if missing)
  • The blocks from the template are appended to any existing blocks

This is useful for:

  • Importing messages designed in Slack's Block Kit Builder
  • Reusing complex message templates
  • Testing with pre-defined JSON payloads

Sources: src/Slack/SlackMessage.php279-291

Debugging Helper

The dd() method provides debugging assistance:


When called without the raw parameter, it generates a URL to Slack's Block Kit Builder with the message pre-loaded. This allows visual inspection and editing of the message structure.

The generated URL format:

https://app.slack.com/block-kit-builder#<urlencoded-json>

Sources: src/Slack/SlackMessage.php328-335


Validation and Constraints

Required Content

The toArray() method enforces that messages contain either text or blocks:


This validation occurs when the message is converted to its JSON array representation.

Block Count Limit

Slack limits messages to 50 blocks maximum:


This is a Slack API limitation enforced by the toArray() method.

Sources: src/Slack/SlackMessage.php298-304


Array Conversion

The toArray() method converts the SlackMessage to the array structure expected by Slack's API:


Field Mapping:

SlackMessage PropertyJSON FieldNotes
channelchannelAlways included
texttextOptional
blocksblocksEach block converted via toArray()
iconicon_emojiOptional
imageicon_urlOptional
metaDatametadataConverted via EventMetadata::toArray()
mrkdwnmrkdwnOptional
threadTsthread_tsOptional
broadcastReplyreply_broadcastOptional
unfurlLinksunfurl_linksOptional
unfurlMediaunfurl_mediaOptional
usernameusernameOptional

Null values are filtered out before the final array is returned, ensuring only explicitly set values are included in the API payload.

Sources: src/Slack/SlackMessage.php296-323


Integration with Notification System

The SlackMessage integrates with the notification system through the notification's toSlack() method:


The notification's toSlack() method constructs and returns a SlackMessage instance. The channel then converts it to an array and sends it to the appropriate Slack endpoint (Web API or Webhook).

Sources: src/Slack/SlackMessage.php21-336


Usage Example

Complete example showing common features:


Sources: src/Slack/SlackMessage.php1-336