Blog Detail

16

Mar
Quickly send a message to Slack with Laravel Slack Alerts cover image

arrow_back Quickly send a message to Slack with Laravel Slack Alerts

Spatie has launched an amazing package called the Laravel Slack Alerts that can quickly send alerts to Slack using laravel. You can use this to notify yourself of any noteworthy events happening in your application. Under the hood, a job is used to communicate with Slack. This prevents your app from failing in case Slack is down.

use Spatie\SlackAlerts\Facades\SlackAlert;

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

Installation

You can install the package via composer by running this command in the terminal:

composer require spatie/laravel-slack-alerts

You can set a SLACK_ALERT_WEBHOOK env variable containing a valid Slack webhook URL. You can learn how to get a webhook URL in the Slack API docs.

Alternatively, you can publish the config file with:

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

Usage

To send a message to Slack, simply call SlackAlert::message() and pass it any message you want.

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

Using multiple webhooks

You can also use an alternative webhook, by specifying extra ones in the config file.

// in config/slack-alerts.php

'webhook_urls' => [
    'default' => 'https://hooks.slack.com/services/XXXXXX',
    'marketing' => 'https://hooks.slack.com/services/YYYYYY',
],

The webhook to be used can be chosen using them to function.

use Spatie\SlackAlerts\Facades\SlackAlert;

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

Using custom webhooks

The to function also supports custom webhook URLs.

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::to('https://custom-url.com')->message("You have a new subscriber to the {$newsletter->name} newsletter!");

Formatting

Markdown

You can format your messages with Slack’s markup. Learn how in the Slack API docs.

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::message("A message *with some bold statements* and _some italicized text_.");

Links are formatted differently in Slack than the classic markdown structure.

SlackAlert::message("<https://spatie.be|This is a link to our homepage>");

Emojis

You can use the same emoji codes as in Slack. This means custom emojis are also supported.

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::message(":smile: :custom-code:");

If you want to utilize this package, you can visit its complete documentation and source code on Github.

Published at : 16-03-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project