Files
gatus/alerting/provider/twilio/twilio.go
T

56 lines
1.9 KiB
Go
Raw Normal View History

package twilio
import (
"encoding/base64"
"fmt"
2020-11-23 22:20:06 +01:00
"net/http"
"net/url"
"github.com/TwiN/gatus/v3/alerting/alert"
"github.com/TwiN/gatus/v3/alerting/provider/custom"
"github.com/TwiN/gatus/v3/core"
)
2020-09-26 15:15:50 -04:00
// AlertProvider is the configuration necessary for sending an alert using Twilio
type AlertProvider struct {
SID string `yaml:"sid"`
Token string `yaml:"token"`
From string `yaml:"from"`
To string `yaml:"to"`
2021-05-15 21:31:32 -04:00
2021-10-23 16:47:12 -04:00
// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
DefaultAlert *alert.Alert `yaml:"default-alert"`
}
2020-09-26 15:15:50 -04:00
// IsValid returns whether the provider's configuration is valid
func (provider *AlertProvider) IsValid() bool {
return len(provider.Token) > 0 && len(provider.SID) > 0 && len(provider.From) > 0 && len(provider.To) > 0
}
2020-09-26 15:15:50 -04:00
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
2021-10-23 16:47:12 -04:00
func (provider *AlertProvider) ToCustomAlertProvider(endpoint *core.Endpoint, alert *alert.Alert, _ *core.Result, resolved bool) *custom.AlertProvider {
var message string
if resolved {
2021-10-23 16:47:12 -04:00
message = fmt.Sprintf("RESOLVED: %s - %s", endpoint.Name, alert.GetDescription())
} else {
2021-10-23 16:47:12 -04:00
message = fmt.Sprintf("TRIGGERED: %s - %s", endpoint.Name, alert.GetDescription())
}
return &custom.AlertProvider{
2020-10-23 16:29:20 -04:00
URL: fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json", provider.SID),
2020-11-23 22:20:06 +01:00
Method: http.MethodPost,
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)))),
},
}
}
2021-05-15 21:31:32 -04:00
// GetDefaultAlert returns the provider's default alert configuration
func (provider AlertProvider) GetDefaultAlert() *alert.Alert {
2021-05-15 21:31:32 -04:00
return provider.DefaultAlert
}