ntfy.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ntfy // import "miniflux.app/v2/internal/integration/ntfy"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "fmt"
  8. "log/slog"
  9. "net/http"
  10. "time"
  11. "miniflux.app/v2/internal/model"
  12. "miniflux.app/v2/internal/version"
  13. )
  14. const (
  15. defaultClientTimeout = 10 * time.Second
  16. defaultNtfyURL = "https://ntfy.sh"
  17. )
  18. type Client struct {
  19. ntfyURL, ntfyTopic, ntfyApiToken, ntfyUsername, ntfyPassword, ntfyIconURL string
  20. ntfyPriority int
  21. }
  22. func NewClient(ntfyURL, ntfyTopic, ntfyApiToken, ntfyUsername, ntfyPassword, ntfyIconURL string, ntfyPriority int) *Client {
  23. if ntfyURL == "" {
  24. ntfyURL = defaultNtfyURL
  25. }
  26. return &Client{ntfyURL, ntfyTopic, ntfyApiToken, ntfyUsername, ntfyPassword, ntfyIconURL, ntfyPriority}
  27. }
  28. func (c *Client) SendMessages(feed *model.Feed, entries model.Entries) error {
  29. for _, entry := range entries {
  30. ntfyMessage := &ntfyMessage{
  31. Topic: c.ntfyTopic,
  32. Message: entry.Title,
  33. Title: feed.Title,
  34. Priority: c.ntfyPriority,
  35. Click: entry.URL,
  36. }
  37. if c.ntfyIconURL != "" {
  38. ntfyMessage.Icon = c.ntfyIconURL
  39. }
  40. slog.Debug("Sending Ntfy message",
  41. slog.String("url", c.ntfyURL),
  42. slog.String("topic", c.ntfyTopic),
  43. slog.Int("priority", ntfyMessage.Priority),
  44. slog.String("message", ntfyMessage.Message),
  45. slog.String("entry_url", entry.URL),
  46. )
  47. if err := c.makeRequest(ntfyMessage); err != nil {
  48. return err
  49. }
  50. }
  51. return nil
  52. }
  53. func (c *Client) makeRequest(payload any) error {
  54. requestBody, err := json.Marshal(payload)
  55. if err != nil {
  56. return fmt.Errorf("ntfy: unable to encode request body: %v", err)
  57. }
  58. request, err := http.NewRequest(http.MethodPost, c.ntfyURL, bytes.NewReader(requestBody))
  59. if err != nil {
  60. return fmt.Errorf("ntfy: unable to create request: %v", err)
  61. }
  62. request.Header.Set("Content-Type", "application/json")
  63. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  64. // See https://docs.ntfy.sh/publish/#access-tokens
  65. if c.ntfyApiToken != "" {
  66. request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.ntfyApiToken))
  67. }
  68. // See https://docs.ntfy.sh/publish/#username-password
  69. if c.ntfyUsername != "" && c.ntfyPassword != "" {
  70. request.SetBasicAuth(c.ntfyUsername, c.ntfyPassword)
  71. }
  72. httpClient := &http.Client{Timeout: defaultClientTimeout}
  73. response, err := httpClient.Do(request)
  74. if err != nil {
  75. return fmt.Errorf("ntfy: unable to send request: %v", err)
  76. }
  77. defer response.Body.Close()
  78. if response.StatusCode >= 400 {
  79. return fmt.Errorf("ntfy: incorrect response status code %d for url %s", response.StatusCode, c.ntfyURL)
  80. }
  81. return nil
  82. }
  83. // See https://docs.ntfy.sh/publish/#publish-as-json
  84. type ntfyMessage struct {
  85. Topic string `json:"topic"`
  86. Message string `json:"message"`
  87. Title string `json:"title"`
  88. Tags []string `json:"tags,omitempty"`
  89. Priority int `json:"priority,omitempty"`
  90. Icon string `json:"icon,omitempty"` // https://docs.ntfy.sh/publish/#icons
  91. Click string `json:"click,omitempty"`
  92. Actions []ntfyAction `json:"actions,omitempty"`
  93. }
  94. // See https://docs.ntfy.sh/publish/#action-buttons
  95. type ntfyAction struct {
  96. Action string `json:"action"`
  97. Label string `json:"label"`
  98. URL string `json:"url"`
  99. }