LoginSignup
2
1

More than 3 years have passed since last update.

laravel5.8-slack簡単通知

Last updated at Posted at 2019-05-21

準備

まず、Slack側でWebhookUrlを作成しておきます。リンク

laravel5.8以降は、composerからSlackチャンネルのインストールが必要なので入れます。

コマンド
composer require laravel/slack-notification-channel

.envファイルにSlackに通知するため、WebhookUrlとChannelを指定する。

.env
SLACK_CHANNEL=#test
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/自分用

設定を変えたので、反映させるためにキャッシュを消します。

コマンド
php artisan config:cache

作成

Notification

laravelの機能のNotificationを利用する。

コマンド
php artisan make:notification SlackNotification
SlackNotification
<?php

namespace App\Notifications;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;

class SlackNotification extends Notification
{
    use Queueable;

    protected $content;
    protected $channel;

    public function __construct($message)
    {
        $this->channel = env('SLACK_CHANNEL');
        $this->content = $message;
    }

    public function via($notifiable)
    {
        return ['slack'];
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

    public function toSlack($notifiable)
    {
        return (new SlackMessage)
            ->from('管理者')
            ->image('https://larapet.hinaloe.net/wp-content/uploads/sites/15/2017/03/cropped-86f8cf99663ac01836df2fa9c56890ff-1.png')
            ->to($this->channel)
            ->content('Hello')
            ->attachment(function (SlackAttachment $attachment) {
                $attachment
                    ->title('ユーザー', '')
                    ->field('名前', 'testUser')
                    ->field('メールアドレス', 'test@co.jp')
                    ->timestamp(Carbon::now());

            });
    }
}

SlackAttachmentの参考記事・SlackAttachmentチートシート

viaメソッド どこのチャンネルに飛ばすか
toSlackメソッド もしfromの名前を動的に変えたいなどがあれば、constructで指定します。
SlackAttachmentを指定するといろいろ通知を工夫できます。

Nortificableをuseしたクラスを用意する。

use Notifiableを記述したクラスならなんでもOK。Modelだとデフォルトでそうなっている場合があるので、基本そこの指定なのかな(しらない) 今回は、別クラスを用意してみます。

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

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

class SlackService
{
    use Notifiable;

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

    protected function routeNotificationForSlack()
    {
        return env('SLACK_WEBHOOK_URL');
    }
}

routeNotificationForSlack routeNotificationFor○○の○○の部分で書かれたサービス名をもとに、Laravelが自動的にドライバ情報を読み込んでくれる
send $this->notifyで、SlackNotificationを発火させてる。

これで基本準備は完了したので、てきとうなRoute作って呼び出してみる。

web.php
Route::get('/slackTest', function (){
    (new \App\Services\SlackService())->send();
});

Screenshot_1.png

こんな感じで出れば完了。

参考にさせていただいた記事

https://www.ritolab.com/entry/110
https://qiita.com/10monori/items/a78fb7e270b141c793b1

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1