ntfy.go 4.0 KB

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