6

Sending Slack Notification with Laravel

 3 years ago
source link: https://webomnizz.com/sending-slack-notification-with-laravel/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Laravel has lots of built-in and advanced features to make any developer impress with. Sending Notifications are one of them. You can send email, SMS or Slack notification with its built-in functionality. In this article, we going to send the slack notification with Laravel.

I am going to use Laravel 5.8.x. The setup is pretty easy. If you haven’t setup Laravel yet then please following the Laravel Installation guide from the documentation.

Requirements

Slack Notification Channel

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

Configure Incoming Webhooks

After installation of Slack Notification Channel package, we have to collect the Slack Webhook URL. So first of all, login to your slack application. Now on the main screen, navigate the App menu on the left sidebar at the very bottom.

Screenshot-2019-03-15-at-11.06.58-PM-1024x495.png
Slack App Sidebar

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

incoming-webhook-screen.png
Incoming Webhook Screen

If you haven’t installed the incoming-webhook app on the slack app then please install it. After installation, the incoming-webhook app, navigate the settings tab on the incoming-webhook section and collect the Webhook URL.

webhook-settings-screen.png

Now copy the Webhook URL and paste it to the .env file at the very bottom or whatever you like.

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

SLACK_HOOK="https://hooks.slack.com/services/TXXXXXXX/BXXXXXXX/XXXXXXXXXXXXXX"

This is what you need from the Slack app. Now it’s time to create a notification on Laravel. So let’s create our first notification.

php artisan make:notification TestNotification

The above command will create a Notifications folder on the app folder from where you can find the TestNotification file. So let’s open and start adding it.

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 TestNotification extends Notification {

Notice the SlackMessage package that we have included on our application.

use Illuminate\Notifications\Messages\SlackMessage;

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

Add the slack value in the via method’s returned array and create your first slack notification in the new method named toSlack().

public function toSlack($notifiable)
    {
        $message = "Famous Hello World!";
        
        return (new SlackMessage)
                ->from('Ghost', ':ghost:')
                ->to('#channel-name')
                ->content('Fix service request by '.$message);
    }

If you like to more customized notification then please follow the documentation link. Now its time to trigger this notification.

Notification::route('slack', env('SLACK_HOOK'))
      ->notify(new TestNotification());

Complete code for Notification

<?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 TestNotification 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!');
    }


    public function toSlack($notifiable)
    {
        $message = "Famous Hello World!";
        
        return (new SlackMessage)
                ->from('Ghost', ':ghost:')
                ->to('#channel-name')
                ->content('Fix service request by '.$message);
    }


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

Paste the above code to send the notification to your slack channel and that’s all.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK