Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Laravel Slack Alerts

spatie/laravel-slack-alerts image

Laravel Slack Alerts stats

Downloads
346.8K
Stars
231
Open Issues
0
Forks
25

View on GitHub →

Send a message to Slack

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 photo

We create open source, digital products and courses for the developer community


Spatie Laravel Slack Alerts Related Articles

Laravel Google Chat Alerts image

Laravel Google Chat Alerts

Read article
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

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
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Harpoon: Next generation time tracking and invoicing logo

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