laravelでのSlack通知実装


laravelでお問い合わせフォームを作成し、入力された内容をSlackに通知する必要があったので、そこで学習したことを残しておきます。

WebHookURLの取得

通知したいチャンネルのwebhookURLを取得する
Incoming Webhook
ここで取得したURLを.envファイルに書き込む

次に、Slack通知チャンネルをlaravelに追加。

> composer require laravel/slack-notification-channel

Notificationクラスの作成

次に、Slackに通知を送るためのNotificationクラスを作成する。

> php artisan make:Notification SlackNotification

app/Notifications/SlackNotification.phpというファイルが作られる。

app/Notifications/SlackNotification.php
<?php

namespace App\Notifications;

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

class SlackNotification extends Notification
{
    use Queueable;

    protected $message;
    protected $channel;
    protected $name;
    protected $icon;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

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

    /**
     * Get the Slack representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\SlackMessage
     */
    public function toSlack($notifiable)
    {
        return (new SlackMessage)
                    ->from($this->name)
                    ->image($this->icon)
                    ->to($this->channel)
                    ->content($this->message);
    }

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

処理本体の作成

次に、通知処理本体の作成。

app/Services/Slack/SlackService.php
<?php
namespace App\Services\Slack;

use Illuminate\Notifications\Notifiable;
use App\Notifications\SlackNotification;

class SlackService
{
  use Notifiable;

  public function send($message = null)
  {
    $this->notify(new SlackNotification($message));
  }

  protected function routeNotificationForSlack()
  {
    return config('app.slack_url');
  }
}

ファサードクラスを作成

app/Services/Slack/SlackFacade.php
<?php
namespace App\Services\Slack;

use Illuminate\Support\Facades\Facade;

class SlackFacade extends Facade
{
  protected static function getFacadeAccessor()
  {
    return 'slack';
  }
}

routeNotificationForSlackで、通知するURLを見に行っている。

プロバイダの作成

サービスプロバイダを作成する。

> php artisan make:provider SlackServiceProvider

app/Providers/Slack/SlackServiceProvider.phpというファイルが作成される。

app/Providers/Slack/SlackServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class SlackServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
          'slack',
          'App\Services\Slack\SlackService'
        );
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

サービスプロバイダ、エイリアス、envを追記する

参照するenvの値は、Notification内には書かず、config/app.phpに追加する。
これは、config:cacheされた時に、.envファイルがロードされなくなり、nullを返すため。

app/config/app.php

'slack_url' => env('SLACK_URL'),

'providers' => [
        App\Providers\SlackServiceProvider::class,
],
'aliases' => [
        'Slack' => App\Services\Slack\SlackFacade::class,
],

あとは、以下の記述でどこでもSlack通知が届くようになる。

\Slack::send('Hello World!')