coolify/app/Notifications/Channels/TelegramChannel.php

47 lines
2 KiB
PHP
Raw Normal View History

2023-09-06 12:31:38 +00:00
<?php
namespace App\Notifications\Channels;
use App\Jobs\SendMessageToTelegramJob;
class TelegramChannel
{
public function send($notifiable, $notification): void
{
$data = $notification->toTelegram($notifiable);
$telegramData = $notifiable->routeNotificationForTelegram();
$message = data_get($data, 'message');
$buttons = data_get($data, 'buttons', []);
$telegramToken = data_get($telegramData, 'token');
$chatId = data_get($telegramData, 'chat_id');
2024-05-17 10:09:22 +00:00
$topicId = null;
2023-09-08 12:15:28 +00:00
$topicsInstance = get_class($notification);
2023-09-06 12:31:38 +00:00
2023-09-08 12:15:28 +00:00
switch ($topicsInstance) {
2024-10-28 13:56:13 +00:00
case \App\Notifications\Test::class:
2023-09-08 12:15:28 +00:00
$topicId = data_get($notifiable, 'telegram_notifications_test_message_thread_id');
break;
2024-10-28 13:56:13 +00:00
case \App\Notifications\Application\StatusChanged::class:
case \App\Notifications\Container\ContainerRestarted::class:
case \App\Notifications\Container\ContainerStopped::class:
2024-05-17 10:09:22 +00:00
$topicId = data_get($notifiable, 'telegram_notifications_status_changes_message_thread_id');
break;
2024-10-28 13:56:13 +00:00
case \App\Notifications\Application\DeploymentSuccess::class:
case \App\Notifications\Application\DeploymentFailed::class:
2023-09-08 12:15:28 +00:00
$topicId = data_get($notifiable, 'telegram_notifications_deployments_message_thread_id');
break;
2024-10-28 13:56:13 +00:00
case \App\Notifications\Database\BackupSuccess::class:
case \App\Notifications\Database\BackupFailed::class:
2023-09-08 12:15:28 +00:00
$topicId = data_get($notifiable, 'telegram_notifications_database_backups_message_thread_id');
break;
2024-10-28 13:56:13 +00:00
case \App\Notifications\ScheduledTask\TaskFailed::class:
$topicId = data_get($notifiable, 'telegram_notifications_scheduled_tasks_thread_id');
break;
2023-09-08 12:15:28 +00:00
}
2024-06-10 20:43:34 +00:00
if (! $telegramToken || ! $chatId || ! $message) {
2023-09-08 14:16:28 +00:00
return;
2023-09-06 12:31:38 +00:00
}
SendMessageToTelegramJob::dispatch($message, $buttons, $telegramToken, $chatId, $topicId);
2023-09-06 12:31:38 +00:00
}
}