Hinzufügen eines Action Buttons um den Beitrag anzuzgeigen

This commit is contained in:
Simon Rieger 2025-07-01 20:38:44 +02:00
parent 7d6767f2e1
commit 1ed5bdfdc9

49
main.go
View file

@ -29,18 +29,29 @@ type Notification struct {
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
Account struct { Account struct {
DisplayName string `json:"display_name"` DisplayName string `json:"display_name"`
Acct string `json:"acct"`
} `json:"account"` } `json:"account"`
Status struct { Status struct {
ID string `json:"id"`
Content string `json:"content"` Content string `json:"content"`
URL string `json:"url"` // Original-URL des Posts
} `json:"status"` } `json:"status"`
} }
type NtfyAction struct {
Action string `json:"action"`
Label string `json:"label"`
URL string `json:"url"`
Clear bool `json:"clear"`
}
type NtfyMessage struct { type NtfyMessage struct {
Topic string `json:"topic"` Topic string `json:"topic"`
Title string `json:"title"` Title string `json:"title"`
Message string `json:"message"` Message string `json:"message"`
Tags []string `json:"tags"` Tags []string `json:"tags"`
Priority int `json:"priority"` Priority int `json:"priority"`
Actions []NtfyAction `json:"actions,omitempty"`
} }
func main() { func main() {
@ -129,31 +140,39 @@ func fetchNotifications(sinceID string) ([]Notification, error) {
// HTML zu Plaintext konvertieren // HTML zu Plaintext konvertieren
func htmlToPlaintext(html string) string { func htmlToPlaintext(html string) string {
// Ersetze HTML-Tags
re := regexp.MustCompile(`<[^>]*>`) re := regexp.MustCompile(`<[^>]*>`)
plain := re.ReplaceAllString(html, "") plain := re.ReplaceAllString(html, "")
// Ersetze HTML-Entities
plain = strings.ReplaceAll(plain, "&lt;", "<") plain = strings.ReplaceAll(plain, "&lt;", "<")
plain = strings.ReplaceAll(plain, "&gt;", ">") plain = strings.ReplaceAll(plain, "&gt;", ">")
plain = strings.ReplaceAll(plain, "&amp;", "&") plain = strings.ReplaceAll(plain, "&amp;", "&")
plain = strings.ReplaceAll(plain, "&quot;", "\"") plain = strings.ReplaceAll(plain, "&quot;", "\"")
plain = strings.ReplaceAll(plain, "&apos;", "'") plain = strings.ReplaceAll(plain, "&apos;", "'")
return plain return plain
} }
func sendToNtfy(n Notification) error { func sendToNtfy(n Notification) error {
// HTML zu Plaintext konvertieren
plainContent := htmlToPlaintext(n.Status.Content) plainContent := htmlToPlaintext(n.Status.Content)
title := fmt.Sprintf("Neue Benachrichtigung von %s", n.Account.DisplayName) title := fmt.Sprintf("Neue Benachrichtigung von %s", n.Account.DisplayName)
var actions []NtfyAction
if n.Status.URL != "" {
actions = []NtfyAction{
{
Action: "view",
Label: "Beitrag anzeigen",
URL: n.Status.URL,
Clear: true,
},
}
}
msg := NtfyMessage{ msg := NtfyMessage{
Topic: ntfyTopic, Topic: ntfyTopic,
Title: title, Title: title,
Message: fmt.Sprintf("Typ: %s\n\n%s", n.Type, plainContent), 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,
Actions: actions,
} }
jsonData, err := json.Marshal(msg) jsonData, err := json.Marshal(msg)