github.com/simpleiot/simpleiot@v0.18.3/msg/twilio.go (about)

     1  package msg
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/kevinburke/twilio-go"
     7  )
     8  
     9  // Twilio can be used to send messages through Twilio
    10  type Twilio struct {
    11  	twilioClient *twilio.Client
    12  	smsFrom      string
    13  }
    14  
    15  // NewTwilio creates a new messenger object
    16  func NewTwilio(twilioSid, twilioAuth, smsFrom string) *Twilio {
    17  	return &Twilio{
    18  		twilioClient: twilio.NewClient(twilioSid, twilioAuth, nil),
    19  		smsFrom:      smsFrom,
    20  	}
    21  }
    22  
    23  // SendSMS sends a sms message
    24  func (m *Twilio) SendSMS(to, msg string) error {
    25  	if m.twilioClient == nil {
    26  		return errors.New("Twilio not set up")
    27  	}
    28  
    29  	ret, err := m.twilioClient.Messages.SendMessage(m.smsFrom, to, msg, nil)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	if ret.ErrorCode != 0 {
    35  		return errors.New(ret.ErrorMessage)
    36  	}
    37  
    38  	return nil
    39  }