Hinzufügen eines Action Buttons um den Beitrag anzuzgeigen
This commit is contained in:
parent
7d6767f2e1
commit
1ed5bdfdc9
1 changed files with 34 additions and 15 deletions
31
main.go
31
main.go
|
@ -29,18 +29,29 @@ type Notification struct {
|
|||
CreatedAt string `json:"created_at"`
|
||||
Account struct {
|
||||
DisplayName string `json:"display_name"`
|
||||
Acct string `json:"acct"`
|
||||
} `json:"account"`
|
||||
Status struct {
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
URL string `json:"url"` // Original-URL des Posts
|
||||
} `json:"status"`
|
||||
}
|
||||
|
||||
type NtfyAction struct {
|
||||
Action string `json:"action"`
|
||||
Label string `json:"label"`
|
||||
URL string `json:"url"`
|
||||
Clear bool `json:"clear"`
|
||||
}
|
||||
|
||||
type NtfyMessage struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -129,31 +140,39 @@ func fetchNotifications(sinceID string) ([]Notification, error) {
|
|||
|
||||
// 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)
|
||||
|
||||
var actions []NtfyAction
|
||||
if n.Status.URL != "" {
|
||||
actions = []NtfyAction{
|
||||
{
|
||||
Action: "view",
|
||||
Label: "Beitrag anzeigen",
|
||||
URL: n.Status.URL,
|
||||
Clear: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
msg := NtfyMessage{
|
||||
Topic: ntfyTopic,
|
||||
Title: title,
|
||||
Message: fmt.Sprintf("Typ: %s\n\n%s", n.Type, plainContent),
|
||||
Tags: []string{"bell", "incoming_envelope"},
|
||||
Priority: 4,
|
||||
Actions: actions,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(msg)
|
||||
|
|
Loading…
Add table
Reference in a new issue