mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-21 23:37:31 +00:00
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
|
package twilio
|
||
|
|
||
|
import (
|
||
|
"github.com/caos/logging"
|
||
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
||
|
"github.com/caos/zitadel/internal/notification/providers"
|
||
|
twilio "github.com/kevinburke/twilio-go"
|
||
|
)
|
||
|
|
||
|
type Twilio struct {
|
||
|
client *twilio.Client
|
||
|
}
|
||
|
|
||
|
func InitTwilioProvider(config TwilioConfig) *Twilio {
|
||
|
return &Twilio{
|
||
|
client: twilio.NewClient(config.SID, config.Token, nil),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (t *Twilio) CanHandleMessage(message providers.Message) bool {
|
||
|
twilioMsg, ok := message.(*TwilioMessage)
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
return twilioMsg.Content != "" && twilioMsg.RecipientPhoneNumber != "" && twilioMsg.SenderPhoneNumber != ""
|
||
|
}
|
||
|
|
||
|
func (t *Twilio) HandleMessage(message providers.Message) error {
|
||
|
twilioMsg, ok := message.(*TwilioMessage)
|
||
|
if !ok {
|
||
|
return caos_errs.ThrowInternal(nil, "TWILI-s0pLc", "message is not TwilioMessage")
|
||
|
}
|
||
|
m, err := t.client.Messages.SendMessage(twilioMsg.SenderPhoneNumber, twilioMsg.RecipientPhoneNumber, twilioMsg.GetContent(), nil)
|
||
|
if err != nil {
|
||
|
return caos_errs.ThrowInternal(err, "TWILI-osk3S", "could not send message")
|
||
|
}
|
||
|
logging.LogWithFields("SMS_-f335c523", "message_sid", m.Sid, "status", m.Status).Debug("sms sent")
|
||
|
return nil
|
||
|
}
|