2025-07-01 20:28:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2025-07-01 20:33:33 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2025-07-01 20:28:36 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Konfiguration aus Umgebungsvariablen
|
|
|
|
var (
|
2025-07-01 20:29:31 +02:00
|
|
|
gotosocialURL = os.Getenv("GOTOSOCIAL_URL")
|
|
|
|
accessToken = os.Getenv("GOTOSOCIAL_TOKEN")
|
|
|
|
ntfyServer = os.Getenv("NTFY_SERVER")
|
|
|
|
ntfyToken = os.Getenv("NTFY_TOKEN")
|
|
|
|
ntfyTopic = os.Getenv("NTFY_TOPIC")
|
|
|
|
pollInterval, _ = time.ParseDuration(os.Getenv("POLL_INTERVAL"))
|
2025-07-01 20:28:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Notification struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
Account struct {
|
|
|
|
DisplayName string `json:"display_name"`
|
2025-07-01 20:38:44 +02:00
|
|
|
Acct string `json:"acct"`
|
2025-07-01 20:28:36 +02:00
|
|
|
} `json:"account"`
|
|
|
|
Status struct {
|
2025-07-01 20:38:44 +02:00
|
|
|
ID string `json:"id"`
|
2025-07-01 20:28:36 +02:00
|
|
|
Content string `json:"content"`
|
2025-07-01 20:38:44 +02:00
|
|
|
URL string `json:"url"` // Original-URL des Posts
|
2025-07-01 20:28:36 +02:00
|
|
|
} `json:"status"`
|
|
|
|
}
|
|
|
|
|
2025-07-01 20:38:44 +02:00
|
|
|
type NtfyAction struct {
|
|
|
|
Action string `json:"action"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Clear bool `json:"clear"`
|
|
|
|
}
|
|
|
|
|
2025-07-01 20:28:36 +02:00
|
|
|
type NtfyMessage struct {
|
2025-07-01 20:38:44 +02:00
|
|
|
Topic string `json:"topic"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
Priority int `json:"priority"`
|
|
|
|
Actions []NtfyAction `json:"actions,omitempty"`
|
2025-07-01 20:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2025-07-01 20:29:31 +02:00
|
|
|
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
|
|
|
|
log.Println("Starte GoToSocial-Notifier...")
|
|
|
|
|
2025-07-01 20:28:36 +02:00
|
|
|
if pollInterval == 0 {
|
|
|
|
pollInterval = 30 * time.Second
|
2025-07-01 20:29:31 +02:00
|
|
|
log.Printf("Kein POLL_INTERVAL gesetzt, verwende Default: %s", pollInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotosocialURL == "" || accessToken == "" || ntfyServer == "" || ntfyTopic == "" {
|
2025-07-01 20:33:33 +02:00
|
|
|
log.Fatal("Eine oder mehrere erforderliche Umgebungsvariablen fehlen")
|
2025-07-01 20:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(pollInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
lastID := ""
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2025-07-01 20:33:33 +02:00
|
|
|
log.Println("Frage GoToSocial-Benachrichtigungen ab...")
|
2025-07-01 20:28:36 +02:00
|
|
|
notifications, err := fetchNotifications(lastID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Fehler beim Abrufen: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(notifications) > 0 {
|
|
|
|
lastID = notifications[0].ID
|
2025-07-01 20:33:33 +02:00
|
|
|
log.Printf("%d neue Benachrichtigungen gefunden", len(notifications))
|
2025-07-01 20:29:31 +02:00
|
|
|
} else {
|
2025-07-01 20:33:33 +02:00
|
|
|
log.Println("Keine neuen Benachrichtigungen")
|
2025-07-01 20:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range notifications {
|
2025-07-01 20:33:33 +02:00
|
|
|
title := fmt.Sprintf("Neue Benachrichtigung von %s", n.Account.DisplayName)
|
2025-07-01 20:28:36 +02:00
|
|
|
if err := sendToNtfy(n); err != nil {
|
2025-07-01 20:33:33 +02:00
|
|
|
log.Printf("Fehler beim Senden von '%s': %v", title, err)
|
2025-07-01 20:29:31 +02:00
|
|
|
} else {
|
2025-07-01 20:33:33 +02:00
|
|
|
log.Printf("Benachrichtigung gesendet: '%s' (Typ: %s)", title, n.Type)
|
2025-07-01 20:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchNotifications(sinceID string) ([]Notification, error) {
|
|
|
|
req, err := http.NewRequestWithContext(
|
|
|
|
context.Background(),
|
|
|
|
"GET",
|
|
|
|
gotosocialURL+"/api/v1/notifications",
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Add("limit", "10")
|
|
|
|
if sinceID != "" {
|
|
|
|
q.Add("since_id", sinceID)
|
|
|
|
}
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("Statuscode: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var notifications []Notification
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(¬ifications); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifications, nil
|
|
|
|
}
|
|
|
|
|
2025-07-01 20:33:33 +02:00
|
|
|
// HTML zu Plaintext konvertieren
|
|
|
|
func htmlToPlaintext(html string) string {
|
|
|
|
re := regexp.MustCompile(`<[^>]*>`)
|
|
|
|
plain := re.ReplaceAllString(html, "")
|
|
|
|
plain = strings.ReplaceAll(plain, "<", "<")
|
|
|
|
plain = strings.ReplaceAll(plain, ">", ">")
|
|
|
|
plain = strings.ReplaceAll(plain, "&", "&")
|
|
|
|
plain = strings.ReplaceAll(plain, """, "\"")
|
|
|
|
plain = strings.ReplaceAll(plain, "'", "'")
|
|
|
|
return plain
|
|
|
|
}
|
|
|
|
|
2025-07-01 20:28:36 +02:00
|
|
|
func sendToNtfy(n Notification) error {
|
2025-07-01 20:33:33 +02:00
|
|
|
plainContent := htmlToPlaintext(n.Status.Content)
|
|
|
|
title := fmt.Sprintf("Neue Benachrichtigung von %s", n.Account.DisplayName)
|
2025-07-01 20:38:44 +02:00
|
|
|
|
|
|
|
var actions []NtfyAction
|
|
|
|
if n.Status.URL != "" {
|
|
|
|
actions = []NtfyAction{
|
|
|
|
{
|
|
|
|
Action: "view",
|
|
|
|
Label: "Beitrag anzeigen",
|
|
|
|
URL: n.Status.URL,
|
|
|
|
Clear: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-01 20:28:36 +02:00
|
|
|
msg := NtfyMessage{
|
2025-07-01 20:38:44 +02:00
|
|
|
Topic: ntfyTopic,
|
|
|
|
Title: title,
|
|
|
|
Message: fmt.Sprintf("Typ: %s\n\n%s", n.Type, plainContent),
|
|
|
|
Tags: []string{"bell", "incoming_envelope"},
|
2025-07-01 20:28:36 +02:00
|
|
|
Priority: 4,
|
2025-07-01 20:38:44 +02:00
|
|
|
Actions: actions,
|
2025-07-01 20:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonData, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(
|
|
|
|
"POST",
|
|
|
|
ntfyServer,
|
|
|
|
bytes.NewBuffer(jsonData),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
if ntfyToken != "" {
|
|
|
|
req.Header.Set("Authorization", "Bearer "+ntfyToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("ntfy antwortete mit: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|