Laravel: Notification to Slack

By | February 2, 2018

Assalamualaikum / Hi,

Today I will show you how to make a notification to Slack channel.

The approach slightly different you may came across on the internet, but they key point, still rely on Laravel Notification. It’s just how I manage my Slack service.

Configuration

First, we going to start with setting up our environment for Slack Notification by adding the Slack service configuration. Add new services in config/services.php:

Then create a webhook URL in Incoming Webhooks.

In your .env, add SLACK_WEBHOOK_URL, and add the webhook you just created above.

Once you’re done, in your terminal, don’t forget to run php artisan config:cache.

Service

Next step is to setup our Slack service and helper.

The service basically it’s just a simple class to have a webhook URL store in it, and a helper make it simpler to call the Slack service.

We going to create a service class in app/Services/Slack.php:

Then create a helper in app/Support, call it helpers.php. Then add the following and then make sure in composer.json, to load the app/Support/helpers.php:

Then composer dumpautoload -o once you have added the helper.

Notifiable Trait

Laravel provide a trait Illuminate\Notifications\Notifiable, which you can use it anywhere in any classes you want.

In our case, I want to create a class called Notify.php in app/Models.

Did you notice how I use slack() helper to get the Slack Webhook URL? Much convenient. You may want to extend the Slack service class in future, but in our case, we don’t need any as for now.

But again, you can simplify that by using env() function to get the Slack Webhook URL instead of having configuration, helper and Slack service class.

This is just my practice when dealing with 3rd party service – configuration, service class and a helper.

Don’t forget to composer dumpautoload -o once created the class.

Do take note the routeNotificationForSlack(), that is required in order Slack to work. Next will see how to notify to Slack channel.

Notification

This it the last section – now go and create a notification class via artisan – php artisan make:notification NotifyToSlackChannel.

Open up the NotifyToSlackChannel class and do two things:

One, update the via() method to accept only slack in the array.

Two, add a new method called toSlack($notification) and use \Illuminate\Notifications\Messages\SlackMessage to send messages to Slack channel.

Both steps above in following gist:

Let’s try!

So, you are ready to send messages to your Slack channel!

The fastest way to test your Slack notification is using tinker.

Here how I did:

Save it to tinker/notify-to-slack.php.

Then run php artisan tinker tinker/notify-to-slack.php.

That’s it! Working great!

User Cases

Some of user cases might need this type of notification:

  1. Helpdesk system – quick response from support team when get notify on new issues raised.
  2. E-commerce – placing new order, successful payment, out of stock items.
  3. Attendance system – you want know what time your employee come to your office (of course involve some other integration)

And I believe, it’s quite a lot of user cases can be implement, especially those involved with support, request, management.

Feel free to share your idea how we can implement more with Laravel Notification and Slack. šŸ™‚

Happy coding guys!

2 thoughts on “Laravel: Notification to Slack

    1. nasrulhazim.m Post author

      what’s the issue with the php artisan config:cache? any error messages?

      Reply

Leave a Reply to nasrulhazim.m Cancel reply

Your email address will not be published. Required fields are marked *

7 + 4 =