Skip to content

Slack Channel ​

The slack channel delivers rich, interactive notifications to Slack channels via Incoming Webhooks utilizing Slack's Block Kit framework.


The SlackMessage Builder ​

Define a toSlack() method returning a Jengo\Notifications\Messages\SlackMessage:

php
namespace App\Notifications;

use Jengo\Notifications\Messages\SlackMessage;
use Jengo\Notifications\Notification;

class DeploymentCompleted extends Notification
{
    public function __construct(public array $deployment)
    {
        parent::__construct();
    }

    public function via(object $notifiable): array
    {
        return ['slack'];
    }

    public function toSlack(object $notifiable): SlackMessage
    {
        return (new SlackMessage("Deployment to {$this->deployment['env']} completed!"))
            ->header('🚀 Deployment Succeeded')
            ->section("Application *{$this->deployment['app']}* deployed to *{$this->deployment['env']}*.")
            ->fields([
                'Commit'     => "`{$this->deployment['commit']}`",
                'Author'     => $this->deployment['author'],
                'Duration'   => '42s',
                'Status'     => '✅ Healthy',
            ])
            ->divider()
            ->button('View Deployment', $this->deployment['url'], 'primary');
    }
}

Block Kit Formatting Helpers ​

  • header(string $text): Prominent header block.
  • section(string $mrkdwn): Markdown-formatted content section.
  • fields(array $keyValuePairs): Multi-column key/value layout.
  • button(string $text, string $url, string $style = 'primary'): Interactive button link ('primary' or 'danger').
  • divider(): Visual horizontal separator.
  • username(string $botName): Override bot display name.
  • iconUrl(string $avatarUrl): Override bot avatar icon.

Routing Slack Notifications ​

Global Webhook URL ​

Define the default incoming webhook URL in app/Config/Notifications.php:

php
public array $slack = [
    'webhookUrl' => 'https://hooks.slack.com/services/T00/B00/XXXXX',
];

Dynamic Recipient Routing ​

On an entity (such as a Team or Workspace), define routeNotificationForSlack():

php
public function routeNotificationForSlack(): string
{
    return $this->slack_webhook_url;
}

On-Demand Routing ​

php
Notification::route('slack', 'https://hooks.slack.com/services/T00/B00/XXXXX')
    ->notify(new ServerAlert('CPU load above 90%'));

Released under the MIT License.