Debian Codegraph @

pkg/services/push/pushover/pushover.go

raw
1 package pushover
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/url"
8 "strconv"
9 "strings"
10 "time"
11
12 "github.com/nicholas-fedor/shoutrrr/pkg/format"
13 "github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
14 "github.com/nicholas-fedor/shoutrrr/pkg/types"
15 )
16
17 // Service provides the Pushover notification service.
18 type Service struct {
19 standard.Standard
20
21 Config *Config
22 pkr format.PropKeyResolver
23 Client *http.Client
24 }
25
26 // hookURL is the Pushover API endpoint for sending messages.
27 const (
28 hookURL = "https://api.pushover.net/1/messages.json"
29 contentType = "application/x-www-form-urlencoded"
30 defaultHTTPTimeout = 10 * time.Second // defaultHTTPTimeout is the default timeout for HTTP requests.
31 )
32
33 // GetID returns the service identifier.
34 func (s *Service) GetID() string {
35 return Scheme
36 }
37
38 // Initialize configures the service with a URL and logger.
39 func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error {
40 s.SetLogger(logger)
41 s.Config = &Config{}
42 s.pkr = format.NewPropKeyResolver(s.Config)
43 s.Client = &http.Client{
44 Timeout: defaultHTTPTimeout,
45 }
46
47 if err := s.Config.setURL(&s.pkr, serviceURL); err != nil {
48 return err
49 }
50
51 return nil
52 }
53
54 // Send delivers a notification message to Pushover.
55 func (s *Service) Send(message string, params *types.Params) error {
56 config := s.Config
57 if err := s.pkr.UpdateConfigFromParams(config, params); err != nil {
58 return fmt.Errorf("updating config from params: %w", err)
59 }
60
61 device := strings.Join(config.Devices, ",")
62 if err := s.sendToDevice(device, message, config); err != nil {
63 return fmt.Errorf("failed to send notifications to pushover devices: %w", err)
64 }
65
66 return nil
67 }
68
69 // sendToDevice sends a notification to a specific Pushover device.
70 func (s *Service) sendToDevice(device, message string, config *Config) error {
71 data := url.Values{}
72 data.Set("device", device)
73 data.Set("user", config.User)
74 data.Set("token", config.Token)
75 data.Set("message", message)
76
77 if config.Title != "" {
78 data.Set("title", config.Title)
79 }
80
81 if config.Priority >= -2 && config.Priority <= 1 {
82 data.Set("priority", strconv.FormatInt(int64(config.Priority), 10))
83 }
84
85 ctx, cancel := context.WithTimeout(context.Background(), defaultHTTPTimeout)
86 defer cancel()
87
88 req, err := http.NewRequestWithContext(
89 ctx,
90 http.MethodPost,
91 hookURL,
92 strings.NewReader(data.Encode()),
93 )
94 if err != nil {
95 return fmt.Errorf("creating request: %w", err)
96 }
97
98 req.Header.Set("Content-Type", contentType)
99
100 res, err := s.Client.Do(req)
101 if err != nil {
102 return fmt.Errorf("sending request to Pushover API: %w", err)
103 }
104
105 defer func() { _ = res.Body.Close() }()
106
107 if res.StatusCode != http.StatusOK {
108 return fmt.Errorf("%w: %q, response status %q", ErrSendFailed, device, res.Status)
109 }
110
111 return nil
112 }