1
0
mirror of https://github.com/nikoksr/notify.git synced 2024-11-24 08:22:18 +02:00
notify/service/plivo/plivo.go

111 lines
2.6 KiB
Go

package plivo
import (
"context"
"fmt"
"net/http"
"strings"
plivo "github.com/plivo/plivo-go"
)
// ClientOptions allow you to configure a Plivo SDK client.
type ClientOptions struct {
AuthID string // If empty, env variable PLIVO_AUTH_ID will be used
AuthToken string // If empty, env variable PLIVO_AUTH_TOKEN will be used
// Optional
HTTPClient *http.Client // Bring Your Own Client
}
// MessageOptions allow you to configure options for sending a message.
type MessageOptions struct {
Source string // a Plivo source phone number or a Plivo Powerpack UUID
// Optional
CallbackURL string // URL to which status update callbacks for the message should be sent
CallbackMethod string // The HTTP method to be used when calling CallbackURL - GET or POST(default)
}
// mockPlivoMsgClient abstracts Plivo SDK for writing unit tests
type plivoMsgClient interface {
Create(plivo.MessageCreateParams) (*plivo.MessageCreateResponseBody, error)
}
// Service is a Plivo client
type Service struct {
client plivoMsgClient
mopts MessageOptions
destinations []string
}
// New creates a new instance of plivo service.
func New(cOpts *ClientOptions, mOpts *MessageOptions) (*Service, error) {
if cOpts == nil {
return nil, fmt.Errorf("client-options cannot be nil")
}
if mOpts == nil {
return nil, fmt.Errorf("message-options cannot be nil")
}
if mOpts.Source == "" {
return nil, fmt.Errorf("source cannot be empty")
}
client, err := plivo.NewClient(
cOpts.AuthID,
cOpts.AuthToken,
&plivo.ClientOptions{
HttpClient: cOpts.HTTPClient,
},
)
if err != nil {
return nil, fmt.Errorf("plivo.NewClient() failed: %w", err)
}
return &Service{
client: client.Messages,
mopts: *mOpts,
}, nil
}
// AddReceivers adds the given destination phone numbers to the notifier.
func (s *Service) AddReceivers(phoneNumbers ...string) {
s.destinations = append(s.destinations, phoneNumbers...)
}
// Send sends a SMS via Plivo to all previously added receivers.
func (s *Service) Send(ctx context.Context, subject, message string) error {
// TODO: Same question here.
select {
case <-ctx.Done():
return nil
default:
}
text := subject + "\n" + message
var dst string
switch len(s.destinations) {
case 0:
return fmt.Errorf("no receivers added")
case 1:
dst = s.destinations[0]
default:
// multiple destinations, use bulk message syntax
// see: https://www.plivo.com/docs/sms/api/message#bulk-messaging
dst = strings.Join(s.destinations, "<")
}
_, err := s.client.Create(plivo.MessageCreateParams{
Dst: dst,
Text: text,
Src: s.mopts.Source,
URL: s.mopts.CallbackURL,
Method: s.mopts.CallbackMethod,
})
return err
}