Skip to content

#3925 Enable Sending AdaptiveCards to Webex for Enhanced Alert Notifi… #3926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,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.
Expand Down
27 changes: 23 additions & 4 deletions notify/webex/webex.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ func New(c *config.WebexConfig, t *template.Template, l *slog.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.
Expand All @@ -89,12 +94,26 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if truncated {
n.logger.Debug("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
Expand Down