ntfy.go 3.7 KB

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