2020-09-19 22:29:08 +02:00
|
|
|
package twilio
|
2020-09-19 22:22:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2020-09-19 22:29:08 +02:00
|
|
|
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
2020-09-19 22:22:12 +02:00
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2020-09-19 22:29:08 +02:00
|
|
|
type AlertProvider struct {
|
2020-09-19 22:22:12 +02:00
|
|
|
SID string `yaml:"sid"`
|
|
|
|
Token string `yaml:"token"`
|
|
|
|
From string `yaml:"from"`
|
|
|
|
To string `yaml:"to"`
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:29:08 +02:00
|
|
|
func (provider *AlertProvider) IsValid() bool {
|
2020-09-19 22:22:12 +02:00
|
|
|
return len(provider.Token) > 0 && len(provider.SID) > 0 && len(provider.From) > 0 && len(provider.To) > 0
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:29:08 +02:00
|
|
|
func (provider *AlertProvider) ToCustomAlertProvider(message string) *custom.AlertProvider {
|
|
|
|
return &custom.AlertProvider{
|
2020-09-19 22:22:12 +02:00
|
|
|
Url: fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json", provider.SID),
|
|
|
|
Method: "POST",
|
|
|
|
Body: url.Values{
|
|
|
|
"To": {provider.To},
|
|
|
|
"From": {provider.From},
|
|
|
|
"Body": {message},
|
|
|
|
}.Encode(),
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
"Authorization": fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", provider.SID, provider.Token)))),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|