VOOZH about

URL: https://deepwiki.com/hypervel/notifications/5.3-slack-channels

⇱ Slack Channels | hypervel/notifications | DeepWiki


Loading...
Menu

Slack Channels

Purpose and Scope

This page provides an overview of Slack notification delivery within the hypervel/notifications package. It covers the dual-channel architecture that supports both Slack Web API and Webhook delivery mechanisms, and explains how the system routes notifications between these channels based on routing configuration.

For detailed information about specific components:

Sources: src/Channels/SlackNotificationRouterChannel.php1-52 src/Channels/SlackWebApiChannel.php1-97 src/Channels/SlackWebhookChannel.php1-111

Slack Delivery Architecture

The Slack notification subsystem implements a dual-channel architecture that supports two distinct delivery mechanisms:

Delivery MechanismChannel ClassAuthenticationUse Case
Web APISlackWebApiChannelOAuth token (Bearer)Programmatic access to Slack workspace with bot user
WebhookSlackWebhookChannelWebhook URLSimple incoming webhooks with pre-configured channel

The SlackNotificationRouterChannel acts as the primary entry point and determines which delivery mechanism to use based on the routing information provided by the notifiable entity.

Sources: src/Channels/SlackNotificationRouterChannel.php13-52 src/Channels/SlackWebApiChannel.php16-97 src/Channels/SlackWebhookChannel.php18-111

Routing Flow


Routing Flow Diagram

This diagram shows how SlackNotificationRouterChannel inspects the routing information returned by routeNotificationFor('slack') and delegates to the appropriate channel implementation.

Sources: src/Channels/SlackNotificationRouterChannel.php26-51

Channel Selection Logic

The SlackNotificationRouterChannel.determineChannel() method implements the routing decision logic:


Channel Selection Decision Tree

This diagram maps the exact logic in SlackNotificationRouterChannel::determineChannel() showing how route type determines channel selection.

Sources: src/Channels/SlackNotificationRouterChannel.php26-51

Routing Information Types

Notifiable entities configure Slack routing by implementing routeNotificationFor('slack', $notification). The return value determines the delivery mechanism:

Return TypeExampleSelected ChannelBehavior
falsereturn false;NoneSkips Slack delivery entirely
UriInterfacereturn new Uri('https://hooks.slack.com/...');SlackWebhookChannelPosts to webhook URL
string (URL)return 'https://hooks.slack.com/...';SlackWebhookChannelPosts to webhook URL
string (channel)return '#general'; or return 'C1234567890';SlackWebApiChannelPosts to channel via Web API
SlackRoutereturn SlackRoute::make('#alerts', 'xoxb-...');SlackWebApiChannelPosts with custom token/channel

Sources: src/Channels/SlackNotificationRouterChannel.php28-34 src/Channels/SlackWebApiChannel.php83-96

Entry Point Integration

The SlackNotificationRouterChannel is registered in the ChannelManager as the 'slack' driver. When a notification specifies 'slack' in its via() method, the NotificationSender resolves and invokes this router channel:


Slack Channel Resolution and Invocation Sequence

This sequence diagram shows how the notification system resolves the Slack router and how it delegates to concrete channel implementations.

Sources: src/Channels/SlackNotificationRouterChannel.php18-35

Message Requirements

Both Slack channels require notifications to implement a toSlack() method that returns a SlackMessage instance:


Notification and Message Class Relationships

The diagram illustrates the contract that notifications must satisfy to work with Slack channels.

Sources: src/Channels/SlackWebApiChannel.php32-39 src/Channels/SlackWebhookChannel.php31-43

Both channel implementations check for the toSlack() method at runtime and throw a RuntimeException if it is missing:

Response Handling

Both Slack channels return a Psr\Http\Message\ResponseInterface on successful delivery:

  • Web API Channel: Returns the HTTP response from chat.postMessage endpoint. Validates that the response status is 200 and the JSON response contains "ok": true. Throws RuntimeException if Slack returns an error. src/Channels/SlackWebApiChannel.php53-65

  • Webhook Channel: Returns the HTTP response from posting to the webhook URL. The Slack webhook endpoint returns simple status codes without detailed error information. src/Channels/SlackWebhookChannel.php41-43

  • Router Channel: Returns null if the routing configuration indicates delivery should be skipped (route === false), otherwise returns the response from the delegated channel. src/Channels/SlackNotificationRouterChannel.php30-34

Sources: src/Channels/SlackNotificationRouterChannel.php26-35 src/Channels/SlackWebApiChannel.php32-66 src/Channels/SlackWebhookChannel.php31-44

Dependency Injection

All three Slack channel classes are managed by the dependency injection container:

ClassConstructor DependenciesPurpose
SlackNotificationRouterChannelContainerInterface $containerResolves channel instances dynamically
SlackWebApiChannelHttpClient $client
ConfigInterface $config
Makes API requests and reads configuration
SlackWebhookChannelHttpClient $clientMakes webhook POST requests

The router uses the container to instantiate the appropriate channel based on routing logic, enabling lazy loading and proper lifecycle management.

Sources: src/Channels/SlackNotificationRouterChannel.php18-21 src/Channels/SlackWebApiChannel.php23-27 src/Channels/SlackWebhookChannel.php23-26

Configuration

The Web API channel reads default configuration from services.slack.notifications:

Configuration KeyPurposeUsed When
services.slack.notifications.bot_user_oauth_tokenDefault OAuth tokenNo token provided in routing
services.slack.notifications.channelDefault channelNo channel specified in message or route

The webhook channel requires no configuration as the webhook URL contains all necessary routing information.

Sources: src/Channels/SlackWebApiChannel.php76-78 src/Channels/SlackWebApiChannel.php89-94