forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextmagic.go
50 lines (41 loc) · 1.27 KB
/
textmagic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package textmagic
import (
"context"
"strings"
textMagic "github.com/textmagic/textmagic-rest-go-v2/v2"
)
// Service allow you to configure a TextMagic SDK client.
type Service struct {
userName string
apiKey string
phoneNumbers []string
client *textMagic.APIClient
}
// New creates a new text magic client. Use your user-name and API key from
// https://my.textmagic.com/online/api/rest-api/keys.
func New(userName, apiKey string) *Service {
config := textMagic.NewConfiguration()
client := textMagic.NewAPIClient(config)
return &Service{
client: client,
userName: userName,
apiKey: apiKey,
}
}
// AddReceivers adds the given phone numbers to the notifier.
func (s *Service) AddReceivers(phoneNumbers ...string) {
s.phoneNumbers = append(s.phoneNumbers, phoneNumbers...)
}
// Send sends a SMS via TextMagic to all previously added receivers.
func (s *Service) Send(ctx context.Context, subject, message string) error {
auth := context.WithValue(ctx, textMagic.ContextBasicAuth, textMagic.BasicAuth{
UserName: s.userName,
Password: s.apiKey,
})
text := subject + "\n" + message
_, _, err := s.client.TextMagicApi.SendMessage(auth, textMagic.SendMessageInputObject{
Text: text,
Phones: strings.Join(s.phoneNumbers, ","),
})
return err
}