konventiere HTML zu Reintext und verbessere Logging

This commit is contained in:
Simon Rieger 2025-07-01 20:33:33 +02:00
parent 9bbb9157fe
commit 7d6767f2e1

38
main.go
View file

@ -8,6 +8,8 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"regexp"
"strings"
"time" "time"
) )
@ -43,7 +45,6 @@ type NtfyMessage struct {
func main() { func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile) log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
log.Println("Starte GoToSocial-Notifier...") log.Println("Starte GoToSocial-Notifier...")
if pollInterval == 0 { if pollInterval == 0 {
@ -52,7 +53,7 @@ func main() {
} }
if gotosocialURL == "" || accessToken == "" || ntfyServer == "" || ntfyTopic == "" { if gotosocialURL == "" || accessToken == "" || ntfyServer == "" || ntfyTopic == "" {
log.Fatal("Eine oder mehrere erforderliche Umgebungsvariablen fehlen (GOTOSOCIAL_URL, GOTOSOCIAL_TOKEN, NTFY_SERVER, NTFY_TOPIC)") log.Fatal("Eine oder mehrere erforderliche Umgebungsvariablen fehlen")
} }
ticker := time.NewTicker(pollInterval) ticker := time.NewTicker(pollInterval)
@ -72,16 +73,17 @@ func main() {
if len(notifications) > 0 { if len(notifications) > 0 {
lastID = notifications[0].ID lastID = notifications[0].ID
log.Printf("%d neue Benachrichtigungen gefunden.", len(notifications)) log.Printf("%d neue Benachrichtigungen gefunden", len(notifications))
} else { } else {
log.Println("Keine neuen Benachrichtigungen.") log.Println("Keine neuen Benachrichtigungen")
} }
for _, n := range notifications { for _, n := range notifications {
title := fmt.Sprintf("Neue Benachrichtigung von %s", n.Account.DisplayName)
if err := sendToNtfy(n); err != nil { if err := sendToNtfy(n); err != nil {
log.Printf("Fehler beim Senden an ntfy: %v", err) log.Printf("Fehler beim Senden von '%s': %v", title, err)
} else { } else {
log.Printf("Benachrichtigung '%s' an ntfy gesendet.", n.ID) log.Printf("Benachrichtigung gesendet: '%s' (Typ: %s)", title, n.Type)
} }
} }
} }
@ -125,11 +127,31 @@ func fetchNotifications(sinceID string) ([]Notification, error) {
return notifications, nil return notifications, nil
} }
// HTML zu Plaintext konvertieren
func htmlToPlaintext(html string) string {
// Ersetze HTML-Tags
re := regexp.MustCompile(`<[^>]*>`)
plain := re.ReplaceAllString(html, "")
// Ersetze HTML-Entities
plain = strings.ReplaceAll(plain, "&lt;", "<")
plain = strings.ReplaceAll(plain, "&gt;", ">")
plain = strings.ReplaceAll(plain, "&amp;", "&")
plain = strings.ReplaceAll(plain, "&quot;", "\"")
plain = strings.ReplaceAll(plain, "&apos;", "'")
return plain
}
func sendToNtfy(n Notification) error { func sendToNtfy(n Notification) error {
// HTML zu Plaintext konvertieren
plainContent := htmlToPlaintext(n.Status.Content)
title := fmt.Sprintf("Neue Benachrichtigung von %s", n.Account.DisplayName)
msg := NtfyMessage{ msg := NtfyMessage{
Topic: ntfyTopic, Topic: ntfyTopic,
Title: fmt.Sprintf("Neue Benachrichtigung von %s", n.Account.DisplayName), Title: title,
Message: fmt.Sprintf("Typ: %s\n\n%s", n.Type, n.Status.Content), Message: fmt.Sprintf("Typ: %s\n\n%s", n.Type, plainContent),
Tags: []string{"bell", "incoming_envelope"}, Tags: []string{"bell", "incoming_envelope"},
Priority: 4, Priority: 4,
} }