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
40
main.go
40
main.go
|
@ -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)
|
||||||
|
@ -63,7 +64,7 @@ func main() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
log.Println("Frage GoToSocial-Benachrichtigungen ab ...")
|
log.Println("Frage GoToSocial-Benachrichtigungen ab...")
|
||||||
notifications, err := fetchNotifications(lastID)
|
notifications, err := fetchNotifications(lastID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Fehler beim Abrufen: %v", err)
|
log.Printf("Fehler beim Abrufen: %v", err)
|
||||||
|
@ -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, "<", "<")
|
||||||
|
plain = strings.ReplaceAll(plain, ">", ">")
|
||||||
|
plain = strings.ReplaceAll(plain, "&", "&")
|
||||||
|
plain = strings.ReplaceAll(plain, """, "\"")
|
||||||
|
plain = strings.ReplaceAll(plain, "'", "'")
|
||||||
|
|
||||||
|
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,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue