feat: Telegram topics separation
This commit is contained in:
parent
953bcfb5bb
commit
acd78ae196
@ -17,6 +17,10 @@ class TelegramSettings extends Component
|
|||||||
'team.telegram_notifications_deployments' => 'nullable|boolean',
|
'team.telegram_notifications_deployments' => 'nullable|boolean',
|
||||||
'team.telegram_notifications_status_changes' => 'nullable|boolean',
|
'team.telegram_notifications_status_changes' => 'nullable|boolean',
|
||||||
'team.telegram_notifications_database_backups' => 'nullable|boolean',
|
'team.telegram_notifications_database_backups' => 'nullable|boolean',
|
||||||
|
'team.telegram_notifications_test_message_thread_id' => 'nullable|string',
|
||||||
|
'team.telegram_notifications_deployments_message_thread_id' => 'nullable|string',
|
||||||
|
'team.telegram_notifications_status_changes_message_thread_id' => 'nullable|string',
|
||||||
|
'team.telegram_notifications_database_backups_message_thread_id' => 'nullable|string',
|
||||||
];
|
];
|
||||||
protected $validationAttributes = [
|
protected $validationAttributes = [
|
||||||
'team.telegram_token' => 'Token',
|
'team.telegram_token' => 'Token',
|
||||||
|
@ -31,6 +31,7 @@ class SendMessageToTelegramJob implements ShouldQueue
|
|||||||
public array $buttons,
|
public array $buttons,
|
||||||
public string $token,
|
public string $token,
|
||||||
public string $chatId,
|
public string $chatId,
|
||||||
|
public ?string $topicId = null,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +64,9 @@ class SendMessageToTelegramJob implements ShouldQueue
|
|||||||
'chat_id' => $this->chatId,
|
'chat_id' => $this->chatId,
|
||||||
'text' => $this->text,
|
'text' => $this->text,
|
||||||
];
|
];
|
||||||
ray($payload);
|
if ($this->topicId) {
|
||||||
|
$payload['message_thread_id'] = $this->topicId;
|
||||||
|
}
|
||||||
$response = Http::post($url, $payload);
|
$response = Http::post($url, $payload);
|
||||||
if ($response->failed()) {
|
if ($response->failed()) {
|
||||||
throw new \Exception('Telegram notification failed with ' . $response->status() . ' status code.' . $response->body());
|
throw new \Exception('Telegram notification failed with ' . $response->status() . ' status code.' . $response->body());
|
||||||
|
@ -28,7 +28,7 @@ class Team extends Model implements SendsDiscord, SendsEmail
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
"token" => data_get($this, 'telegram_token', null),
|
"token" => data_get($this, 'telegram_token', null),
|
||||||
"chat_id" => data_get($this, 'telegram_chat_id', null)
|
"chat_id" => data_get($this, 'telegram_chat_id', null),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,16 +10,30 @@ class TelegramChannel
|
|||||||
{
|
{
|
||||||
$data = $notification->toTelegram($notifiable);
|
$data = $notification->toTelegram($notifiable);
|
||||||
$telegramData = $notifiable->routeNotificationForTelegram();
|
$telegramData = $notifiable->routeNotificationForTelegram();
|
||||||
|
|
||||||
$message = data_get($data, 'message');
|
$message = data_get($data, 'message');
|
||||||
$buttons = data_get($data, 'buttons', []);
|
$buttons = data_get($data, 'buttons', []);
|
||||||
ray($message, $buttons);
|
|
||||||
$telegramToken = data_get($telegramData, 'token');
|
$telegramToken = data_get($telegramData, 'token');
|
||||||
$chatId = data_get($telegramData, 'chat_id');
|
$chatId = data_get($telegramData, 'chat_id');
|
||||||
|
$topicId = null;
|
||||||
|
$topicsInstance = get_class($notification);
|
||||||
|
|
||||||
|
switch ($topicsInstance) {
|
||||||
|
case 'App\Notifications\StatusChange':
|
||||||
|
$topicId = data_get($notifiable, 'telegram_notifications_status_changes_message_thread_id');
|
||||||
|
break;
|
||||||
|
case 'App\Notifications\Test':
|
||||||
|
$topicId = data_get($notifiable, 'telegram_notifications_test_message_thread_id');
|
||||||
|
break;
|
||||||
|
case 'App\Notifications\Deployment':
|
||||||
|
$topicId = data_get($notifiable, 'telegram_notifications_deployments_message_thread_id');
|
||||||
|
break;
|
||||||
|
case 'App\Notifications\DatabaseBackup':
|
||||||
|
$topicId = data_get($notifiable, 'telegram_notifications_database_backups_message_thread_id');
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (!$telegramToken || !$chatId || !$message) {
|
if (!$telegramToken || !$chatId || !$message) {
|
||||||
throw new \Exception('Telegram token, chat id and message are required');
|
throw new \Exception('Telegram token, chat id and message are required');
|
||||||
}
|
}
|
||||||
dispatch(new SendMessageToTelegramJob($message, $buttons, $telegramToken, $chatId));
|
dispatch(new SendMessageToTelegramJob($message, $buttons, $telegramToken, $chatId, $topicId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('teams', function (Blueprint $table) {
|
||||||
|
$table->text('telegram_notifications_test_message_thread_id')->nullable();
|
||||||
|
$table->text('telegram_notifications_deployments_message_thread_id')->nullable();
|
||||||
|
$table->text('telegram_notifications_status_changes_message_thread_id')->nullable();
|
||||||
|
$table->text('telegram_notifications_database_backups_message_thread_id')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('teams', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('telegram_message_thread_id');
|
||||||
|
$table->dropColumn('telegram_notifications_test_message_thread_id');
|
||||||
|
$table->dropColumn('telegram_notifications_deployments_message_thread_id');
|
||||||
|
$table->dropColumn('telegram_notifications_status_changes_message_thread_id');
|
||||||
|
$table->dropColumn('telegram_notifications_database_backups_message_thread_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
@ -16,27 +16,50 @@
|
|||||||
<x-forms.checkbox instantSave id="team.telegram_enabled" label="Notification Enabled" />
|
<x-forms.checkbox instantSave id="team.telegram_enabled" label="Notification Enabled" />
|
||||||
</div>
|
</div>
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
<x-forms.input type="password" helper="Get it from the <a class='inline-block text-white underline' href='https://t.me/botfather' target='_blank'>BotFather Bot</a> on Telegram." required
|
<x-forms.input type="password"
|
||||||
id="team.telegram_token" label="Token" />
|
helper="Get it from the <a class='inline-block text-white underline' href='https://t.me/botfather' target='_blank'>BotFather Bot</a> on Telegram."
|
||||||
<x-forms.input type="password" helper="Recommended to add your bot to a group chat and add its Chat ID here." required
|
required id="team.telegram_token" label="Token" />
|
||||||
|
<x-forms.input
|
||||||
|
helper="Recommended to add your bot to a group chat and add its Chat ID here." required
|
||||||
id="team.telegram_chat_id" label="Chat ID" />
|
id="team.telegram_chat_id" label="Chat ID" />
|
||||||
</div>
|
</div>
|
||||||
</form>
|
|
||||||
@if (data_get($team, 'telegram_enabled'))
|
@if (data_get($team, 'telegram_enabled'))
|
||||||
<h3 class="mt-4">Subscribe to events</h3>
|
<h2 class="mt-4">Subscribe to events</h2>
|
||||||
<div class="w-64">
|
<div class="w-96">
|
||||||
@if (isDev())
|
@if (isDev())
|
||||||
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_test" label="Test" />
|
<h3 class="mt-4">Test</h3>
|
||||||
|
<div class="flex items-end gap-10">
|
||||||
|
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_test" 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="team.telegram_notifications_test_message_thread_id" label="Custom Topic ID" />
|
||||||
|
</div>
|
||||||
@endif
|
@endif
|
||||||
<h4 class="mt-4">General</h4>
|
<h3 class="mt-4">Container Status Changes</h3>
|
||||||
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_status_changes"
|
<div class="flex items-end gap-10">
|
||||||
label="Container Status Changes" />
|
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_status_changes"
|
||||||
<h4 class="mt-4">Applications</h4>
|
label="Enabled" />
|
||||||
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_deployments"
|
<x-forms.input
|
||||||
label="Deployments" />
|
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used."
|
||||||
<h4 class="mt-4">Databases</h4>
|
id="team.telegram_notifications_status_changes_message_thread_id" label="Custom Topic ID" />
|
||||||
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_database_backups"
|
</div>
|
||||||
label="Backup Statuses" />
|
<h3 class="mt-4">Application Deployments</h3>
|
||||||
|
<div class="flex items-end gap-10">
|
||||||
|
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_deployments"
|
||||||
|
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="team.telegram_notifications_deployments_message_thread_id" label="Custom Topic ID" />
|
||||||
|
</div>
|
||||||
|
<h3 class="mt-4">Backup Status</h3>
|
||||||
|
<div class="flex items-end gap-10">
|
||||||
|
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_database_backups"
|
||||||
|
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="team.telegram_notifications_database_backups_message_thread_id" label="Custom Topic ID" />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user