Skip to content
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

Adaptive Cards Support #3799

Open
wants to merge 3 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
4 changes: 1 addition & 3 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ var (
NotifierConfig: NotifierConfig{
VSendResolved: true,
},
Title: `{{ template "msteams.default.title" . }}`,
Summary: `{{ template "msteams.default.summary" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
}
)

Expand Down
6 changes: 0 additions & 6 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -918,12 +918,6 @@ Microsoft Teams notifications are sent via the [Incoming Webhooks](https://learn
[ webhook_url: <secret> ]
[ webhook_url_file: <filepath> ]

# Message title template.
[ title: <tmpl_string> | default = '{{ template "msteams.default.title" . }}' ]

# Message summary template.
[ summary: <tmpl_string> | default = '{{ template "msteams.default.summary" . }}' ]

# Message body template.
[ text: <tmpl_string> | default = '{{ template "msteams.default.text" . }}' ]

Expand Down
69 changes: 18 additions & 51 deletions notify/msteams/msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,19 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"

"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"
)

const (
colorRed = "8C1A1A"
colorGreen = "2DC72D"
colorGrey = "808080"
)

type Notifier struct {
conf *config.MSTeamsConfig
tmpl *template.Template
Expand All @@ -50,14 +40,16 @@ type Notifier struct {
postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error)
}

// Message card reference can be found at https://learn.microsoft.com/en-us/outlook/actionable-messages/message-card-reference.
// Adaptive card reference can be found at https://learn.microsoft.com/en-us/power-automate/overview-adaptive-cards
type teamsMessage struct {
Context string `json:"@context"`
Type string `json:"type"`
Title string `json:"title"`
Summary string `json:"summary"`
Text string `json:"text"`
ThemeColor string `json:"themeColor"`
Type string `json:"type"`
Attachments []teamsMessageAttachments `json:"attachments"`
}

type teamsMessageAttachments struct {
ContentType string `json:"contentType"`
Content *json.RawMessage `json:"content"`
//ContentURL string `json:"contentUrl"`
}

// New returns a new notifier that uses the Microsoft Teams Webhook API.
Expand Down Expand Up @@ -94,54 +86,29 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}

title := tmpl(n.conf.Title)
if err != nil {
return false, err
}
text := tmpl(n.conf.Text)
if err != nil {
return false, err
}
summary := tmpl(n.conf.Summary)
if err != nil {
return false, err
}

alerts := types.Alerts(as...)
color := colorGrey
switch alerts.Status() {
case model.AlertFiring:
color = colorRed
case model.AlertResolved:
color = colorGreen
}

var url string
if n.conf.WebhookURL != nil {
url = n.conf.WebhookURL.String()
} else {
content, err := os.ReadFile(n.conf.WebhookURLFile)
if err != nil {
return false, fmt.Errorf("read webhook_url_file: %w", err)
}
url = strings.TrimSpace(string(content))
}
textJson := json.RawMessage(text)

t := teamsMessage{
Context: "http://schema.org/extensions",
Type: "MessageCard",
Title: title,
Summary: summary,
Text: text,
ThemeColor: color,
Type: "message",
Attachments: []teamsMessageAttachments{
{
ContentType: "application/vnd.microsoft.card.adaptive",
Content: &textJson,
},
},
}

var payload bytes.Buffer
if err = json.NewEncoder(&payload).Encode(t); err != nil {
return false, err
}

resp, err := n.postJSONFunc(ctx, n.client, url, &payload)
resp, err := n.postJSONFunc(ctx, n.client, n.webhookURL.String(), &payload)
if err != nil {
return true, notify.RedactURL(err)
}
Expand Down
28 changes: 1 addition & 27 deletions notify/msteams/msteams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,10 @@ func TestMSTeamsTemplating(t *testing.T) {
{
title: "full-blown message",
cfg: &config.MSTeamsConfig{
Title: `{{ template "msteams.default.title" . }}`,
Summary: `{{ template "msteams.default.summary" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
},
retry: false,
},
{
title: "title with templating errors",
cfg: &config.MSTeamsConfig{
Title: "{{ ",
},
errMsg: "template: :1: unclosed action",
},
{
title: "summary with templating errors",
cfg: &config.MSTeamsConfig{
Title: `{{ template "msteams.default.title" . }}`,
Summary: "{{ ",
},
errMsg: "template: :1: unclosed action",
},
{
title: "message with templating errors",
cfg: &config.MSTeamsConfig{
Title: `{{ template "msteams.default.title" . }}`,
Summary: `{{ template "msteams.default.summary" . }}`,
Text: "{{ ",
},
errMsg: "template: :1: unclosed action",
},
} {
t.Run(tc.title, func(t *testing.T) {
tc.cfg.WebhookURL = &config.SecretURL{URL: u}
Expand Down
Loading