A package to quickly send Slack alerts in a Laravel app
Our team has released a small package called spatie/laravel-slack-alerts. It can be used to send a message to Slack quickly. In this blog post, I'd like to share why we've created this package and how you can use it.
Why we've created this package
At Spatie, we've created several applications where we as a team are interested in certain events that occur. For instance, we like to know whenever someone subscribes to Flare. There's a dedicated
#flare-hooks channel in our Slack where we get these alerts.
Sending such "system" alerts via Laravel's otherwise excellent notification system is a bit cumbersome. You need to create a notifiable (or use the anonymous one) and create the notification itself.
You might think that the alternative could be to use one of the available Slack packages and manually send a request to Slack. That would work, but ideally, calling Slack should happen in a job so that our users don't see any errors in their web requests when Slack is down.
Send Slack alerts
Using our package, you can conveniently send a message to Slack. Once you've set up the Slack webhook in the config file, you can send an alert using this code:
Behind the scenes, this code will dispatch a job to send that message to Slack. So, it's safe to use this code in a web request.
You can also use multiple channels:
SlackAlert::to('marketing')->message('We got a new subscriber.');
And that's all there is to this package.
We've tried to make the package as simple as possible. We could have added more features, such as message formatting, or support for other platforms besides Slack, but we opted to keep it as simple as it can be.
Take a look at the readme on GitHub to know more about spatie/laravel-slack-alerts. This package was created by my colleague Niels. Head over to our website to see a list of other packages our team created previously.
What are your thoughts on "A package to quickly send Slack alerts in a Laravel app"?