もくじ
新規Webhookの作成
リンクにアクセスしてWebHook URLを作成する
https://slack.com/services/new/incoming-webhook
- チャンネル名
sample_app_slack_test
.env
#Slack SLACK_CHANNEL='sample_app_slack_test' SLACK_NAME='sample app Job Fail Alert' SLACK_URL='https://hooks.slack.com/services/xxxxx/yyyyy/zzzzz'
SLACK_NAMEは任意で。
Slack用notificationクラスを作成
$ php artisan make:notification SlackNotification
App\Notifications\SlackNotification.php
namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; class SlackNotification 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']; } /** * 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!'); } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ // ]; } }
書き換える
namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\SlackMessage; class SlackNotification extends Notification { use Queueable; protected $channel; protected $name; protected $message; /** * Create a new notification instance. * * @return void */ public function __construct($message = null) { $this->channel = env('SLACK_CHANNEL'); $this->name = env('SLACK_NAME'); $this->message = $message; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['slack']; //slackに設定 } /** * Slack通知組み立て * * @param mixed $notifiable * @return \Illuminate\Notifications\Messages\SlackMessage */ public function toSlack($notifiable) { $message = (new SlackMessage) ->from($this->name) ->to($this->channel) ->content($this->message); return $message; } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ // ]; } }
App\Http\Util\Slack\SlackFailAlert.php
namespace App\Util\Slack; use Illuminate\Notifications\Notifiable; use App\Notifications\SlackNotification; class SlackFailAlert { use Notifiable; public function send($message = null) { $this->notify(new SlackNotification($message)); } protected function routeNotificationForSlack() { return env('SLACK_URL'); } }
利用したい箇所で
use App\Util\Slack\SlackFailAlert; ・・・ class Sample { ・・・ public function handle() { ・・・ // Slackアラート通知 $slack_fail_alert = new SlackFailAlert(); $slack_fail_alert->send('失敗しました。'); ・・・ } ・・・ }