VOOZH about

URL: https://deepwiki.com/hypervel/notifications/5.3.3-slack-webhook-channel

⇱ Slack Webhook Channel | hypervel/notifications | DeepWiki


Loading...
Menu

Slack Webhook Channel

Purpose and Scope

This document details the SlackWebhookChannel implementation, which delivers notifications through Slack incoming webhooks. This channel provides a simple, authentication-free way to post messages to Slack channels by sending HTTP POST requests to webhook URLs.

For information about routing between webhook and Web API delivery, see Slack Routing. For delivery through Slack's authenticated API, see Slack Web API Channel. For constructing Slack message content, see Slack Messages.

Sources: src/Channels/SlackWebhookChannel.php1-111


Channel Overview

The SlackWebhookChannel class is a delivery channel implementation located at src/Channels/SlackWebhookChannel.php that sends notifications to Slack by posting JSON payloads to incoming webhook URLs. Unlike the Web API channel, webhooks require no OAuth authentication—each webhook URL is pre-configured in Slack to post to a specific channel or user.


Delivery Flow Diagram

Sources: src/Channels/SlackWebhookChannel.php18-44


Send Method Implementation

The send() method at src/Channels/SlackWebhookChannel.php31-44 orchestrates webhook delivery:

StepActionCode Reference
1Verify notification has toSlack() methodsrc/Channels/SlackWebhookChannel.php33-35
2Retrieve webhook URL from notifiablesrc/Channels/SlackWebhookChannel.php37-39
3Build JSON payload from SlackMessagesrc/Channels/SlackWebhookChannel.php41-43
4POST payload to webhook URLsrc/Channels/SlackWebhookChannel.php41
5Return HTTP responsesrc/Channels/SlackWebhookChannel.php31

Send Method Sequence

If the notifiable does not provide a webhook URL via routeNotificationFor('slack', $notification), the method returns null and no delivery is attempted src/Channels/SlackWebhookChannel.php37-39

Sources: src/Channels/SlackWebhookChannel.php31-44


Routing Configuration

The webhook channel requires the notifiable entity to provide a webhook URL through the routeNotificationFor() method. The channel requests routing for the 'slack' channel name:


src/Channels/SlackWebhookChannel.php37

The notifiable should return a complete Slack webhook URL string such as:

https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXX

This URL is pre-configured in Slack's "Incoming Webhooks" integration and specifies the target channel, workspace, and authorization token. The webhook URL itself contains all routing and authentication information.

Sources: src/Channels/SlackWebhookChannel.php37-39


JSON Payload Construction

The buildJsonPayload() method at src/Channels/SlackWebhookChannel.php49-67 transforms a SlackMessage object into the JSON structure expected by Slack's webhook API.


Payload Construction Process

Required Fields

The payload always includes:

Optional Fields

The following fields are extracted using data_get() and included only if present src/Channels/SlackWebhookChannel.php51-59:

FieldMessage PropertyPurpose
channel$message->channelOverride webhook's default channel
icon_emoji$message->iconDisplay custom emoji icon
icon_url$message->imageDisplay custom image icon
link_names$message->linkNamesEnable @mention and #channel linking
unfurl_links$message->unfurlLinksAuto-expand URLs in message
unfurl_media$message->unfurlMediaAuto-expand media URLs
username$message->usernameOverride webhook's default username

HTTP Options

The $message->http array is merged with the JSON payload to allow additional Guzzle HTTP client options such as headers, timeout, or proxy configuration src/Channels/SlackWebhookChannel.php66

Sources: src/Channels/SlackWebhookChannel.php49-67


Attachment Formatting

The webhook channel supports the legacy Slack attachments format through the attachments() method at src/Channels/SlackWebhookChannel.php72-96


Attachment Transformation Process

Each SlackAttachment object in the message is transformed into an array with standardized Slack API field names. The color field defaults to the message-level color if not specified per-attachment src/Channels/SlackWebhookChannel.php81

Field Formatting

The fields() method at src/Channels/SlackWebhookChannel.php101-110 handles two formats:

  1. SlackAttachmentField Objects - Converted via toArray() src/Channels/SlackWebhookChannel.php104-105
  2. Key-Value Pairs - Converted to {title, value, short: true} format src/Channels/SlackWebhookChannel.php108

This provides a simple interface for adding structured data to attachments. See Legacy Slack Attachments for detailed message construction.

Sources: src/Channels/SlackWebhookChannel.php72-110


Dependency Injection

The channel requires a GuzzleHttp\Client instance injected through the constructor src/Channels/SlackWebhookChannel.php23-26:


Dependency Resolution

The HTTP client handles the actual webhook POST request and returns a PSR-7 ResponseInterface object src/Channels/SlackWebhookChannel.php41

Sources: src/Channels/SlackWebhookChannel.php23-26


Error Handling

The webhook channel implements the following error conditions:

ConditionBehaviorCode Reference
Missing toSlack() methodThrows RuntimeExceptionsrc/Channels/SlackWebhookChannel.php33-35
No webhook URL returnedReturns null, skips deliverysrc/Channels/SlackWebhookChannel.php37-39
HTTP request failureException propagates from Guzzlesrc/Channels/SlackWebhookChannel.php41

The channel does not catch HTTP client exceptions—network errors, timeout errors, or 4xx/5xx responses from Slack will propagate to the NotificationSender for handling at a higher level.

Sources: src/Channels/SlackWebhookChannel.php31-44


Webhook vs Web API Comparison

AspectSlackWebhookChannelSlackWebApiChannel
AuthenticationNone (URL contains token)Requires OAuth token
Routing MethodReturns webhook URL stringReturns channel ID/name
Channel TargetingPre-configured in webhookDynamic via channel parameter
Message FormatLegacy attachments + simple textBlock Kit + attachments
Rate Limits1 request per second per webhookMore permissive workspace limits
FeaturesBasic formatting, attachmentsRich interactions, blocks, threading
Use CaseSimple notifications, external systemsFull Slack app integration

The webhook channel is identified by webhook URLs (starting with https://hooks.slack.com/) while the Web API channel is identified by channel IDs (like C1234567890). The SlackNotificationRouterChannel at src/Channels/SlackNotificationRouterChannel.php inspects the routing result to determine which channel to use—see Slack Routing for details.

Sources: src/Channels/SlackWebhookChannel.php1-111


Usage Example

To send a notification via webhook:

  1. The notification implements toSlack() returning a SlackMessage
  2. The notifiable implements routeNotificationFor('slack') returning a webhook URL
  3. The notification is sent with 'slack' in the via() array
  4. The router detects the webhook URL format and delegates to SlackWebhookChannel
  5. The channel POSTs the JSON payload to the webhook URL

The webhook URL determines the target workspace and channel. The optional channel field in the message payload can override the webhook's pre-configured channel within the same workspace src/Channels/SlackWebhookChannel.php52

Sources: src/Channels/SlackWebhookChannel.php1-111