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

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Laravel Slack Alerts Related Articles

Laravel Google Chat Alerts image

Laravel Google Chat Alerts

Read article
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian