From d9d58154880b1e0c58d5f0536ded2ecd5501dae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imre=20L=C3=A1szl=C3=B3?= <48689330+imrelaszlo@users.noreply.github.com> Date: Wed, 28 May 2025 01:28:20 +0200 Subject: [PATCH] fix(alerting): Support Twilio overrides for `text-twilio-triggered` and `text-twilio-resolved` (#1120) * Make twilio alert provider translatable * Update alerting/provider/twilio/twilio.go --------- Co-authored-by: Imre Laszlo Co-authored-by: TwiN --- alerting/provider/twilio/twilio.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/alerting/provider/twilio/twilio.go b/alerting/provider/twilio/twilio.go index 544397fb..ccb83770 100644 --- a/alerting/provider/twilio/twilio.go +++ b/alerting/provider/twilio/twilio.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/url" + "strings" "github.com/TwiN/gatus/v5/alerting/alert" "github.com/TwiN/gatus/v5/client" @@ -27,6 +28,9 @@ type Config struct { Token string `yaml:"token"` From string `yaml:"from"` To string `yaml:"to"` + + TextTwilioTriggered string `yaml:"text-twilio-triggered,omitempty"` // String used in the SMS body and subject (optional) + TextTwilioResolved string `yaml:"text-twilio-resolved,omitempty"` // String used in the SMS body and subject (optional) } func (cfg *Config) Validate() error { @@ -58,6 +62,12 @@ func (cfg *Config) Merge(override *Config) { if len(override.To) > 0 { cfg.To = override.To } + if len(override.TextTwilioTriggered) > 0 { + cfg.TextTwilioTriggered = override.TextTwilioTriggered + } + if len(override.TextTwilioResolved) > 0 { + cfg.TextTwilioResolved = override.TextTwilioResolved + } } // AlertProvider is the configuration necessary for sending an alert using Twilio @@ -102,9 +112,17 @@ func (provider *AlertProvider) Send(ep *endpoint.Endpoint, alert *alert.Alert, r func (provider *AlertProvider) buildRequestBody(cfg *Config, ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) string { var message string if resolved { - message = fmt.Sprintf("RESOLVED: %s - %s", ep.DisplayName(), alert.GetDescription()) + if len(cfg.TextTwilioResolved) > 0 { + message = strings.Replace(strings.Replace(cfg.TextTwilioResolved, "{endpoint}", ep.DisplayName(), 1), "{description}", alert.GetDescription(), 1) + } else { + message = fmt.Sprintf("RESOLVED: %s - %s", ep.DisplayName(), alert.GetDescription()) + } } else { - message = fmt.Sprintf("TRIGGERED: %s - %s", ep.DisplayName(), alert.GetDescription()) + if len(cfg.TextTwilioTriggered) > 0 { + message = strings.Replace(strings.Replace(cfg.TextTwilioTriggered, "{endpoint}", ep.DisplayName(), 1), "{description}", alert.GetDescription(), 1) + } else { + message = fmt.Sprintf("TRIGGERED: %s - %s", ep.DisplayName(), alert.GetDescription()) + } } return url.Values{ "To": {cfg.To},