Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Discord Webhook Functionality #1196

Merged
merged 8 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/Actions/Notifications/SendDiscordTestNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Actions\Notifications;

use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Http;
use Lorisleiva\Actions\Concerns\AsAction;

class SendDiscordTestNotification
{
use AsAction;

public function handle(array $webhooks)
{
if (! count($webhooks)) {
Notification::make()
->title('You need to add webhook urls!')
->warning()
->send();

return;
}

foreach ($webhooks as $webhook) {
$payload = [
'content' => 'πŸ‘‹ Testing the Webhook notification channel.',
];
// Send the request using Laravel's HTTP client
$response = Http::post($webhook['discord_webhook_url'], $payload);
}

Notification::make()
->title('Test webhook notification sent.')
->success()
->send();
}
}
43 changes: 43 additions & 0 deletions app/Filament/Pages/Settings/NotificationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Pages\Settings;

use App\Actions\Notifications\SendDatabaseTestNotification;
use App\Actions\Notifications\SendDiscordTestNotification;
use App\Actions\Notifications\SendMailTestNotification;
use App\Actions\Notifications\SendTelegramTestNotification;
use App\Actions\Notifications\SendWebhookTestNotification;
Expand Down Expand Up @@ -85,6 +86,48 @@ public function form(Form $form): Form
'md' => 2,
]),

Forms\Components\Section::make('Discord')
->schema([
Forms\Components\Toggle::make('discord_enabled')
->label('Enable Discord webhook notifications')
->reactive()
->columnSpanFull(),
Forms\Components\Grid::make([
'default' => 1,
])
->hidden(fn (Forms\Get $get) => $get('discord_enabled') !== true)
->schema([
Forms\Components\Fieldset::make('Triggers')
->schema([
Forms\Components\Toggle::make('discord_on_speedtest_run')
->label('Notify on every speedtest run')
->columnSpanFull(),
Forms\Components\Toggle::make('discord_on_threshold_failure')
->label('Notify on threshold failures')
->columnSpanFull(),
]),
Forms\Components\Repeater::make('discord_webhooks')
->label('Webhooks')
->schema([
Forms\Components\TextInput::make('discord_webhook_url')
->label('Webhook URL')
->required(),
])
->columnSpanFull(),
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('test discord')
->label('Test Discord webhook')
->action(fn (Forms\Get $get) => SendDiscordTestNotification::run(webhooks: $get('discord_webhooks')))
->hidden(fn (Forms\Get $get) => ! count($get('discord_webhooks'))),
]),
]),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),

Forms\Components\Section::make('Mail')
->schema([
Forms\Components\Toggle::make('mail_enabled')
Expand Down
19 changes: 19 additions & 0 deletions app/Listeners/SpeedtestCompletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Settings\NotificationSettings;
use App\Telegram\TelegramNotification;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Spatie\WebhookServer\WebhookCall;

Expand Down Expand Up @@ -75,6 +76,24 @@ public function handle(ResultCreated $event): void
}
}

if ($this->notificationSettings->discord_enabled) {
if ($this->notificationSettings->discord_on_speedtest_run && count($this->notificationSettings->discord_webhooks)) {
foreach ($this->notificationSettings->discord_webhooks as $webhook) {
// Construct the payload
$payload = [
'content' => 'There are new speedtest results for your network.'.
"\nResult ID: ".$event->result->id.
"\nSite Name: ".$this->generalSettings->site_name.
"\nPing: ".$event->result->ping.' ms'.
"\nDownload: ".($event->result->downloadBits / 1000000).' (Mbps)'.
"\nUpload: ".($event->result->uploadBits / 1000000).' (Mbps)',
];
// Send the request using Laravel's HTTP client
$response = Http::post($webhook['discord_webhook_url'], $payload);
}
}
}

if ($this->notificationSettings->webhook_enabled) {
if ($this->notificationSettings->webhook_on_speedtest_run && count($this->notificationSettings->webhook_urls)) {
foreach ($this->notificationSettings->webhook_urls as $url) {
Expand Down
54 changes: 54 additions & 0 deletions app/Listeners/Threshold/AbsoluteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function handle(ResultCreated $event): void
$this->telegramChannel($event);
}

// Discord notification channel
if ($this->notificationSettings->discord_enabled == true && $this->notificationSettings->discord_on_threshold_failure == true) {
$this->discordChannel($event);
}
jonezy35 marked this conversation as resolved.
Show resolved Hide resolved

// Webhook notification channel
if ($this->notificationSettings->webhook_enabled == true && $this->notificationSettings->webhook_on_threshold_failure == true) {
$this->webhookChannel($event);
Expand Down Expand Up @@ -219,6 +224,55 @@ protected function telegramChannel(ResultCreated $event): void
}
}

/**
* Handle Discord notifications.
*/
protected function discordChannel(ResultCreated $event): void
{
if ($this->notificationSettings->discord_enabled) {
$failedThresholds = []; // Initialize an array to keep track of failed thresholds

// Check Download threshold
if ($this->thresholdSettings->absolute_download > 0 && absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->downloadBits)) {
$failedThresholds['Download'] = ($event->result->downloadBits / 1000000).' (Mbps)';
}

// Check Upload threshold
if ($this->thresholdSettings->absolute_upload > 0 && absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->uploadBits)) {
$failedThresholds['Upload'] = ($event->result->uploadBits / 1000000).' (Mbps)';
}

// Check Ping threshold
if ($this->thresholdSettings->absolute_ping > 0 && absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
$failedThresholds['Ping'] = $event->result->ping.' ms';
}

// Proceed with sending notifications only if there are any failed thresholds
if (count($failedThresholds) > 0) {
if ($this->notificationSettings->discord_on_threshold_failure && count($this->notificationSettings->discord_webhooks)) {
foreach ($this->notificationSettings->discord_webhooks as $webhook) {
// Construct the payload with the failed thresholds information
$contentLines = [
'Result ID: '.$event->result->id,
'Site Name: '.$this->generalSettings->site_name,
];

foreach ($failedThresholds as $metric => $result) {
$contentLines[] = "{$metric} threshold failed with result: {$result}.";
}

$payload = [
'content' => implode("\n", $contentLines),
];

// Send the request using Laravel's HTTP client
$response = Http::post($webhook['discord_webhook_url'], $payload);
}
}
}
}
}

/**
* Handle webhook notifications.
*
Expand Down
8 changes: 8 additions & 0 deletions app/Settings/NotificationSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ class NotificationSettings extends Settings

public ?array $webhook_urls;

public bool $discord_enabled;

public bool $discord_on_speedtest_run;

public bool $discord_on_threshold_failure;

public ?array $discord_webhooks;

public static function group(): string
{
return 'notification';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

return new class extends SettingsMigration
{
/**
* Run the migrations.
*/
public function up(): void
{
$this->migrator->add('notification.discord_enabled', false);
$this->migrator->add('notification.discord_on_speedtest_run', false);
$this->migrator->add('notification.discord_on_threshold_failure', false);
$this->migrator->add('notification.discord_webhooks', null);
}
};
Loading