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

migrate connector to power platform for msteams #3921

Closed
Closed
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
14 changes: 13 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,18 @@ tls_config:

### `<msteams_config>`

Microsoft Teams notifications are sent via the [Incoming Webhooks](https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/what-are-webhooks-and-connectors) API endpoint.
Microsoft Teams notifications are sent via the [Workflow](https://support.microsoft.com/en-us/office/creating-a-workflow-from-a-channel-in-teams-242eb8f2-f328-45be-b81f-9817b51a5f0e).

#### How to create a workflow

1. Select the workflow template, `Post to a channel when a webhook request is received`
2. Select `Microsoft Teams Team` you want to receive the notification
3. Select `Microsoft Teams Channel` you want to receive the notification

After that, you can get the webhook URL.

A Power Automate license is required to use this feature. You can get details
from [Types of Power Automate licenses](https://learn.microsoft.com/en-us/power-platform/admin/power-automate-licensing/types) and [Office 365 licenses](https://learn.microsoft.com/en-us/power-platform/admin/power-automate-licensing/faqs#what-power-automate-capabilities-are-included-in-office-365-licenses).

```yaml
# Whether to notify about resolved alerts.
Expand All @@ -934,6 +945,7 @@ Microsoft Teams notifications are sent via the [Incoming Webhooks](https://learn
# Message title template.
[ title: <tmpl_string> | default = '{{ template "msteams.default.title" . }}' ]

# DEPRECATED: Will be removed in future release.
# Message summary template.
[ summary: <tmpl_string> | default = '{{ template "msteams.default.summary" . }}' ]

Expand Down
76 changes: 56 additions & 20 deletions notify/msteams/msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ import (
)

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

type Notifier struct {
Expand All @@ -50,14 +50,33 @@ 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.
// https://learn.microsoft.com/en-us/connectors/teams/?tabs=text1#adaptivecarditemschema
type Content struct {
Schema string `json:"$schema"`
Type string `json:"type"`
Version string `json:"version"`
Body []Body `json:"body"`
}

type Body struct {
Type string `json:"type"`
Text string `json:"text"`
Weight string `json:"weigth,omitempty"`
Size string `json:"size,omitempty"`
Wrap bool `json:"wrap,omitempty"`
Style string `json:"style,omitempty"`
Color string `json:"color,omitempty"`
}

type Attachment struct {
ContentType string `json:"contentType"`
ContentURL *string `json:"contentUrl"` // Use a pointer to handle null values
Content Content `json:"content"`
}

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 []Attachment `json:"attachments"`
}

// New returns a new notifier that uses the Microsoft Teams Webhook API.
Expand Down Expand Up @@ -102,10 +121,6 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if err != nil {
return false, err
}
summary := tmpl(n.conf.Summary)
if err != nil {
return false, err
}

alerts := types.Alerts(as...)
color := colorGrey
Expand All @@ -128,12 +143,33 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}

t := teamsMessage{
Context: "http://schema.org/extensions",
Type: "MessageCard",
Title: title,
Summary: summary,
Text: text,
ThemeColor: color,
Type: "message",
Attachments: []Attachment{
{
ContentType: "application/vnd.microsoft.card.adaptive",
ContentURL: nil,
Content: Content{
Schema: "http://adaptivecards.io/schemas/adaptive-card.json",
Type: "AdaptiveCard",
Version: "1.2",
Body: []Body{
{
Type: "TextBlock",
Text: title,
Weight: "Bolder",
Size: "Medium",
Wrap: true,
Style: "heading",
Color: color,
},
{
Type: "TextBlock",
Text: text,
},
},
},
},
},
}

var payload bytes.Buffer
Expand Down
18 changes: 4 additions & 14 deletions notify/msteams/msteams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ 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" . }}`,
Title: `{{ template "msteams.default.title" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
},
retry: false,
},
Expand All @@ -91,20 +90,11 @@ func TestMSTeamsTemplating(t *testing.T) {
},
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: "{{ ",
Title: `{{ template "msteams.default.title" . }}`,
Text: "{{ ",
},
errMsg: "template: :1: unclosed action",
},
Expand Down
Loading