feat: new telegram UI

This commit is contained in:
peaklabs-dev
2024-12-09 16:49:09 +01:00
parent b4b48692af
commit bc726ddd6e
2 changed files with 186 additions and 95 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Livewire\Notifications; namespace App\Livewire\Notifications;
use App\Models\Team; use App\Models\Team;
use App\Models\TelegramNotificationSettings;
use App\Notifications\Test; use App\Notifications\Test;
use Livewire\Attributes\Validate; use Livewire\Attributes\Validate;
use Livewire\Component; use Livewire\Component;
@@ -11,6 +12,8 @@ class Telegram extends Component
{ {
public Team $team; public Team $team;
public TelegramNotificationSettings $settings;
#[Validate(['boolean'])] #[Validate(['boolean'])]
public bool $telegramEnabled = false; public bool $telegramEnabled = false;
@@ -21,42 +24,76 @@ class Telegram extends Component
public ?string $telegramChatId = null; public ?string $telegramChatId = null;
#[Validate(['boolean'])] #[Validate(['boolean'])]
public bool $telegramNotificationsTest = false; public bool $deploymentSuccessTelegramNotifications = false;
#[Validate(['boolean'])] #[Validate(['boolean'])]
public bool $telegramNotificationsDeployments = false; public bool $deploymentFailureTelegramNotifications = true;
#[Validate(['boolean'])] #[Validate(['boolean'])]
public bool $telegramNotificationsStatusChanges = false; public bool $statusChangeTelegramNotifications = false;
#[Validate(['boolean'])] #[Validate(['boolean'])]
public bool $telegramNotificationsDatabaseBackups = false; public bool $backupSuccessTelegramNotifications = false;
#[Validate(['boolean'])] #[Validate(['boolean'])]
public bool $telegramNotificationsScheduledTasks = false; public bool $backupFailureTelegramNotifications = true;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsTestMessageThreadId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsDeploymentsMessageThreadId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsStatusChangesMessageThreadId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsDatabaseBackupsMessageThreadId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsScheduledTasksThreadId = null;
#[Validate(['boolean'])] #[Validate(['boolean'])]
public bool $telegramNotificationsServerDiskUsage = false; public bool $scheduledTaskSuccessTelegramNotifications = false;
#[Validate(['boolean'])]
public bool $scheduledTaskFailureTelegramNotifications = true;
#[Validate(['boolean'])]
public bool $dockerCleanupTelegramNotifications = false;
#[Validate(['boolean'])]
public bool $serverDiskUsageTelegramNotifications = true;
#[Validate(['boolean'])]
public bool $serverReachableTelegramNotifications = false;
#[Validate(['boolean'])]
public bool $serverUnreachableTelegramNotifications = true;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsDeploymentSuccessTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsDeploymentFailureTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsStatusChangeTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsBackupSuccessTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsBackupFailureTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsScheduledTaskSuccessTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsScheduledTaskFailureTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsDockerCleanupTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsServerDiskUsageTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsServerReachableTopicId = null;
#[Validate(['nullable', 'string'])]
public ?string $telegramNotificationsServerUnreachableTopicId = null;
public function mount() public function mount()
{ {
try { try {
$this->team = auth()->user()->currentTeam(); $this->team = auth()->user()->currentTeam();
$this->settings = $this->team->telegramNotificationSettings;
$this->syncData(); $this->syncData();
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
@@ -67,39 +104,65 @@ class Telegram extends Component
{ {
if ($toModel) { if ($toModel) {
$this->validate(); $this->validate();
$this->team->telegram_enabled = $this->telegramEnabled; $this->settings->telegram_enabled = $this->telegramEnabled;
$this->team->telegram_token = $this->telegramToken; $this->settings->telegram_token = $this->telegramToken;
$this->team->telegram_chat_id = $this->telegramChatId; $this->settings->telegram_chat_id = $this->telegramChatId;
$this->team->telegram_notifications_test = $this->telegramNotificationsTest;
$this->team->telegram_notifications_deployments = $this->telegramNotificationsDeployments; $this->settings->deployment_success_telegram_notifications = $this->deploymentSuccessTelegramNotifications;
$this->team->telegram_notifications_status_changes = $this->telegramNotificationsStatusChanges; $this->settings->deployment_failure_telegram_notifications = $this->deploymentFailureTelegramNotifications;
$this->team->telegram_notifications_database_backups = $this->telegramNotificationsDatabaseBackups; $this->settings->status_change_telegram_notifications = $this->statusChangeTelegramNotifications;
$this->team->telegram_notifications_scheduled_tasks = $this->telegramNotificationsScheduledTasks; $this->settings->backup_success_telegram_notifications = $this->backupSuccessTelegramNotifications;
$this->team->telegram_notifications_test_message_thread_id = $this->telegramNotificationsTestMessageThreadId; $this->settings->backup_failure_telegram_notifications = $this->backupFailureTelegramNotifications;
$this->team->telegram_notifications_deployments_message_thread_id = $this->telegramNotificationsDeploymentsMessageThreadId; $this->settings->scheduled_task_success_telegram_notifications = $this->scheduledTaskSuccessTelegramNotifications;
$this->team->telegram_notifications_status_changes_message_thread_id = $this->telegramNotificationsStatusChangesMessageThreadId; $this->settings->scheduled_task_failure_telegram_notifications = $this->scheduledTaskFailureTelegramNotifications;
$this->team->telegram_notifications_database_backups_message_thread_id = $this->telegramNotificationsDatabaseBackupsMessageThreadId; $this->settings->docker_cleanup_telegram_notifications = $this->dockerCleanupTelegramNotifications;
$this->team->telegram_notifications_scheduled_tasks_thread_id = $this->telegramNotificationsScheduledTasksThreadId; $this->settings->server_disk_usage_telegram_notifications = $this->serverDiskUsageTelegramNotifications;
$this->team->telegram_notifications_server_disk_usage = $this->telegramNotificationsServerDiskUsage; $this->settings->server_reachable_telegram_notifications = $this->serverReachableTelegramNotifications;
$this->team->save(); $this->settings->server_unreachable_telegram_notifications = $this->serverUnreachableTelegramNotifications;
$this->settings->telegram_notifications_deployment_success_topic_id = $this->telegramNotificationsDeploymentSuccessTopicId;
$this->settings->telegram_notifications_deployment_failure_topic_id = $this->telegramNotificationsDeploymentFailureTopicId;
$this->settings->telegram_notifications_status_change_topic_id = $this->telegramNotificationsStatusChangeTopicId;
$this->settings->telegram_notifications_backup_success_topic_id = $this->telegramNotificationsBackupSuccessTopicId;
$this->settings->telegram_notifications_backup_failure_topic_id = $this->telegramNotificationsBackupFailureTopicId;
$this->settings->telegram_notifications_scheduled_task_success_topic_id = $this->telegramNotificationsScheduledTaskSuccessTopicId;
$this->settings->telegram_notifications_scheduled_task_failure_topic_id = $this->telegramNotificationsScheduledTaskFailureTopicId;
$this->settings->telegram_notifications_docker_cleanup_topic_id = $this->telegramNotificationsDockerCleanupTopicId;
$this->settings->telegram_notifications_server_disk_usage_topic_id = $this->telegramNotificationsServerDiskUsageTopicId;
$this->settings->telegram_notifications_server_reachable_topic_id = $this->telegramNotificationsServerReachableTopicId;
$this->settings->telegram_notifications_server_unreachable_topic_id = $this->telegramNotificationsServerUnreachableTopicId;
$this->settings->save();
refreshSession(); refreshSession();
} else { } else {
$this->telegramEnabled = $this->team->telegram_enabled; $this->telegramEnabled = $this->settings->telegram_enabled;
$this->telegramToken = $this->team->telegram_token; $this->telegramToken = $this->settings->telegram_token;
$this->telegramChatId = $this->team->telegram_chat_id; $this->telegramChatId = $this->settings->telegram_chat_id;
$this->telegramNotificationsTest = $this->team->telegram_notifications_test;
$this->telegramNotificationsDeployments = $this->team->telegram_notifications_deployments;
$this->telegramNotificationsStatusChanges = $this->team->telegram_notifications_status_changes;
$this->telegramNotificationsDatabaseBackups = $this->team->telegram_notifications_database_backups;
$this->telegramNotificationsScheduledTasks = $this->team->telegram_notifications_scheduled_tasks;
$this->telegramNotificationsTestMessageThreadId = $this->team->telegram_notifications_test_message_thread_id;
$this->telegramNotificationsDeploymentsMessageThreadId = $this->team->telegram_notifications_deployments_message_thread_id;
$this->telegramNotificationsStatusChangesMessageThreadId = $this->team->telegram_notifications_status_changes_message_thread_id;
$this->telegramNotificationsDatabaseBackupsMessageThreadId = $this->team->telegram_notifications_database_backups_message_thread_id;
$this->telegramNotificationsScheduledTasksThreadId = $this->team->telegram_notifications_scheduled_tasks_thread_id;
$this->telegramNotificationsServerDiskUsage = $this->team->telegram_notifications_server_disk_usage;
}
$this->deploymentSuccessTelegramNotifications = $this->settings->deployment_success_telegram_notifications;
$this->deploymentFailureTelegramNotifications = $this->settings->deployment_failure_telegram_notifications;
$this->statusChangeTelegramNotifications = $this->settings->status_change_telegram_notifications;
$this->backupSuccessTelegramNotifications = $this->settings->backup_success_telegram_notifications;
$this->backupFailureTelegramNotifications = $this->settings->backup_failure_telegram_notifications;
$this->scheduledTaskSuccessTelegramNotifications = $this->settings->scheduled_task_success_telegram_notifications;
$this->scheduledTaskFailureTelegramNotifications = $this->settings->scheduled_task_failure_telegram_notifications;
$this->dockerCleanupTelegramNotifications = $this->settings->docker_cleanup_telegram_notifications;
$this->serverDiskUsageTelegramNotifications = $this->settings->server_disk_usage_telegram_notifications;
$this->serverReachableTelegramNotifications = $this->settings->server_reachable_telegram_notifications;
$this->serverUnreachableTelegramNotifications = $this->settings->server_unreachable_telegram_notifications;
$this->telegramNotificationsDeploymentSuccessTopicId = $this->settings->telegram_notifications_deployment_success_topic_id;
$this->telegramNotificationsDeploymentFailureTopicId = $this->settings->telegram_notifications_deployment_failure_topic_id;
$this->telegramNotificationsStatusChangeTopicId = $this->settings->telegram_notifications_status_change_topic_id;
$this->telegramNotificationsBackupSuccessTopicId = $this->settings->telegram_notifications_backup_success_topic_id;
$this->telegramNotificationsBackupFailureTopicId = $this->settings->telegram_notifications_backup_failure_topic_id;
$this->telegramNotificationsScheduledTaskSuccessTopicId = $this->settings->telegram_notifications_scheduled_task_success_topic_id;
$this->telegramNotificationsScheduledTaskFailureTopicId = $this->settings->telegram_notifications_scheduled_task_failure_topic_id;
$this->telegramNotificationsDockerCleanupTopicId = $this->settings->telegram_notifications_docker_cleanup_topic_id;
$this->telegramNotificationsServerDiskUsageTopicId = $this->settings->telegram_notifications_server_disk_usage_topic_id;
$this->telegramNotificationsServerReachableTopicId = $this->settings->telegram_notifications_server_reachable_topic_id;
$this->telegramNotificationsServerUnreachableTopicId = $this->settings->telegram_notifications_server_unreachable_topic_id;
}
} }
public function instantSave() public function instantSave()

View File

@@ -12,7 +12,7 @@
@if ($telegramEnabled) @if ($telegramEnabled)
<x-forms.button class="normal-case dark:text-white btn btn-xs no-animation btn-primary" <x-forms.button class="normal-case dark:text-white btn btn-xs no-animation btn-primary"
wire:click="sendTestNotification"> wire:click="sendTestNotification">
Send Test Notifications Send Test Notification
</x-forms.button> </x-forms.button>
@endif @endif
</div> </div>
@@ -20,7 +20,6 @@
<x-forms.checkbox instantSave="instantSaveTelegramEnabled" id="telegramEnabled" label="Enabled" /> <x-forms.checkbox instantSave="instantSaveTelegramEnabled" id="telegramEnabled" label="Enabled" />
</div> </div>
<div class="flex gap-2"> <div class="flex gap-2">
<x-forms.input type="password" autocomplete="new-password" <x-forms.input type="password" autocomplete="new-password"
helper="Get it from the <a class='inline-block underline dark:text-white' href='https://t.me/botfather' target='_blank'>BotFather Bot</a> on Telegram." helper="Get it from the <a class='inline-block underline dark:text-white' href='https://t.me/botfather' target='_blank'>BotFather Bot</a> on Telegram."
required id="telegramToken" label="Token" /> required id="telegramToken" label="Token" />
@@ -28,51 +27,80 @@
id="telegramChatId" label="Chat ID" /> id="telegramChatId" label="Chat ID" />
</div> </div>
@if ($telegramEnabled) @if ($telegramEnabled)
<h2 class="mt-4">Subscribe to events</h2> <h2 class="mt-8 mb-4">Notification Settings</h2>
<div class="flex flex-col gap-4 w-96"> <p class="mb-4">
@if (isDev()) Select events for which you would like to receive Telegram notifications.
<div class="flex flex-col"> </p>
<h4>Test Notification</h4> <div class="flex flex-col gap-4 max-w-2xl">
<x-forms.checkbox instantSave="saveModel" id="telegramNotificationsTest" label="Enabled" /> <div class="border dark:border-coolgray-300 p-4 rounded-lg">
<x-forms.input <h3 class="text-lg font-medium mb-3">Backups</h3>
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used." <div class="flex flex-col gap-1.5 pl-1">
id="telegramNotificationsTestMessageThreadId" label="Custom Topic ID" /> <h4 class="font-medium mt-2">Backup Success</h4>
<div class="pl-1">
<x-forms.checkbox instantSave="saveModel" id="backupSuccessTelegramNotifications" label="Enabled" />
<x-forms.input helper="Topic ID for backup success notifications"
id="telegramNotificationsBackupSuccessTopicId" label="Success Topic ID" />
</div>
<h4 class="font-medium mt-3">Backup Failure</h4>
<div class="pl-1">
<x-forms.checkbox instantSave="saveModel" id="backupFailureTelegramNotifications" label="Enabled" />
<x-forms.input helper="Topic ID for backup failure notifications"
id="telegramNotificationsBackupFailureTopicId" label="Failure Topic ID" />
</div>
</div> </div>
@endif
<div class="flex flex-col">
<h4>Container Status Changes</h4>
<x-forms.checkbox instantSave="saveModel" id="telegramNotificationsStatusChanges" label="Enabled" />
<x-forms.input
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used."
id="telegramNotificationsStatusChangesMessageThreadId" label="Custom Topic ID" />
</div> </div>
<div class="flex flex-col">
<h4>Application Deployments</h4> <div class="border dark:border-coolgray-300 p-4 rounded-lg">
<x-forms.checkbox instantSave="saveModel" id="telegramNotificationsDeployments" label="Enabled" /> <h3 class="text-lg font-medium mb-3">Scheduled Tasks</h3>
<x-forms.input <div class="flex flex-col gap-1.5 pl-1">
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used." <h4 class="font-medium mt-2">Scheduled Task Success</h4>
id="telegramNotificationsDeploymentsMessageThreadId" label="Custom Topic ID" /> <div class="pl-1">
<x-forms.checkbox instantSave="saveModel" id="scheduledTaskSuccessTelegramNotifications" label="Enabled" />
<x-forms.input helper="Topic ID for scheduled task success notifications"
id="telegramNotificationsScheduledTaskSuccessTopicId" label="Success Topic ID" />
</div>
<h4 class="font-medium mt-3">Scheduled Task Failure</h4>
<div class="pl-1">
<x-forms.checkbox instantSave="saveModel" id="scheduledTaskFailureTelegramNotifications" label="Enabled" />
<x-forms.input helper="Topic ID for scheduled task failure notifications"
id="telegramNotificationsScheduledTaskFailureTopicId" label="Failure Topic ID" />
</div>
</div>
</div> </div>
<div class="flex flex-col">
<h4>Database Backup Status</h4> <div class="border dark:border-coolgray-300 p-4 rounded-lg">
<x-forms.checkbox instantSave="saveModel" id="telegramNotificationsDatabaseBackups" <h3 class="text-lg font-medium mb-3">Server</h3>
label="Enabled" /> <div class="flex flex-col gap-1.5 pl-1">
<x-forms.input <h4 class="font-medium mt-2">Docker Cleanup</h4>
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used." <div class="pl-1">
id="telegramNotificationsDatabaseBackupsMessageThreadId" label="Custom Topic ID" /> <x-forms.checkbox instantSave="saveModel" id="dockerCleanupTelegramNotifications" label="Enabled" />
</div> <x-forms.input helper="Topic ID for Docker cleanup notifications"
<div class="flex flex-col"> id="telegramNotificationsDockerCleanupTopicId" label="Docker Cleanup Topic ID" />
<h4>Scheduled Tasks Status</h4> </div>
<x-forms.checkbox instantSave="saveModel" id="telegramNotificationsScheduledTasks"
label="Enabled" /> <h4 class="font-medium mt-3">Server Disk Usage</h4>
<x-forms.input <div class="pl-1">
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used." <x-forms.checkbox instantSave="saveModel" id="serverDiskUsageTelegramNotifications" label="Enabled" />
id="telegramNotificationsScheduledTasksMessageThreadId" label="Custom Topic ID" /> <x-forms.input helper="Topic ID for disk usage notifications"
</div> id="telegramNotificationsServerDiskUsageTopicId" label="Disk Usage Topic ID" />
<div class="flex flex-col"> </div>
<h4>Server Disk Usage</h4>
<x-forms.checkbox instantSave="saveModel" id="telegramNotificationsServerDiskUsage" <h4 class="font-medium mt-3">Server Reachable</h4>
label="Enabled" /> <div class="pl-1">
<x-forms.checkbox instantSave="saveModel" id="serverReachableTelegramNotifications" label="Enabled" />
<x-forms.input helper="Topic ID for server reachable notifications"
id="telegramNotificationsServerReachableTopicId" label="Server Reachable Topic ID" />
</div>
<h4 class="font-medium mt-3">Server Unreachable</h4>
<div class="pl-1">
<x-forms.checkbox instantSave="saveModel" id="serverUnreachableTelegramNotifications" label="Enabled" />
<x-forms.input helper="Topic ID for server unreachable notifications"
id="telegramNotificationsServerUnreachableTopicId" label="Server Unreachable Topic ID" />
</div>
</div>
</div> </div>
</div> </div>
@endif @endif