Skip to content

Commit 655ba21

Browse files
committed
add apprise support to notifications
1 parent 592b574 commit 655ba21

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

app/Filament/Resources/Users/Schemas/UserForm.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static function configure(Schema $schema): Schema
5656
->remove(['https://', 'http://'])
5757
->explode('/');
5858

59-
if (count($ntfy_url) ==1) {
59+
if (count($ntfy_url) == 1) {
6060
Notification::make()
6161
->title('you are missing the topic in ntfy url')
6262
->body('the url should be something like https://myntfy.com/topic')
@@ -74,19 +74,27 @@ public static function configure(Schema $schema): Schema
7474

7575
TextInput::make('notification_settings.ntfy_auth_token'),
7676

77-
TextInput::make('notification_settings.telegram_bot_token'),
77+
TextInput::make('notification_settings.telegram_bot_token')
78+
->columnStart(1),
7879
TextInput::make('notification_settings.telegram_channel_id')
7980
->hint('include the "-"'),
8081

81-
TextInput::make('notification_settings.gotify_url'),
82+
TextInput::make('notification_settings.gotify_url')
83+
->columnStart(1),
8284
TextInput::make('notification_settings.gotify_token'),
8385

84-
Toggle::make('notification_settings.enable_rss_feed')
86+
TextInput::make('notification_settings.apprise_url')
87+
->placeholder('https://mydomain.com/notify/configKey')
88+
->url()
8589
->columnStart(1),
90+
8691
TextInput::make('rss_feed')
8792
->formatStateUsing(fn ($state) => config('app.url').'/feed/?feed_id='.$state)
8893
->copyable()
89-
->disabled(),
94+
->disabled()
95+
->columnStart(1),
96+
97+
Toggle::make('notification_settings.enable_rss_feed'),
9098

9199
]),
92100

app/Notifications/ProductDiscounted.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\Link;
66
use App\Models\RssFeedItem;
77
use App\Models\User;
8+
use App\NotificationsChannels\AppriseChannel;
89
use App\NotificationsChannels\GotifyChannel;
910
use App\NotificationsChannels\NtfyChannel;
1011
use Illuminate\Notifications\Notification;
@@ -78,6 +79,10 @@ public function via(User $notifiable): array
7879
$channels[] = GotifyChannel::class;
7980
}
8081

82+
if ($notifiable->notification_settings['apprise_url']) {
83+
$channels[] = AppriseChannel::class;
84+
}
85+
8186
if ($notifiable->notification_settings['enable_rss_feed']) {
8287
RssFeedItem::create([
8388
'user_id' => $notifiable->id,
@@ -101,11 +106,11 @@ public function toApprise(object $notifiable): array
101106
{
102107

103108
$content = [
104-
'title' => "For Just $this->price - Discount For ".Str::words($this->product_name, 5),
109+
"title" => $this->notification_title,
105110
'body' => $this->notification_text.
106111
"----------------<br>
107112
Product URL: . {$this->product_url}",
108-
'attach' => [$this->image],
113+
'attach' => [$this->product_image],
109114
'format' => 'html',
110115
];
111116

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\NotificationsChannels;
4+
5+
use App\Models\User;
6+
use Illuminate\Support\Facades\Http;
7+
8+
class Apprise
9+
{
10+
protected $http_client;
11+
12+
public function __construct()
13+
{
14+
$this->http_client = new Http;
15+
}
16+
17+
public function send(array $notification, string $notification_content, User $user)
18+
{
19+
if (! isset($user->notification_settings['apprise_url']) || ! $user->notification_settings['apprise_url']) {
20+
return null;
21+
}
22+
23+
$response = Http::withHeaders([
24+
"Content-Type" => "application/json",
25+
"Cache: no",
26+
])
27+
->post($user->notification_settings['apprise_url'], [
28+
'tags' => 'all',
29+
"title" => $notification['title'],
30+
'body' => $notification['body'],
31+
'attach' => $notification['attach'],
32+
'format' => 'html',
33+
]);
34+
35+
return $response->json();
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\NotificationsChannels;
4+
5+
use Illuminate\Notifications\Notification;
6+
7+
class AppriseChannel
8+
{
9+
protected $apprise;
10+
11+
public function __construct(Apprise $apprise)
12+
{
13+
$this->apprise = $apprise;
14+
}
15+
16+
/**
17+
* Send the given notification.
18+
*/
19+
public function send(object $notifiable, Notification $notification)
20+
{
21+
$message = $notification->toApprise($notifiable);
22+
23+
return $this->apprise->send(notification: $message['content'], notification_content: $message["content"]['body'], user: $notifiable);
24+
25+
}
26+
}

0 commit comments

Comments
 (0)