Laravel Slack Alerts
Laravel Slack Alerts stats
- Downloads
- 346.8K
- Stars
- 231
- Open Issues
- 0
- Forks
- 25
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 configSlackAlert::to('marketing')->message("Marketing-specific message!"); // Sending to a custom webhook URLSlackAlert::to('https://custom-url.com')->message("Custom webhook message!"); // Directing message to a specific channelSlackAlert::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 assertionuse 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.