Laravel Slack Integration

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

  1. 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

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

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

  1. 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 

  1. 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


  1. use Illuminate\Notifications\Messages\SlackMessage;  
  2.   
  3.   
  4. <?php  
  5.   
  6. namespace App\Notifications;  
  7.   
  8. use Illuminate\Bus\Queueable;  
  9. use Illuminate\Notifications\Notification;  
  10. use Illuminate\Contracts\Queue\ShouldQueue;  
  11. use Illuminate\Notifications\Messages\MailMessage;  
  12. use Illuminate\Notifications\Messages\SlackMessage;  
  13.   
  14. class MemberRegisterNotification extends Notification  
  15. {  
  16.     use Queueable;  
  17.   
  18.     /** 
  19.      * Create a new notification instance. 
  20.      * 
  21.      * @return void 
  22.      */  
  23.     public function __construct()  
  24.     {  
  25.         //  
  26.     }  
  27.   
  28.     /** 
  29.      * Get the notification's delivery channels. 
  30.      * 
  31.      * @param  mixed  $notifiable 
  32.      * @return array 
  33.      */  
  34.     public function via($notifiable)  
  35.     {  
  36.         return ['mail','slack'];  
  37.     }  
  38.   
  39.     /** 
  40.      * Get the mail representation of the notification. 
  41.      * 
  42.      * @param  mixed  $notifiable 
  43.      * @return \Illuminate\Notifications\Messages\MailMessage 
  44.      */  
  45.     public function toMail($notifiable)  
  46.     {  
  47.         return (new MailMessage)  
  48.                     ->line('The introduction to the notification.')  
  49.                     ->action('Notification Action', url('/'))  
  50.                     ->line('Thank you for using our application!');  
  51.     }  
  52.   
  53.      /** 
  54.      * Send slack notification. 
  55.      * 
  56.      * @param  mixed  $notifiable 
  57.      * @return \Illuminate\Notifications\Messages\MailMessage 
  58.      */  
  59.   
  60.     public function toSlack($notifiable)  
  61.     {    
  62.          return (new SlackMessage)  
  63.                  ->to('#chanel-name')  
  64.                 ->content('One of your invoices has been paid!');  
  65.                   
  66.     }  
  67.   
  68.     /** 
  69.      * Get the array representation of the notification. 
  70.      * 
  71.      * @param  mixed  $notifiable 
  72.      * @return array 
  73.      */  
  74.     public function toArray($notifiable)  
  75.     {  
  76.         return [  
  77.             //  
  78.         ];  
  79.     }  
  80. }  

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.


  1. use Illuminate\Support\Facades\Notification;  
  2. use App\Notifications\MemberRegisterNotification;  

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


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

 

Categories: PHP Tags: #Laravel,

Newsletter Subcribe

Receive updates and latest news direct from our team. Simply enter your email.