|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Notifications; |
| 4 | + |
| 5 | +use Illuminate\Notifications\Notification; |
| 6 | +use Illuminate\Support\Facades\Http; |
| 7 | +use Illuminate\Support\Facades\Log; |
| 8 | + |
| 9 | +class AppriseChannel |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Send the given notification. |
| 13 | + */ |
| 14 | + public function send(object $notifiable, Notification $notification): void |
| 15 | + { |
| 16 | + // Get the Apprise message from the notification |
| 17 | + $message = $notification->toApprise($notifiable); |
| 18 | + |
| 19 | + if (! $message) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + $appriseUrl = config('services.apprise.url'); |
| 24 | + |
| 25 | + try { |
| 26 | + $response = Http::timeout(5) |
| 27 | + ->withHeaders([ |
| 28 | + 'Content-Type' => 'application/json', |
| 29 | + ]) |
| 30 | + // ->when(true, function ($http) { |
| 31 | + // $http->withoutVerifying(); |
| 32 | + // }) |
| 33 | + ->post("{$appriseUrl}/notify", [ |
| 34 | + 'urls' => $message->urls, |
| 35 | + 'title' => $message->title, |
| 36 | + 'body' => $message->body, |
| 37 | + 'type' => $message->type ?? 'info', |
| 38 | + 'format' => $message->format ?? 'text', |
| 39 | + 'tag' => $message->tag ?? null, |
| 40 | + ]); |
| 41 | + |
| 42 | + if ($response->failed()) { |
| 43 | + Log::error('Apprise notification failed', [ |
| 44 | + 'status' => $response->status(), |
| 45 | + 'body' => $response->body(), |
| 46 | + ]); |
| 47 | + } |
| 48 | + } catch (\Exception $e) { |
| 49 | + Log::error('Apprise notification exception', [ |
| 50 | + 'message' => $e->getMessage(), |
| 51 | + ]); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments