konventiere HTML zu Reintext und verbessere Logging
This commit is contained in:
parent
9bbb9157fe
commit
7d6767f2e1
1 changed files with 31 additions and 9 deletions
38
main.go
38
main.go
|
@ -8,6 +8,8 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -43,7 +45,6 @@ type NtfyMessage struct {
|
|||
|
||||
func main() {
|
||||
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
|
||||
|
||||
log.Println("Starte GoToSocial-Notifier...")
|
||||
|
||||
if pollInterval == 0 {
|
||||
|
@ -52,7 +53,7 @@ func main() {
|
|||
}
|
||||
|
||||
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)
|
||||
|
@ -72,16 +73,17 @@ func main() {
|
|||
|
||||
if len(notifications) > 0 {
|
||||
lastID = notifications[0].ID
|
||||
log.Printf("%d neue Benachrichtigungen gefunden.", len(notifications))
|
||||
log.Printf("%d neue Benachrichtigungen gefunden", len(notifications))
|
||||
} else {
|
||||
log.Println("Keine neuen Benachrichtigungen.")
|
||||
log.Println("Keine neuen Benachrichtigungen")
|
||||
}
|
||||
|
||||
for _, n := range notifications {
|
||||
title := fmt.Sprintf("Neue Benachrichtigung von %s", n.Account.DisplayName)
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
// 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, "<", "<")
|
||||
plain = strings.ReplaceAll(plain, ">", ">")
|
||||
plain = strings.ReplaceAll(plain, "&", "&")
|
||||
plain = strings.ReplaceAll(plain, """, "\"")
|
||||
plain = strings.ReplaceAll(plain, "'", "'")
|
||||
|
||||
return plain
|
||||
}
|
||||
|
||||
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{
|
||||
Topic: ntfyTopic,
|
||||
Title: fmt.Sprintf("Neue Benachrichtigung von %s", n.Account.DisplayName),
|
||||
Message: fmt.Sprintf("Typ: %s\n\n%s", n.Type, n.Status.Content),
|
||||
Title: title,
|
||||
Message: fmt.Sprintf("Typ: %s\n\n%s", n.Type, plainContent),
|
||||
Tags: []string{"bell", "incoming_envelope"},
|
||||
Priority: 4,
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue