@@ -23,6 +23,28 @@ public function save() {
23
23
echo __ ("Ntfy settings saved. " );
24
24
}
25
25
26
+ public function test_notify () {
27
+ $ ntfy_server = $ _POST ["ntfy_server " ];
28
+ $ ntfy_topic = $ _POST ["ntfy_topic " ];
29
+ $ ntfy_token = $ _POST ["ntfy_token " ];
30
+
31
+ $ message = [
32
+ "topic " => $ ntfy_topic ,
33
+ "message " => "This is a test notification from Tiny Tiny RSS " ,
34
+ "title " => "Test Notification " ,
35
+ "tags " => ["mailbox_with_mail " ]
36
+ ];
37
+
38
+ $ error = $ this ->send_ntfy_message ($ ntfy_server , $ ntfy_token , $ message );
39
+
40
+
41
+ if ($ error ) {
42
+ echo __ ("Error sending test notification: " ) . " " . $ error ;
43
+ } else {
44
+ echo __ ("Test notification sent successfully " );
45
+ }
46
+ }
47
+
26
48
public function init ($ host ) {
27
49
$ this ->host = $ host ;
28
50
@@ -53,7 +75,7 @@ public function hook_prefs_tab($args) {
53
75
54
76
<h2> <?= __ ('Send feed notification via Ntfy ' ) ?> </h2>
55
77
56
- <form dojoType="dijit.form.Form">
78
+ <form id="ntfyForm" dojoId="ntfyForm" dojoType="dijit.form.Form">
57
79
<?= \Controls \pluginhandler_tags ($ this , "save " ) ?>
58
80
<script type="dojo/method" event="onSubmit" args="evt">
59
81
evt.preventDefault();
@@ -78,6 +100,29 @@ public function hook_prefs_tab($args) {
78
100
79
101
<?= \Controls \submit_tag (__ ("Save " )) ?>
80
102
103
+ <button dojoType="dijit.form.Button">
104
+ <?= __ ("Test " ) ?>
105
+ <script type="dojo/on" data-dojo-event="click" data-dojo-args="evt" >
106
+ require(['dojo/dom-form'], function(domForm) {
107
+ Notify.progress('Sending test notification...', true);
108
+ const ntfyData = domForm.toObject('ntfyForm')
109
+ xhr.post("backend.php", {
110
+ ...ntfyData,
111
+ op: "PluginHandler",
112
+ plugin: "ntfy",
113
+ method: "test_notify"
114
+ }, reply => {
115
+ if (reply.errors) {
116
+ Notify.error(reply.errors.join('; '));
117
+ } else {
118
+ Notify.info(reply);
119
+ }
120
+ });
121
+ });
122
+
123
+ </script>
124
+ </button>
125
+
81
126
</form>
82
127
<hr />
83
128
<?php
@@ -162,40 +207,47 @@ public function send_notification($article) {
162
207
$ ntfy_topic = $ this ->host ->get ($ this , "ntfy_topic " );
163
208
$ ntfy_token = $ this ->host ->get ($ this , "ntfy_token " );
164
209
165
- $ ch = curl_init ();
166
-
167
210
$ content = $ this ->clean_html ($ article ['content ' ]);
168
211
$ truncatedContent = strlen ($ content ) > 500
169
- ? mb_substr ($ content , 0 , 500 , 'UTF-8 ' ) . "... "
170
- : $ content ;
171
-
172
- $ headers = [];
173
- if (strlen (trim ($ ntfy_token )) > 0 ) {
174
- $ headers [] = "Authorization: Bearer " . $ ntfy_token ;
175
- }
212
+ ? mb_substr ($ content , 0 , 500 , 'UTF-8 ' ) . "... "
213
+ : $ content ;
176
214
177
- // get article feed title
178
215
$ title = "【 " . Feeds::_get_title ($ article ["feed " ]["id " ]) . "】 " . $ article ["title " ];
179
- $ jsonData = json_encode ([
180
- "topic " => $ ntfy_topic ,
181
- "message " => $ truncatedContent ,
182
- "title " => $ title ,
183
- "tags " => ["mailbox_with_mail " ],
184
- "click " => $ article ["link " ],
185
- ]);
186
- curl_setopt ($ ch , CURLOPT_URL , $ ntfy_server );
187
- curl_setopt ($ ch , CURLOPT_POST , true );
188
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
189
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ jsonData );
190
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
191
- curl_exec ($ ch );
192
- curl_close ($ ch );
193
-
216
+ $ message = [
217
+ "topic " => $ ntfy_topic ,
218
+ "message " => $ truncatedContent ,
219
+ "title " => $ title ,
220
+ "tags " => ["mailbox_with_mail " ],
221
+ "click " => $ article ["link " ],
222
+ ];
223
+
224
+ $ this ->send_ntfy_message ($ ntfy_server , $ ntfy_token , $ message );
194
225
array_push ($ article ["tags " ], "ntfy " );
195
226
}
196
227
return $ article ;
197
228
}
198
229
230
+ private function send_ntfy_message ($ server , $ token , $ message ) {
231
+ $ ch = curl_init ();
232
+
233
+ $ headers = [];
234
+ if (strlen (trim ($ token )) > 0 ) {
235
+ $ headers [] = "Authorization: Bearer " . $ token ;
236
+ }
237
+
238
+ curl_setopt ($ ch , CURLOPT_URL , $ server );
239
+ curl_setopt ($ ch , CURLOPT_POST , true );
240
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
241
+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ message ));
242
+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
243
+
244
+ curl_exec ($ ch );
245
+ $ error = curl_error ($ ch );
246
+ curl_close ($ ch );
247
+
248
+ return $ error ;
249
+ }
250
+
199
251
public function api_version () {
200
252
return 2 ;
201
253
}
0 commit comments