mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
80 lines
2.0 KiB
PHP
Executable File
80 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Filament\Actions\Notifications;
|
|
|
|
use App\Notifications\Channels\GotifyChannel;
|
|
use Closure;
|
|
use Exception;
|
|
use Filament\Forms\Components\Actions\Action;
|
|
use Filament\Notifications\Notification;
|
|
use Illuminate\Http\Client\RequestException;
|
|
|
|
class TestGotifyAction extends Action
|
|
{
|
|
public Closure $settingsCallback;
|
|
|
|
public static function getDefaultName(): ?string
|
|
{
|
|
return 'gotify_tesst';
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->label(__('Test'));
|
|
|
|
$this->successNotificationTitle(__('Test notification sent successfully'));
|
|
|
|
$this->failureNotificationTitle(__('Error'));
|
|
|
|
$this->icon('heroicon-m-bell');
|
|
|
|
$this->color('gray');
|
|
|
|
$this->action(fn () => $this->testGotifyNotification());
|
|
}
|
|
|
|
public function setSettings(Closure $settingsCallback): self
|
|
{
|
|
$this->settingsCallback = $settingsCallback;
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function testGotifyNotification(): void
|
|
{
|
|
$settings = call_user_func($this->settingsCallback);
|
|
$baseUrl = data_get($settings, 'url');
|
|
$token = data_get($settings, 'token');
|
|
|
|
if (empty($baseUrl) || empty($token)) {
|
|
Notification::make()
|
|
->title('Error')
|
|
->body('Please save your Gotify settings first')
|
|
->danger()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$response = GotifyChannel::sendRequest(
|
|
GotifyChannel::makeUrl($baseUrl, $token),
|
|
'Test Notification',
|
|
'This is a test notification from PriceBuddy',
|
|
url('/')
|
|
);
|
|
|
|
$response->throw();
|
|
$this->success();
|
|
} catch (RequestException|Exception $e) {
|
|
Notification::make()
|
|
->title('Failed to send test notification')
|
|
->body('Error: '.$e->getMessage())
|
|
->danger()
|
|
->send();
|
|
}
|
|
}
|
|
}
|