From 2cc8fd5eeda19e4e1aa001f1063a59916382928e Mon Sep 17 00:00:00 2001 From: florianspk Date: Sun, 28 Jul 2024 12:13:02 +0200 Subject: [PATCH] #3925 Enable Sending AdaptiveCards to Webex for Enhanced Alert Notifications Signed-off-by: florianspk --- config/notifiers.go | 6 +++--- notify/webex/webex.go | 27 +++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/config/notifiers.go b/config/notifiers.go index 34bef22cd1..04e18e23cf 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -188,9 +188,9 @@ type WebexConfig struct { NotifierConfig `yaml:",inline" json:",inline"` HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"` APIURL *URL `yaml:"api_url,omitempty" json:"api_url,omitempty"` - - Message string `yaml:"message,omitempty" json:"message,omitempty"` - RoomID string `yaml:"room_id" json:"room_id"` + Message string `yaml:"message,omitempty" json:"message,omitempty"` + RoomID string `yaml:"room_id" json:"room_id"` + AdaptiveCard string `yaml:"adaptive_card,omitempty" json:"adaptive_card,omitempty"` } // UnmarshalYAML implements the yaml.Unmarshaler interface. diff --git a/notify/webex/webex.go b/notify/webex/webex.go index 486a13bf78..cd8d100c60 100644 --- a/notify/webex/webex.go +++ b/notify/webex/webex.go @@ -61,9 +61,14 @@ func New(c *config.WebexConfig, t *template.Template, l log.Logger, httpOpts ... return n, nil } -type webhook struct { - Markdown string `json:"markdown"` - RoomID string `json:"roomId,omitempty"` +type webexMessage struct { + Markdown string `json:"markdown"` + RoomID string `json:"roomId,omitempty"` + Attachments *webexMessageAttachments `json:"attachments,omitempty"` +} +type webexMessageAttachments struct { + ContentType string `json:"contentType"` + Content *json.RawMessage `json:"content"` } // Notify implements the Notifier interface. @@ -90,12 +95,26 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) if truncated { level.Debug(n.logger).Log("msg", "message truncated due to exceeding maximum allowed length by webex", "truncated_message", message) } + var AdaptiveCardJson json.RawMessage + if n.conf.AdaptiveCard != "" { + AdaptiveCard := tmpl(n.conf.AdaptiveCard) + if err != nil { + return false, err + } + AdaptiveCardJson = json.RawMessage(AdaptiveCard) + } - w := webhook{ + w := webexMessage{ Markdown: message, RoomID: tmpl(n.conf.RoomID), } + if len(AdaptiveCardJson) != 0 { + w.Attachments = &webexMessageAttachments{ + ContentType: "application/vnd.microsoft.card.adaptive", + Content: &AdaptiveCardJson, + } + } var payload bytes.Buffer if err = json.NewEncoder(&payload).Encode(w); err != nil { return false, err