Last active
July 21, 2022 14:12
-
-
Save freekmurze/e4415090f650e070d3de8b905875cf78 to your computer and use it in GitHub Desktop.
How to use Discord webhooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\NotificationChannels\Discord; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\RequestOptions; | |
use Illuminate\Notifications\Notification; | |
class DiscordChannel | |
{ | |
public function send($notifiable, Notification $notification) | |
{ | |
$discordMessage = $notification->toDiscord(); | |
$discordWebhook = $notifiable->destination['url']; | |
(new Client())->post($discordWebhook, [ | |
RequestOptions::JSON => $discordMessage->toArray(), | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\NotificationChannels\Discord; | |
use Carbon\Carbon; | |
class DiscordMessage | |
{ | |
const COLOR_SUCCESS = '#0B6623'; | |
const COLOR_WARNING = '#FD6A02'; | |
const COLOR_ERROR = '#ED2939'; | |
/** @var string */ | |
protected $title; | |
/** @var string */ | |
protected $description; | |
/** @var string */ | |
protected $timestamp; | |
/** @var string */ | |
protected $footer; | |
/** @var string */ | |
protected $color; | |
public function title($title) | |
{ | |
$this->title = $title; | |
return $this; | |
} | |
/** | |
* @param array|string $descriptionLines | |
* | |
* @return \App\Services\NotificationChannels\Discord\DiscordMessage | |
*/ | |
public function description($descriptionLines): self | |
{ | |
if (! is_array($descriptionLines)) { | |
$descriptionLines = [$descriptionLines]; | |
} | |
$this->description = implode(PHP_EOL, $descriptionLines); | |
return $this; | |
} | |
public function timestamp(Carbon $carbon): self | |
{ | |
$this->timestamp = $carbon->toIso8601String(); | |
return $this; | |
} | |
public function footer(string $footer): self | |
{ | |
$this->footer = $footer; | |
return $this; | |
} | |
public function success(): self | |
{ | |
$this->color = static::COLOR_SUCCESS; | |
return $this; | |
} | |
public function warning(): self | |
{ | |
$this->color = static::COLOR_WARNING; | |
return $this; | |
} | |
public function error(): self | |
{ | |
$this->color = static::COLOR_ERROR; | |
return $this; | |
} | |
public function toArray(): array | |
{ | |
return [ | |
'embeds' => [ | |
[ | |
'title' => $this->title, | |
'type' => 'rich', | |
'description' => $this->description, | |
'color' => hexdec($this->color), | |
'footer' => [ | |
'text' => $this->footer ?? '', | |
], | |
'timestamp' => $this->timestamp, | |
], | |
], | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\Checkers\Uptime\Notifications; | |
use App\Models\NotificationLogItem; | |
use App\Services\Checkers\Notification; | |
use App\Services\NotificationChannels\Discord\DiscordMessage; | |
use Carbon\Carbon; | |
class UptimeCheckFailed extends Notification | |
{ | |
... | |
public function toDiscord() | |
{ | |
return (new DiscordMessage()) | |
->error() | |
->title($this->getMainMessage()) | |
->description([ | |
$this->run->checkerResult()->getErrorDescription(), | |
'Full report: ' . $this->run->result_url, | |
]) | |
->footer("Downtime verified from {$this->run->usedUptimeCheckerLocations()}") | |
->timestamp(Carbon::now()); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment