apprise.go 2.1 KB

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