Skip to content

Commit

Permalink
feat: add pushover notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
zaosoula committed Apr 27, 2024
1 parent b71f1a7 commit b60d447
Show file tree
Hide file tree
Showing 27 changed files with 489 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/Console/Commands/NotifyDemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private function showHelp()
<li>slack</li>
<li>discord</li>
<li>telegram</li>
<li>pushover</li>
</ul>
</div>
</div>
Expand All @@ -72,6 +73,6 @@ private function showHelp()
<div class="mr-1">
In which manner you wish a <strong class="text-coolify">coolified</strong> notification?
</div>
HTML, ['email', 'slack', 'discord', 'telegram']);
HTML, ['email', 'slack', 'discord', 'telegram', 'pushover']);
}
}
65 changes: 65 additions & 0 deletions app/Jobs/SendMessageToPushoverJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class SendMessageToPushoverJob implements ShouldQueue, ShouldBeEncrypted
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 5;

/**
* The maximum number of unhandled exceptions to allow before failing.
*/
public int $maxExceptions = 3;

public function __construct(
public string $text,
public array $buttons,
public string $token,
public string $user,
) {
}

/**
* Execute the job.
*/
public function handle(): void
{
$message = "<p>".$this->text."</p>";

if (!empty($this->buttons)) {
foreach ($this->buttons as $button) {
$buttonUrl = data_get($button, 'url');
$text = data_get($button, 'text', 'Click here');
if ($buttonUrl && Str::contains($buttonUrl, 'http://localhost')) {
$buttonUrl = str_replace('http://localhost', config('app.url'), $buttonUrl);
}
$message .= "&nbsp;<a href='".$buttonUrl."'>".$text."</a>";
}
}

$payload = [
'token' => $this->token,
'user' => $this->user,
'message' => $message,
'html' => 1
];
ray($payload);
Http::post("https://api.pushover.net/1/messages.json", $payload);
}
}
66 changes: 66 additions & 0 deletions app/Livewire/Notifications/Pushover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Livewire\Notifications;

use App\Models\Team;
use App\Notifications\Test;
use Livewire\Component;
use Illuminate\Support\Facades\Log;

class Pushover extends Component
{

public Team $team;
protected $rules = [
'team.pushover_enabled' => 'nullable|boolean',
'team.pushover_token' => 'required|string',
'team.pushover_user' => 'required|string',
'team.pushover_notifications_test' => 'nullable|boolean',
'team.pushover_notifications_deployments' => 'nullable|boolean',
'team.pushover_notifications_status_changes' => 'nullable|boolean',
'team.pushover_notifications_database_backups' => 'nullable|boolean',
];
protected $validationAttributes = [
'team.pushover_token' => 'Token',
'team.pushover_user' => 'User Key',
];

public function mount()
{
$this->team = auth()->user()->currentTeam();
}
public function instantSave()
{
try {
$this->submit();
} catch (\Throwable $e) {
ray($e->getMessage());
$this->team->pushover_enabled = false;
$this->validate();
}
}

public function submit()
{
$this->resetErrorBag();
$this->validate();
$this->saveModel();
}

public function saveModel()
{
$this->team->save();
refreshSession();
$this->dispatch('success', 'Settings saved.');
}

public function sendTestNotification()
{
$this->team?->notify(new Test());
$this->dispatch('success', 'Test notification sent.');
}
public function render()
{
return view('livewire.notifications.pushover');
}
}
16 changes: 13 additions & 3 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use App\Notifications\Channels\SendsDiscord;
use App\Notifications\Channels\SendsEmail;
use App\Notifications\Channels\SendsPushover;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Team extends Model implements SendsDiscord, SendsEmail
class Team extends Model implements SendsDiscord, SendsEmail, SendsPushover
{
use Notifiable;

Expand Down Expand Up @@ -37,7 +38,15 @@ public function routeNotificationForTelegram()
{
return [
"token" => data_get($this, 'telegram_token', null),
"chat_id" => data_get($this, 'telegram_chat_id', null),
"user_key" => data_get($this, 'telegram_user_key', null),
];
}

public function routeNotificationForPushover()
{
return [
"token" => data_get($this, 'pushover_token', null),
"user" => data_get($this, 'pushover_user', null),
];
}

Expand Down Expand Up @@ -179,7 +188,8 @@ public function isAnyNotificationEnabled()
if (isCloud()) {
return true;
}
if ($this->smtp_enabled || $this->resend_enabled || $this->discord_enabled || $this->telegram_enabled || $this->use_instance_email_settings) {

if ($this->smtp_enabled || $this->resend_enabled || $this->discord_enabled || $this->telegram_enabled || $this->pushover_enabled || $this->use_instance_email_settings) {
return true;
}
return false;
Expand Down
19 changes: 19 additions & 0 deletions app/Notifications/Application/DeploymentFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,23 @@ public function toTelegram(): array
],
];
}

