Fix ntfy notifications

This commit is contained in:
headlessdev 2025-04-25 22:51:34 +02:00
parent 7ad4390db7
commit b368075b12

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"context" "context"
"crypto/x509" "crypto/x509"
"database/sql" "database/sql"
@ -653,18 +652,14 @@ func sendGotify(n Notification, message string) {
} }
func sendNtfy(n Notification, message string) { func sendNtfy(n Notification, message string) {
fmt.Println("Sending Ntfy notification...")
baseURL := strings.TrimSuffix(n.NtfyUrl.String, "/") baseURL := strings.TrimSuffix(n.NtfyUrl.String, "/")
topic := "corecontrol"
requestURL := fmt.Sprintf("%s/%s", baseURL, topic)
payload := map[string]string{"message": message} // Don't append a topic to the URL - the URL itself should have the correct endpoint
jsonData, err := json.Marshal(payload) requestURL := baseURL
if err != nil {
fmt.Printf("Ntfy: ERROR marshaling JSON: %v\n", err)
return
}
req, err := http.NewRequest("POST", requestURL, bytes.NewBuffer(jsonData)) // Send message directly as request body instead of JSON
req, err := http.NewRequest("POST", requestURL, strings.NewReader(message))
if err != nil { if err != nil {
fmt.Printf("Ntfy: ERROR creating request: %v\n", err) fmt.Printf("Ntfy: ERROR creating request: %v\n", err)
return return
@ -673,7 +668,8 @@ func sendNtfy(n Notification, message string) {
if n.NtfyToken.Valid { if n.NtfyToken.Valid {
req.Header.Set("Authorization", "Bearer "+n.NtfyToken.String) req.Header.Set("Authorization", "Bearer "+n.NtfyToken.String)
} }
req.Header.Set("Content-Type", "application/json") // Use text/plain instead of application/json
req.Header.Set("Content-Type", "text/plain")
client := &http.Client{Timeout: 5 * time.Second} client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Do(req) resp, err := client.Do(req)
@ -760,6 +756,7 @@ func checkAndSendTestNotifications(db *sql.DB) {
} }
func sendSpecificNotification(n Notification, message string) { func sendSpecificNotification(n Notification, message string) {
fmt.Println("Sending specific notification..." + n.Type)
switch n.Type { switch n.Type {
case "smtp": case "smtp":
if n.SMTPHost.Valid && n.SMTPTo.Valid { if n.SMTPHost.Valid && n.SMTPTo.Valid {