VOOZH about

URL: https://deepwiki.com/hypervel/notifications/5.3.1-slack-routing

⇱ Slack Routing | hypervel/notifications | DeepWiki


Loading...
Menu

Slack Routing

Purpose and Scope

This document explains how the SlackNotificationRouterChannel selects between Slack's Web API and Webhook delivery mechanisms based on routing configuration provided by notifiable entities. This is a routing layer that sits between the notification system and the actual Slack delivery channels.

For details on the Web API delivery mechanism, see Slack Web API Channel. For details on Webhook delivery, see Slack Webhook Channel. For general information about Slack message construction, see Slack Messages.


Router Architecture

The SlackNotificationRouterChannel acts as a decision point in the Slack notification delivery pipeline. Rather than sending notifications directly, it examines routing information from the notifiable entity and delegates to the appropriate concrete channel implementation.


Sources: src/Channels/SlackNotificationRouterChannel.php1-52


Routing Decision Logic

The router implements a simple but effective decision algorithm based on the type and format of the routing value returned by the notifiable entity.

Decision Matrix

Routing ValueTypeRouting Decision
falsebooleanSkip delivery (return null)
PSR-7 UriInterface objectobjectRoute to SlackWebhookChannel
String starting with http://stringRoute to SlackWebhookChannel
String starting with https://stringRoute to SlackWebhookChannel
Any other string valuestringRoute to SlackWebApiChannel
Other valuesmixedRoute to SlackWebApiChannel (default)

The routing logic is implemented in the determineChannel() method:

src/Channels/SlackNotificationRouterChannel.php40-51

Sources: src/Channels/SlackNotificationRouterChannel.php40-51


Routing Flow

The complete routing flow from notification dispatch to channel selection follows this sequence:


Sources: src/Channels/SlackNotificationRouterChannel.php26-35 src/Channels/SlackNotificationRouterChannel.php40-51


Integration with Notifiable Entities

The router relies on the notifiable entity implementing a routeNotificationFor() method. This method receives two parameters: the channel name ('slack') and the notification instance.

Method Signature

public function routeNotificationFor(string $driver, Notification $notification): mixed

Return Value Semantics

The value returned by routeNotificationFor('slack', $notification) determines the routing behavior:

Return TypeExample ValueChannel SelectedPurpose
falsefalseNone (skip)Conditionally disable Slack notifications
UriInterfacePSR-7 URI objectWebhookDirect webhook URL object
String (URL)"https://hooks.slack.com/services/..."WebhookWebhook URL string
String (token)"xoxb-your-token"Web APIOAuth token for API authentication
String (channel)"#general" or "@username"Web APIChannel or user target

Sources: src/Channels/SlackNotificationRouterChannel.php28-34 src/Channels/SlackNotificationRouterChannel.php40-51


Implementation Details

Constructor Dependencies

The router is constructed with a single dependency:

src/Channels/SlackNotificationRouterChannel.php18-21

The ContainerInterface is used to resolve concrete channel instances on-demand, enabling lazy instantiation and proper dependency injection for the selected channel.

Send Method

The public send() method orchestrates the routing:

src/Channels/SlackNotificationRouterChannel.php26-35

Key behaviors:

  • Returns null when routing is explicitly disabled (false)
  • Delegates to the selected channel's send() method
  • Returns the ResponseInterface from the selected channel

Channel Selection Logic

The determineChannel() method implements the routing decision tree:

src/Channels/SlackNotificationRouterChannel.php40-51

The method checks conditions in this order:

  1. Is the route a UriInterface instance? → Webhook
  2. Is the route a string starting with http:// or https://? → Webhook
  3. Otherwise → Web API (default)

The Hyperf\Stringable\Str::startsWith() utility is used for URL detection.

Sources: src/Channels/SlackNotificationRouterChannel.php40-51


Routing Examples

Example 1: Webhook Routing


Result: Routes to SlackWebhookChannel

Example 2: Web API Routing with Token


Result: Routes to SlackWebApiChannel

Example 3: Web API Routing with Channel


Result: Routes to SlackWebApiChannel

Example 4: Conditional Skip


Result: Routes to SlackWebhookChannel if not in maintenance mode, otherwise skips delivery

Sources: src/Channels/SlackNotificationRouterChannel.php28-34


Router in System Context

The router channel is registered as the primary Slack channel in the notification system. When a notification specifies 'slack' in its via() method, the ChannelManager resolves the SlackNotificationRouterChannel, which then performs the routing logic.


This architecture provides:

  • Flexibility: Notifiable entities control routing per-instance
  • Consistency: Single entry point ('slack') for all Slack notifications
  • Separation: Routing logic separated from delivery implementation
  • Testability: Router can be tested independently of actual API calls

Sources: src/Channels/SlackNotificationRouterChannel.php1-52