coolify/app/Notifications/Container/ContainerRestarted.php

87 lines
2.4 KiB
PHP
Raw Normal View History

2023-09-14 10:45:50 +00:00
<?php
namespace App\Notifications\Container;
use App\Models\Server;
use App\Notifications\CustomEmailNotification;
use App\Notifications\Dto\DiscordMessage;
2024-11-12 21:37:55 +00:00
use App\Notifications\Dto\SlackMessage;
2024-12-09 15:57:15 +00:00
use Illuminate\Notifications\Messages\MailMessage;
2023-09-14 10:45:50 +00:00
class ContainerRestarted extends CustomEmailNotification
2023-09-14 10:45:50 +00:00
{
2024-11-12 21:37:55 +00:00
public function __construct(public string $name, public Server $server, public ?string $url = null)
{
$this->onQueue('high');
2024-11-12 21:37:55 +00:00
}
2023-09-14 10:45:50 +00:00
public function via(object $notifiable): array
{
2024-12-09 15:57:15 +00:00
return $notifiable->getEnabledChannels('status_change');
2023-09-14 10:45:50 +00:00
}
public function toMail(): MailMessage
{
2024-07-24 12:27:21 +00:00
$mail = new MailMessage;
2024-01-07 15:23:41 +00:00
$mail->subject("Coolify: A resource ({$this->name}) has been restarted automatically on {$this->server->name}");
2023-09-14 10:45:50 +00:00
$mail->view('emails.container-restarted', [
'containerName' => $this->name,
'serverName' => $this->server->name,
'url' => $this->url,
2023-09-14 10:45:50 +00:00
]);
2024-06-10 20:43:34 +00:00
2023-09-14 10:45:50 +00:00
return $mail;
}
public function toDiscord(): DiscordMessage
2023-09-14 10:45:50 +00:00
{
$message = new DiscordMessage(
2024-10-21 20:40:43 +00:00
title: ':warning: Resource restarted',
description: "{$this->name} has been restarted automatically on {$this->server->name}.",
color: DiscordMessage::infoColor(),
);
2024-06-10 20:43:34 +00:00
2024-10-21 20:40:43 +00:00
if ($this->url) {
2024-12-09 15:57:15 +00:00
$message->addField('Resource', '[Link]('.$this->url.')');
2024-10-21 20:40:43 +00:00
}
2023-09-14 10:45:50 +00:00
return $message;
}
2024-06-10 20:43:34 +00:00
2023-09-14 10:45:50 +00:00
public function toTelegram(): array
{
2024-01-07 15:23:41 +00:00
$message = "Coolify: A resource ({$this->name}) has been restarted automatically on {$this->server->name}";
2023-09-14 10:45:50 +00:00
$payload = [
2024-06-10 20:43:34 +00:00
'message' => $message,
2023-09-14 10:45:50 +00:00
];
if ($this->url) {
$payload['buttons'] = [
[
[
2024-06-10 20:43:34 +00:00
'text' => 'Check Proxy in Coolify',
'url' => $this->url,
],
],
2023-09-14 10:45:50 +00:00
];
2024-06-10 20:43:34 +00:00
}
2023-09-14 10:45:50 +00:00
return $payload;
}
2024-11-12 21:37:55 +00:00
public function toSlack(): SlackMessage
{
2024-12-09 15:57:15 +00:00
$title = 'Resource restarted';
2024-11-12 21:37:55 +00:00
$description = "A resource ({$this->name}) has been restarted automatically on {$this->server->name}";
if ($this->url) {
$description .= "\n**Resource URL:** {$this->url}";
}
return new SlackMessage(
title: $title,
description: $description,
color: SlackMessage::warningColor()
);
}
2023-09-14 10:45:50 +00:00
}