| 1 |
package twilio |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"slices" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 |
) |
| 12 |
|
| 13 |
// Config for the Twilio SMS notification service. |
| 14 |
type Config struct { |
| 15 |
AccountSID string `desc:"Twilio Account SID" required:"" url:"user"` |
| 16 |
AuthToken string `desc:"Twilio Auth Token" required:"" url:"password"` |
| 17 |
FromNumber string `desc:"Sender phone number or Messaging Service SID" required:"" url:"host"` |
| 18 |
ToNumbers []string `desc:"Recipient phone number(s)" required:"" url:"path"` |
| 19 |
Title string `desc:"Notification title" default:"" key:"title" optional:""` |
| 20 |
} |
| 21 |
|
| 22 |
// Scheme is the identifying part of this service's configuration URL. |
| 23 |
const Scheme = "twilio" |
| 24 |
|
| 25 |
// phoneReplacer strips common formatting characters from phone numbers. |
| 26 |
var phoneReplacer = strings.NewReplacer( |
| 27 |
" ", "", |
| 28 |
"-", "", |
| 29 |
"(", "", |
| 30 |
")", "", |
| 31 |
".", "", |
| 32 |
) |
| 33 |
|
| 34 |
// Enums returns the fields that should use a corresponding EnumFormatter to Print/Parse their values. |
| 35 |
func (*Config) Enums() map[string]types.EnumFormatter { |
| 36 |
return map[string]types.EnumFormatter{} |
| 37 |
} |
| 38 |
|
| 39 |
// GetURL returns a URL representation of its current field values. |
| 40 |
func (c *Config) GetURL() *url.URL { |
| 41 |
resolver := format.NewPropKeyResolver(c) |
| 42 |
|
| 43 |
return c.getURL(&resolver) |
| 44 |
} |
| 45 |
|
| 46 |
// SetURL updates the Config from a URL representation of its field values. |
| 47 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 48 |
resolver := format.NewPropKeyResolver(c) |
| 49 |
|
| 50 |
return c.setURL(&resolver, serviceURL) |
| 51 |
} |
| 52 |
|
| 53 |
// getURL constructs a URL from the Config's fields using the provided resolver. |
| 54 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 55 |
path := "/" + strings.Join(c.ToNumbers, "/") |
| 56 |
|
| 57 |
return &url.URL{ |
| 58 |
Scheme: Scheme, |
| 59 |
User: url.UserPassword(c.AccountSID, c.AuthToken), |
| 60 |
Host: c.FromNumber, |
| 61 |
Path: path, |
| 62 |
ForceQuery: true, |
| 63 |
RawQuery: format.BuildQuery(resolver), |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// setURL updates the Config from a URL using the provided resolver. |
| 68 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 69 |
c.AccountSID = serviceURL.User.Username() |
| 70 |
|
| 71 |
password, _ := serviceURL.User.Password() |
| 72 |
c.AuthToken = password |
| 73 |
|
| 74 |
c.FromNumber = normalizePhoneNumber(serviceURL.Host) |
| 75 |
c.ToNumbers = parseToNumbers(serviceURL.Path) |
| 76 |
|
| 77 |
for key, vals := range serviceURL.Query() { |
| 78 |
err := resolver.Set(key, vals[0]) |
| 79 |
if err != nil { |
| 80 |
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if serviceURL.String() != "twilio://[email protected]" { |
| 85 |
return c.validate() |
| 86 |
} |
| 87 |
|
| 88 |
return nil |
| 89 |
} |
| 90 |
|
| 91 |
// validate checks that all required Config fields are present and consistent. |
| 92 |
func (c *Config) validate() error { |
| 93 |
if c.AccountSID == "" { |
| 94 |
return ErrAccountSIDMissing |
| 95 |
} |
| 96 |
|
| 97 |
if c.AuthToken == "" { |
| 98 |
return ErrAuthTokenMissing |
| 99 |
} |
| 100 |
|
| 101 |
if c.FromNumber == "" { |
| 102 |
return ErrFromNumberMissing |
| 103 |
} |
| 104 |
|
| 105 |
if len(c.ToNumbers) == 0 { |
| 106 |
return ErrToNumbersMissing |
| 107 |
} |
| 108 |
|
| 109 |
// Twilio rejects calls/messages where To == From. |
| 110 |
if !strings.HasPrefix(c.FromNumber, msgServicePrefix) { |
| 111 |
if slices.Contains(c.ToNumbers, c.FromNumber) { |
| 112 |
return ErrToFromNumberSame |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
// normalizePhoneNumber strips common formatting characters (spaces, dashes, |
| 120 |
// parentheses, dots) from a phone number string, leaving only digits and a |
| 121 |
// leading '+'. Messaging Service SIDs (starting with "MG") are returned as-is. |
| 122 |
func normalizePhoneNumber(number string) string { |
| 123 |
if strings.HasPrefix(number, msgServicePrefix) { |
| 124 |
return number |
| 125 |
} |
| 126 |
|
| 127 |
return phoneReplacer.Replace(number) |
| 128 |
} |
| 129 |
|
| 130 |
// parseToNumbers extracts and normalizes recipient phone numbers from the URL path. |
| 131 |
func parseToNumbers(path string) []string { |
| 132 |
rawPath := strings.TrimPrefix(path, "/") |
| 133 |
if rawPath == "" { |
| 134 |
return nil |
| 135 |
} |
| 136 |
|
| 137 |
parts := strings.Split(rawPath, "/") |
| 138 |
numbers := make([]string, 0, len(parts)) |
| 139 |
|
| 140 |
for _, p := range parts { |
| 141 |
n := normalizePhoneNumber(strings.TrimSpace(p)) |
| 142 |
if n != "" { |
| 143 |
numbers = append(numbers, n) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return numbers |
| 148 |
} |