Debian Codegraph @

pkg/services/sms/twilio/twilio_service.go

raw
1 package twilio
2
3 import (
4 "errors"
5 "fmt"
6 "net/url"
7
8 "github.com/nicholas-fedor/shoutrrr/pkg/format"
9 "github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
10 "github.com/nicholas-fedor/shoutrrr/pkg/types"
11 )
12
13 // Service provides the Twilio SMS notification service.
14 type Service struct {
15 standard.Standard
16
17 Config *Config
18 pkr format.PropKeyResolver
19 HTTPClient HTTPClient
20 }
21
22 // GetID returns the service identifier.
23 func (s *Service) GetID() string {
24 return Scheme
25 }
26
27 // Initialize configures the service with a URL and logger.
28 func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error {
29 s.SetLogger(logger)
30 s.Config = &Config{}
31 s.pkr = format.NewPropKeyResolver(s.Config)
32 s.HTTPClient = DefaultHTTPClient()
33
34 err := s.Config.setURL(&s.pkr, serviceURL)
35 if err != nil {
36 return err
37 }
38
39 return nil
40 }
41
42 // Send delivers an SMS message via Twilio to all configured recipients.
43 func (s *Service) Send(message string, params *types.Params) error {
44 config := s.Config
45
46 err := s.pkr.UpdateConfigFromParams(config, params)
47 if err != nil {
48 return fmt.Errorf("updating config from params: %w", err)
49 }
50
51 var errs []error
52
53 for _, toNumber := range config.ToNumbers {
54 err := s.sendToRecipient(config, toNumber, message)
55 if err != nil {
56 errs = append(errs, fmt.Errorf("sending to %s: %w", toNumber, err))
57 }
58 }
59
60 return errors.Join(errs...)
61 }