public function toPushover(): array
{
if ($this->preview) {
$message = 'Coolify: Pull request #' . $this->preview->pull_request_id . ' of **' . $this->application_name . '** (' . $this->preview->fqdn . ') deployment failed: ';
} else {
$message = 'Coolify: Deployment failed of **' . $this->application_name . '** (' . $this->fqdn . '): ';
}
$buttons[] = [
"text" => "Deployment logs",
"url" => $this->deployment_url
];
return [
"message" => $message,
"buttons" => [
...$buttons
],
];
}
}
31 changes: 31 additions & 0 deletions app/Notifications/Application/DeploymentSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,35 @@ public function toTelegram(): array
],
];
}

public function toPushover(): array
{
if ($this->preview) {
$message = 'Coolify: New PR' . $this->preview->pull_request_id . ' version successfully deployed of ' . $this->application_name . '';
if ($this->preview->fqdn) {
$buttons[] = [
"text" => "Open Application",
"url" => $this->preview->fqdn
];
}
} else {
$message = '✅ New version successfully deployed of ' . $this->application_name . '';
if ($this->fqdn) {
$buttons[] = [
"text" => "Open Application",
"url" => $this->fqdn
];
}
}
$buttons[] = [
"text" => "Deployment logs",
"url" => $this->deployment_url
];
return [
"message" => $message,
"buttons" => [
...$buttons
],
];
}
}
14 changes: 14 additions & 0 deletions app/Notifications/Application/StatusChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ public function toTelegram(): array
],
];
}

public function toPushover(): array
{
$message = 'Coolify: ' . $this->resource_name . ' has been stopped.';
return [
"message" => $message,
"buttons" => [
[
"text" => "Open Application in Coolify",
"url" => $this->resource_url
]
],
];
}
}
24 changes: 24 additions & 0 deletions app/Notifications/Channels/PushoverChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Notifications\Channels;

use App\Jobs\SendMessageToPushoverJob;
use Illuminate\Notifications\Notification;

class PushoverChannel
{
public function send(SendsDiscord $notifiable, Notification $notification): void
{
$data = $notification->toPushover($notifiable);
$pushoverData = $notifiable->routeNotificationForPushover();
$message = data_get($data, 'message');
$buttons = data_get($data, 'buttons', []);
$pushoverToken = data_get($pushoverData, 'token');
$pushoverUser = data_get($pushoverData, 'user');

if (!$pushoverToken || !$pushoverUser || !$message) {
return;
}
dispatch(new SendMessageToPushoverJob($message, $buttons, $pushoverToken, $pushoverUser));
}
}
8 changes: 8 additions & 0 deletions app/Notifications/Channels/SendsPushover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Notifications\Channels;

interface SendsPushover
{
public function routeNotificationForPushover();
}
19 changes: 19 additions & 0 deletions app/Notifications/Container/ContainerRestarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@ public function toTelegram(): array
};
return $payload;
}

public function toPushover(): array
{
$message = "Coolify: A resource ({$this->name}) has been restarted automatically on {$this->server->name}";
$payload = [
"message" => $message,
];
if ($this->url) {
$payload['buttons'] = [
[
[
"text" => "Check Proxy in Coolify",
"url" => $this->url
]
]
];
};
return $payload;
}
}
19 changes: 19 additions & 0 deletions app/Notifications/Container/ContainerStopped.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,23 @@ public function toTelegram(): array
}
return $payload;
}

public function toPushover(): array
{
$message = "Coolify: A resource ($this->name) has been stopped unexpectedly on {$this->server->name}";
$payload = [
"message" => $message,
];
if ($this->url) {
$payload['buttons'] = [
[
[
"text" => "Open Application in Coolify",
"url" => $this->url
]
]
];
}
return $payload;
}
}
10 changes: 9 additions & 1 deletion app/Notifications/Database/BackupFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\ScheduledDatabaseBackup;
use App\Notifications\Channels\DiscordChannel;
use App\Notifications\Channels\PushoverChannel;
use App\Notifications\Channels\TelegramChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -29,7 +30,7 @@ public function __construct(ScheduledDatabaseBackup $backup, public $database, p

public function via(object $notifiable): array
{
return [DiscordChannel::class, TelegramChannel::class, MailChannel::class];
return [DiscordChannel::class, TelegramChannel::class, MailChannel::class, PushoverChannel::class];
}

public function toMail(): MailMessage
Expand All @@ -56,4 +57,11 @@ public function toTelegram(): array
"message" => $message,
];
}

public function toPushover(): array
{
return [
"message" => "Coolify: Database backup for {$this->name} with frequency of {$this->frequency} was FAILED.\n\nReason: {$this->output}",
];
}
}
7 changes: 7 additions & 0 deletions app/Notifications/Database/BackupSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ public function toTelegram(): array
"message" => $message,
];
}

public function toPushover(): array
{
return [
"message" => "Coolify: Database backup for {$this->name} with frequency of {$this->frequency} was successful.",
];
}
}
Loading

0 comments on commit b60d447

Please sign in to comment.