티스토리 뷰

Laravel

[laravel8] 슬랙 알림 보내기

rediate.will 2023. 1. 16. 13:45

https://medium.com/modulr/send-slack-notifications-with-laravel-8-ff8ad1a6ae61

 

Send Slack Notifications with Laravel 8

We will learn to send Slack notifications with Laravel 8

medium.com

 

요기 참고해서 진행하면 된당!

 

중요부분은 패키지 깔고, 

 

사용하려는 모델에 use Notifiaction; 추가해주고

 

public function routeNotifiacationForSlack($notification) 메소드 추가해주면 된다

리턴값은 해당 슬랙의 서비스 income 생성한 url을 주면 된다.

 

config값에 넣어두고 return config('asdf.SLACK_WEBHOOK_URL'); 이런식으로 주고,

config폴더내 asdf 파일에 return [ 'SLACK_WEBHOOK_URL'=>'https://~~~', ]; 에 추가 해줬다.

 

 

 

실제로 사용할때는 

 

// model
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class ToSlack extends Model
{
    use HasFactory;
    use Notifiable;

    protected $table = 'to_slacks';

    public function routeNotificationForSlack($notification): string
    {
        return config('asdf.SLACK_WEBHOOK_URL');
    }
}

// notification model
namespace App\Notifications;

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

class NotifyToSlack extends Notification
{
    use Queueable;

    private string $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

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

    public function toSlack(): SlackMessage
    {
        $this->message = '오류 발생. 확인해보셔야합니다!' . $this->message;
        return (new SlackMessage())->content($this->message);
    }

}

// tinker
>> $message = '이거저거';
>> $slack = App\Models\ToSlack::find(1);
>> $slack->notify(new App\Notifications\NotifyToSlack($message));

 

슬랙으로 잘 온당!

 

 

 

+. 텔레그램에서 보내기

https://medium.com/modulr/send-telegram-notifications-with-laravel-9-342cc87b406

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함