added embedded Discord messages logic
This commit is contained in:
70
app/Dto/Notification/DiscordMessage.php
Normal file
70
app/Dto/Notification/DiscordMessage.php
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Dto\Notification;
|
||||||
|
|
||||||
|
class DiscordMessage
|
||||||
|
{
|
||||||
|
private array $fields = [];
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public string $title,
|
||||||
|
public string $description,
|
||||||
|
public int $color,
|
||||||
|
public bool $isCritical = false,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public static function successColor(): int
|
||||||
|
{
|
||||||
|
return hexdec('a1ffa5');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function warningColor(): int
|
||||||
|
{
|
||||||
|
return hexdec('ffa743');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function errorColor(): int
|
||||||
|
{
|
||||||
|
return hexdec('ff705f');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addField(string $name, string $value): self
|
||||||
|
{
|
||||||
|
$this->fields[] = [
|
||||||
|
'name' => $name,
|
||||||
|
'value' => $value,
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toPayload(): array
|
||||||
|
{
|
||||||
|
$payload = [
|
||||||
|
'embeds' => [
|
||||||
|
[
|
||||||
|
'title' => $this->title,
|
||||||
|
'description' => $this->description,
|
||||||
|
'color' => $this->color,
|
||||||
|
'fields' => $this->addTimestampToFields($this->fields),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($this->isCritical) {
|
||||||
|
$payload['content'] = '@here';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addTimestampToFields(array $fields): array
|
||||||
|
{
|
||||||
|
$fields[] = [
|
||||||
|
'name' => 'Time',
|
||||||
|
'value' => '<t:'.now()->timestamp.':R>',
|
||||||
|
];
|
||||||
|
|
||||||
|
return $fields;
|
||||||
|
}
|
||||||
|
}
|
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Jobs;
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Dto\Notification\DiscordMessage;
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
@@ -29,7 +30,7 @@ class SendMessageToDiscordJob implements ShouldBeEncrypted, ShouldQueue
|
|||||||
public int $maxExceptions = 5;
|
public int $maxExceptions = 5;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $text,
|
public DiscordMessage $message,
|
||||||
public string $webhookUrl
|
public string $webhookUrl
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -38,9 +39,6 @@ class SendMessageToDiscordJob implements ShouldBeEncrypted, ShouldQueue
|
|||||||
*/
|
*/
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
$payload = [
|
Http::post($this->webhookUrl, $this->message->toPayload());
|
||||||
'content' => $this->text,
|
|
||||||
];
|
|
||||||
Http::post($this->webhookUrl, $payload);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,7 @@ class DiscordChannel
|
|||||||
*/
|
*/
|
||||||
public function send(SendsDiscord $notifiable, Notification $notification): void
|
public function send(SendsDiscord $notifiable, Notification $notification): void
|
||||||
{
|
{
|
||||||
$message = $notification->toDiscord($notifiable);
|
$message = $notification->toDiscord();
|
||||||
$webhookUrl = $notifiable->routeNotificationForDiscord();
|
$webhookUrl = $notifiable->routeNotificationForDiscord();
|
||||||
if (! $webhookUrl) {
|
if (! $webhookUrl) {
|
||||||
return;
|
return;
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Notifications;
|
namespace App\Notifications;
|
||||||
|
|
||||||
|
use App\Dto\Notification\DiscordMessage;
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use Illuminate\Notifications\Messages\MailMessage;
|
use Illuminate\Notifications\Messages\MailMessage;
|
||||||
@@ -29,11 +30,15 @@ class Test extends Notification implements ShouldQueue
|
|||||||
return $mail;
|
return $mail;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toDiscord(): string
|
public function toDiscord(): DiscordMessage
|
||||||
{
|
{
|
||||||
$message = 'Coolify: This is a test Discord notification from Coolify.';
|
$message = new DiscordMessage(
|
||||||
$message .= "\n\n";
|
title: 'Coolify: This is a test Discord notification from Coolify.',
|
||||||
$message .= '[Go to your dashboard]('.base_url().')';
|
description: 'This is a test Discord notification from Coolify.',
|
||||||
|
color: DiscordMessage::successColor(),
|
||||||
|
);
|
||||||
|
|
||||||
|
$message->addField('Link', '[Go to your dashboard]('.base_url().')');
|
||||||
|
|
||||||
return $message;
|
return $message;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user