Laravel Slack Integration

Laravel Slack Integration

----------------------------------------------------


Laravel slack integration comes under notification module in which it provides support for sending notifications across a variety of delivery channels, including mail, SMS (via Nexmo), and Slack.

Basically notifications should be short, informational messages that notify users of something that occurred in your application. 

Before moving to further step requirements for laravel slack integration

1.Slack Notification Channel Package
2.Configure Incoming Webhooks
3.Laravel slack notification setup

Setps
------

1.In this step we will install slack notification chanel package.In the older version of Laravel, Slack notification was built-in configured with the framework. But on this 5.8.x version, they have created a separate package for Slack Notification. First of all, we have to install that package in our application

composer require laravel/slack-notification-channel

2.Configure Incoming Webhooks
After installation of package we have to collect the Slack Webhook URL. So first of all, login to your slack application and click on + icon beside Apps
Click on the + icon to add the incoming-hook app to your slack application. Once you clicked on the + icon, then browse apps screen will pop up on your screen. Just search the incoming-webhook in the search area.
Once you get into that page you can create webhook url for that perticular app and you will get url like this

https://hooks.slack.com/services/BHGV655yy/gqwertyu98/mnbv3697drt

3.Add Webhook URL into your laravel application .env file

SLACK_WEBHOOK="https://hooks.slack.com/services/BHGV655yy/gqwertyu98/mnbv3697drt"

4.Laravel slack notification setup
Now we will create laravel notification module so for that we will run laravel artisan command 

php artisan make:notification MemberRegisterNotification

Once you run the above command it will create a Notifications folder inside app folder from where you can find the MemberRegisterNotification.php file 

5.Open MemberRegisterNotification.php you have to add slack package in your MemberRegisterNotification

use Illuminate\Notifications\Messages\SlackMessage;


<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;

class MemberRegisterNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail','slack'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

     /**
     * Send slack notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */

    public function toSlack($notifiable)
    {  
         return (new SlackMessage)
                 ->to('#chanel-name')
                ->content('One of your invoices has been paid!');
                
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}


IF you want to customized notification then please follow the laravel slack documentation link. 

6.Now we will trigger notification.For that first we have to include laravel notification package on top of your controller along with notification class which we want to use.

use Illuminate\Support\Facades\Notification;
use App\Notifications\MemberRegisterNotification;

Now inside your controller method you can use below code to trigger notification

Notification::route('slack', env('SLACK_WEBHOOK'))->notify(new MemberRegisterNotification());

 

Leave a reply

your email address will not be published. required fields are marked *

Name *
Email *
Website