Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

added embedded Discord messages logic

Jakub Nováka2bca3d5b829
diff --git a/app/Dto/Notification/DiscordMessage.php b/app/Dto/Notification/DiscordMessage.php
new file mode 100644
index 0000000000..88feda7e30
--- /dev/null
+++ b/app/Dto/Notification/DiscordMessage.php
@@ -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;
+ }
+}
diff --git a/app/Jobs/SendMessageToDiscordJob.php b/app/Jobs/SendMessageToDiscordJob.php
index f38cf823c7..8b238a36df 100644
--- a/app/Jobs/SendMessageToDiscordJob.php
+++ b/app/Jobs/SendMessageToDiscordJob.php
@@ -2,6 +2,7 @@
namespace App\Jobs;
+use App\Dto\Notification\DiscordMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -29,7 +30,7 @@ class SendMessageToDiscordJob implements ShouldBeEncrypted, ShouldQueue
public int $maxExceptions = 5;
public function __construct(
- public string $text,
+ public DiscordMessage $message,
public string $webhookUrl
) {}
@@ -38,9 +39,6 @@ class SendMessageToDiscordJob implements ShouldBeEncrypted, ShouldQueue
*/
public function handle(): void
{
- $payload = [
- 'content' => $this->text,
- ];
- Http::post($this->webhookUrl, $payload);
+ Http::post($this->webhookUrl, $this->message->toPayload());
}
}
diff --git a/app/Notifications/Channels/DiscordChannel.php b/app/Notifications/Channels/DiscordChannel.php
index f1706f1380..3a33d89026 100644
--- a/app/Notifications/Channels/DiscordChannel.php
+++ b/app/Notifications/Channels/DiscordChannel.php
@@ -12,7 +12,7 @@ class DiscordChannel
*/
public function send(SendsDiscord $notifiable, Notification $notification): void
{
- $message = $notification->toDiscord($notifiable);
+ $message = $notification->toDiscord();
$webhookUrl = $notifiable->routeNotificationForDiscord();
if (! $webhookUrl) {
return;
diff --git a/app/Notifications/Test.php b/app/Notifications/Test.php
index 3b46a9a249..f3bb1e3f6b 100644
--- a/app/Notifications/Test.php
+++ b/app/Notifications/Test.php
@@ -2,6 +2,7 @@
namespace App\Notifications;
+use App\Dto\Notification\DiscordMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
@@ -29,11 +30,15 @@ class Test extends Notification implements ShouldQueue
return $mail;
}
- public function toDiscord(): string
+ public function toDiscord(): DiscordMessage
{
- $message = 'Coolify: This is a test Discord notification from Coolify.';
- $message .= "\n\n";
- $message .= '[Go to your dashboard]('.base_url().')';
+ $message = new DiscordMessage(
+ title: 'Coolify: This is a test Discord notification from Coolify.',
+ description: 'This is a test Discord notification from Coolify.',
+ color: DiscordMessage::successColor(),
+ );
+
+ $message->addField('Link', '[Go to your dashboard]('.base_url().')');
return $message;
}