mwakaambrose / laravel-slack-alert
Send a message to Slack
Requires
- php: ^8.0
- illuminate/contracts: ^8.73|^9.0
- slack-php/slack-block-kit: 1.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^5.10|^6.0
- nunomaduro/larastan: ^1.0|2.0
- orchestra/testbench: ^6.22
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
README
This package can quickly send alerts to Slack. You can use this to notify yourself of any noteworthy events happening in your app.
use MwakaAmbrose\SlackAlert\Facades\SlackAlert; SlackAlert::string("You have a new subscriber to the {$newsletter->name} newsletter!");
Under the hood, a job is used to communicate with Slack. This prevents your app from failing in case Slack is down.
Installation
You can install the package via composer:
composer require mwakaambrose/laravel-slack-alert
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-alert-config"
This is the contents of the published config file:
return [ /* * The webhook URLs that we'll use to send a message to Slack. */ 'webhook_urls' => [ 'default' => env('SLACK_ALERT_WEBHOOK'), ], ];
Usage
To send a message to Slack, simply call SlackAlert::string()
and pass it any message you want.
SlackAlert::string("You have a new subscriber to the {$newsletter->name} newsletter!");
Using multiple webhooks
You can also use an alternative webhook, by specify extra ones in the config file.
// in config/slack-alert.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 the to
function.
use MwakaAmbrose\SlackAlert\Facades\SlackAlert; SlackAlert::to('marketing')->string("You have a new subscriber to the {$newsletter->name} newsletter!");
Using a custom webhooks
The to
function also supports custom webhook urls.
use MwakaAmbrose\SlackAlert\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 MwakaAmbrose\SlackAlert\Facades\SlackAlert; SlackAlert::string("A message *with some bold statements* and _some italicized text_.");
Links are formatted differently in Slack than the classic markdown structure.
SlackAlert::string("<https://theonehq.com|This is a link to our homepage>");
Emoji's
You can use the same emoji codes as in Slack. This means custom emoji's are also supported.
use MwakaAmbrose\SlackAlert\Facades\SlackAlert; SlackAlert::string(":smile: :custom-code:");
Mentioning
You can use mentions to notify users and groups. Learn how in the Slack API docs.
use MwakaAmbrose\SlackAlert\Facades\SlackAlert; SlackAlert::string("A message that notifies <@username> and everyone else who is <!here>")
Sending an exception
Yo can send an exception to slack by calling the exception function and passing in any instance of throwable. You will receive the message, file, line number and apps environment on slack.
SlackAlert::to("failed_vsla_wallet")->exception(new \Exception("This is a test exception"));
More advanced
You can pass a callback function the returns an instance of SlackPhp\Blockkit\Surfaces\Message
to construct your own robust message blocks in slack. See the packaages home for more information.
SlackAlert::to('failed_vsla_wallet')->send(function(){ $msg = Message::new(); // Then you can add blocks using the surface's available methods. $msg->text('Don\'t you just love Slack?'); $msg->divider(); $msg->newImage() ->title('Team Chat') ->url('https://imgs.xkcd.com/comics/team_chat.png') ->altText('Comic about the stubbornness of some people switching chat clients'); return $msg; });
Testing
composer test
Credits
- slack-php/slack-php-block-kit
- spatie/laravel-slack-alert
- Niels Vanpachtenbeke
- Freek Van der Herten
- All Contributors
License
The MIT License (MIT). Please see License File for more information.