Package

Spatie Laravel Slack Alerts

Send a message to Slack

Package

spatie/laravel-slack-alerts

View on GitHub →

Spatie Laravel Slack Alerts

The spatie/laravel-slack-alerts package is a convenient solution for sending messages to Slack directly from your Laravel applications. It simplifies the process of alerting you about important events or updates within your app through Slack notifications.

Key Features

  • Asynchronous Messaging: Utilizes Laravel jobs to ensure that application performance isn't hindered by Slack's availability.
  • Flexible Webhook Configuration: Supports multiple Slack webhooks configurable via the Laravel config file.
  • Rich Formatting: Integrates Slack's Block Kit for sending messages with rich formatting.
  • Dynamic Channel Routing: Allows messages to be sent to different channels dynamically.
  • Markdown and Emoji Support: Enhances messages with markdown syntax and emojis.
  • User and Group Mentions: Supports direct mentions of users or groups within messages.

Installation

Install the package via composer:

composer require spatie/laravel-slack-alerts

Set up your Slack webhook URL in your environment file:

SLACK_ALERT_WEBHOOK=https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ

Optionally, publish the config file for further customization:

php artisan vendor:publish --tag="slack-alerts-config"

Usage

To send a simple message:

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::message("You have a new subscriber to the {$newsletter->name} newsletter!");

For rich formatting using blocks:

SlackAlert::blocks([
    [
        "type" => "section",
        "text" => [
            "type" => "mrkdwn",
            "text" => "Welcome to the {$newsletter->name} newsletter!"
        ]
    ]
]);

To direct messages to specific or multiple webhooks and channels:

// Using a named webhook from the config
SlackAlert::to('marketing')->message("Marketing-specific message!");

// Sending to a custom webhook URL
SlackAlert::to('https://custom-url.com')->message("Custom webhook message!");

// Directing message to a specific channel
SlackAlert::toChannel('updates')->message("Message to a specific channel!");

Testing Support

The package offers facade support for mocking and asserting Slack messages in your tests, ensuring that your application reacts correctly under different scenarios.

// Example test assertion
use Spatie\SlackAlerts\Facades\SlackAlert;

it('will send an alert to Slack', function() {
    SlackAlert::shouldReceive('message')->once();
});

Additional Tools

For more complex interactions with Slack's Block Kit, slack-php/slack-php-block-kit is recommended as an alternative.

This package is part of Spatie's suite of open-source tools, and contributions or support through their channels is welcome. For more details, visit the package's GitHub repository.

Spatie Laravel Slack Alerts Related Articles

Laravel Slack Alerts Package image

Laravel Slack Alerts Package

Read article
Media Library Pro: Vue, React and Blade components to handle uploads (sponsor) image

Media Library Pro: Vue, React and Blade components to handle uploads (sponsor)

Read article
A Stand-Alone Laravel Slack Package image

A Stand-Alone Laravel Slack Package

Read article
Laravel Google Chat Alerts image

Laravel Google Chat Alerts

Read article
Laravel Slack Notification Channel Supports BlockKit API image

Laravel Slack Notification Channel Supports BlockKit API

Read article
Spatie Shares Their Coding Guidelines as AI Skills image

Spatie Shares Their Coding Guidelines as AI Skills

Read article

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift