apprise.go 2.1 KB

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