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

[Notification] Change Ntfy, Gotify, Pushover from Webhook to framework #1723

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 11 additions & 11 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -294,7 +294,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -536,7 +536,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -778,7 +778,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -1020,7 +1020,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -1262,7 +1262,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -1504,7 +1504,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -1746,7 +1746,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -1988,7 +1988,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -2230,7 +2230,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down Expand Up @@ -2472,7 +2472,7 @@
'Illuminate\Console\Scheduling\ScheduleTestCommand' => \Illuminate\Console\Scheduling\ScheduleTestCommand::class,
'Illuminate\Console\Scheduling\ScheduleWorkCommand' => \Illuminate\Console\Scheduling\ScheduleWorkCommand::class,
'Illuminate\Contracts\Auth\Access\Gate' => \Illuminate\Auth\Access\Gate::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\NullBroadcaster::class,
'Illuminate\Contracts\Broadcasting\Broadcaster' => \Illuminate\Broadcasting\Broadcasters\LogBroadcaster::class,
'Illuminate\Contracts\Console\Kernel' => \Illuminate\Foundation\Console\Kernel::class,
'Illuminate\Contracts\Debug\ExceptionHandler' => \NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler::class,
'Illuminate\Contracts\Foundation\ExceptionRenderer' => \Spatie\LaravelIgnition\Renderers\IgnitionExceptionRenderer::class,
Expand Down
42 changes: 31 additions & 11 deletions app/Actions/Notifications/SendGotifyTestNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,54 @@
namespace App\Actions\Notifications;

use Filament\Notifications\Notification;
use Gotify\Auth\Token;
use Gotify\Endpoint\Message;
use Gotify\Exception\EndpointException;
use Gotify\Exception\GotifyException;
use Gotify\Server;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\WebhookServer\WebhookCall;

class SendGotifyTestNotification
{
use AsAction;

public function handle(array $webhooks)
public function handle(array $webhooks, string $token)
{
if (! count($webhooks)) {
if (empty($webhooks)) {
Notification::make()
->title('You need to add Gotify urls!')
->title('You need to add Gotify URLs!')
->warning()
->send();

return;
}

foreach ($webhooks as $webhook) {
WebhookCall::create()
->url($webhook['url'])
->payload(['message' => 'πŸ‘‹ Testing the Gotify notification channel.'])
->doNotSign()
->dispatch();
foreach ($webhooks as $url) {
try {
// Set up the server and token
$server = new Server($url);
$auth = new Token($token);
$message = new Message($server, $auth);

// Send message
$details = $message->create(
title: 'Test Notification',
message: 'πŸ‘‹ Testing the Gotify notification channel',
);

// Optionally, you can log the sent message details
// echo 'Id: ' . $details->id . PHP_EOL;

} catch (EndpointException|GotifyException $err) {
Notification::make()
->title('Error sending Gotify notification: '.$err->getMessage())
->danger()
->send();
}
}

Notification::make()
->title('Test Gotify notification sent.')
->title('Test Gotify notifications sent.')
->success()
->send();
}
Expand Down
62 changes: 39 additions & 23 deletions app/Actions/Notifications/SendNtfyTestNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

use Filament\Notifications\Notification;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\WebhookServer\WebhookCall;
use Ntfy\Auth\Token;
use Ntfy\Auth\User;
use Ntfy\Client;
use Ntfy\Exception\EndpointException;
use Ntfy\Exception\NtfyException;
use Ntfy\Message;
use Ntfy\Server;

class SendNtfyTestNotification
{
Expand All @@ -14,36 +20,46 @@ public function handle(array $webhooks)
{
if (! count($webhooks)) {
Notification::make()
->title('You need to add ntfy urls!')
->title('You need to add ntfy URLs!')
->warning()
->send();

return;
}

foreach ($webhooks as $webhook) {
$webhookCall = WebhookCall::create()
->url($webhook['url'])
->payload([
'topic' => $webhook['topic'],
'message' => 'πŸ‘‹ Testing the ntfy notification channel.',
])
->doNotSign();

// Only add authentication if username and password are provided
if (! empty($webhook['username']) && ! empty($webhook['password'])) {
$authHeader = 'Basic '.base64_encode($webhook['username'].':'.$webhook['password']);
$webhookCall->withHeaders([
'Authorization' => $authHeader,
]);
}
try {
// Set server
$server = new Server($webhook['url']);

$webhookCall->dispatch();
}
// Create a new message
$message = new Message();
$message->topic($webhook['topic']);
$message->title('Test Notification');
$message->body('πŸ‘‹ Testing the ntfy notification channel.');

$auth = null;
if (! empty($webhook['token'])) {
$auth = new Token($webhook['token']);
} elseif (! empty($webhook['username']) && ! empty($webhook['password'])) {
$auth = new User($webhook['username'], $webhook['password']);
}

// Create a client with optional authentication
$client = new Client($server, $auth);
$response = $client->send($message);

Notification::make()
->title('Test ntfy notification sent.')
->success()
->send();
Notification::make()
->title("Test ntfy notification sent to {$webhook['topic']}.")
->success()
->send();

} catch (EndpointException|NtfyException $err) {
Notification::make()
->title("Failed to send notification to {$webhook['topic']}.")
->warning()
->send();
}
}
}
}
58 changes: 40 additions & 18 deletions app/Actions/Notifications/SendPushoverTestNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App\Actions\Notifications;

use Filament\Notifications\Notification;
use Filament\Notifications\Notification as FilamentNotification;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\WebhookServer\WebhookCall;
use Serhiy\Pushover\Api\Message\Message;
use Serhiy\Pushover\Api\Message\Notification as PushoverNotification;
use Serhiy\Pushover\Application;
use Serhiy\Pushover\Recipient;

class SendPushoverTestNotification
{
Expand All @@ -13,29 +16,48 @@ class SendPushoverTestNotification
public function handle(array $webhooks)
{
if (! count($webhooks)) {
Notification::make()
->title('You need to add Pushover URLs!')
FilamentNotification::make()
->title('You need to add Pushover credentials!')
->warning()
->send();

return;
}

foreach ($webhooks as $webhook) {
WebhookCall::create()
->url($webhook['url'])
->payload([
'token' => $webhook['api_token'],
'user' => $webhook['user_key'],
'message' => 'πŸ‘‹ Testing the Pushover notification channel.',
])
->doNotSign()
->dispatch();
}
try {
// Create Application and Recipient objects
$application = new Application($webhook['api_token']);
$recipient = new Recipient($webhook['user_key']);

// Compose the message with title and body
$message = new Message('πŸ‘‹ Testing the Pushover notification channel.', 'Speedtest Tracker Test Notification');

// Create a notification with the application, recipient, and message
$pushoverNotification = new PushoverNotification($application, $recipient, $message);

Notification::make()
->title('Test Pushover notification sent.')
->success()
->send();
// Push the notification
/** @var \Serhiy\Pushover\Client\Response\MessageResponse $response */
$response = $pushoverNotification->push();

// Check response status
if ($response->isSuccessful()) {
FilamentNotification::make()
->title('Test Pushover notification sent.')
->success()
->send();
} else {
FilamentNotification::make()
->title('Failed to send Pushover notification: '.$response->getMessage())
->danger()
->send();
}
} catch (\Exception $e) {
FilamentNotification::make()
->title('An error occurred: '.$e->getMessage())
->danger()
->send();
}
}
}
}
Loading