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

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
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Shift logo

Shift

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

Shift
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Lucky Media logo

Lucky Media

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

Lucky Media
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel