1
0
mirror of https://github.com/nikoksr/notify.git synced 2025-02-15 13:33:26 +02:00

minor fixes

This commit is contained in:
Himanshu Ranjan 2021-05-07 09:25:49 +05:30
parent de681c4ff1
commit 02fe785e94

View File

@ -1,53 +1,54 @@
package textmagic package textmagic
import ( import (
"context" "context"
tm "github.com/textmagic/textmagic-rest-go-v2/v2" tm "github.com/textmagic/textmagic-rest-go-v2/v2"
"strings" "strings"
) )
// TextMagicClient allow you to configure a TextMagic SDK client. // TMagicClient allow you to configure a TextMagic SDK client.
type TMagicService struct { type TMagicService struct {
UserName string UserName string
APIKey string APIKey string
destinations []string destinations []string
TextMagicAPIClient *tm.APIClient TextMagicAPIClient *tm.APIClient
} }
// NewTextMagicClient creates a new text magic client
func NewTextMagicClient(userName, apiKey string) *TMagicService { func NewTextMagicClient(userName, apiKey string) *TMagicService {
config := tm.NewConfiguration() config := tm.NewConfiguration()
client := tm.NewAPIClient(config) client := tm.NewAPIClient(config)
return &TMagicService{ return &TMagicService{
TextMagicAPIClient: client, TextMagicAPIClient: client,
UserName: userName, UserName: userName,
APIKey: apiKey, APIKey: apiKey,
} }
} }
// AddReceivers adds the given destination phone numbers to the notifier. // AddReceivers adds the given destination phone numbers to the notifier.
func (s *TMagicService) AddReceivers(phoneNumbers ...string) { func (s *TMagicService) AddReceivers(phoneNumbers ...string) {
s.destinations = append(s.destinations, phoneNumbers...) s.destinations = append(s.destinations, phoneNumbers...)
} }
// Send sends a SMS via TextMagic to all previously added receivers. // Send sends a SMS via TextMagic to all previously added receivers.
func (s *TMagicService) Send(ctx context.Context, subject, message string) error { func (s *TMagicService) Send(ctx context.Context, subject, message string) error {
// put your Username and API Key from https://my.textmagic.com/online/api/rest-api/keys page. // put your Username and API Key from https://my.textmagic.com/online/api/rest-api/keys page.
auth := context.WithValue(context.Background(), tm.ContextBasicAuth, tm.BasicAuth{ auth := context.WithValue(context.Background(), tm.ContextBasicAuth, tm.BasicAuth{
UserName: s.UserName, UserName: s.UserName,
Password: s.APIKey, Password: s.APIKey,
}) })
text := subject + "\n" + message text := subject + "\n" + message
_, _, err := s.TextMagicAPIClient.TextMagicApi.SendMessage(auth, tm.SendMessageInputObject{ _, _, err := s.TextMagicAPIClient.TextMagicApi.SendMessage(auth, tm.SendMessageInputObject{
Text: text, Text: text,
Phones: strings.Join(s.destinations, ","), Phones: strings.Join(s.destinations, ","),
}) })
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }