apprise.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package apprise
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "log/slog"
  10. "net/http"
  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/urllib"
  16. "miniflux.app/v2/internal/version"
  17. )
  18. const defaultClientTimeout = 10 * time.Second
  19. type Client struct {
  20. servicesURL string
  21. baseURL string
  22. }
  23. func NewClient(serviceURL, baseURL string) *Client {
  24. return &Client{servicesURL: serviceURL, baseURL: baseURL}
  25. }
  26. func (c *Client) SendNotification(feed *model.Feed, entries model.Entries) error {
  27. if c.baseURL == "" || c.servicesURL == "" {
  28. return errors.New("apprise: missing base URL or services URL")
  29. }
  30. for _, entry := range entries {
  31. message := "[" + entry.Title + "]" + "(" + entry.URL + ")" + "\n\n"
  32. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/notify")
  33. if err != nil {
  34. return fmt.Errorf(`apprise: invalid API endpoint: %v`, err)
  35. }
  36. requestBody, err := json.Marshal(map[string]any{
  37. "urls": c.servicesURL,
  38. "body": message,
  39. "title": feed.Title,
  40. })
  41. if err != nil {
  42. return fmt.Errorf("apprise: unable to encode request body: %v", err)
  43. }
  44. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  45. if err != nil {
  46. return fmt.Errorf("apprise: unable to create request: %v", err)
  47. }
  48. request.Header.Set("Content-Type", "application/json")
  49. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  50. slog.Debug("Sending Apprise notification",
  51. slog.String("apprise_url", c.baseURL),
  52. slog.String("services_url", c.servicesURL),
  53. slog.String("title", feed.Title),
  54. slog.String("body", message),
  55. slog.String("entry_url", entry.URL),
  56. )
  57. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout, BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks()})
  58. response, err := httpClient.Do(request)
  59. if err != nil {
  60. return fmt.Errorf("apprise: unable to send request: %v", err)
  61. }
  62. defer response.Body.Close()
  63. if response.StatusCode >= 400 {
  64. return fmt.Errorf("apprise: unable to send a notification: url=%s status=%d", apiEndpoint, response.StatusCode)
  65. }
  66. }
  67. return nil
  68. }