Skip to content

Commit

Permalink
Merge pull request #19 from rakutentech/kevincobain2000/update-teams-…
Browse files Browse the repository at this point in the history
…webhook

Update code to support new Microsoft Teams webhook JSON format
  • Loading branch information
gabriel-vasile authored Jul 22, 2024
2 parents 1cd41ab + 022b223 commit ae31bd9
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 46 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/rakutentech/go-alertnotification
module github.com/rakutentech/go-alertnotification/v2

go 1.21

require (
github.com/GitbookIO/diskache v0.0.0-20161028144708-bfb81bf58cb1
github.com/joho/godotenv v1.3.0
github.com/joho/godotenv v1.5.1
)

require github.com/GitbookIO/syncgroup v0.0.0-20200915204659-4f0b2961ab10 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ github.com/GitbookIO/diskache v0.0.0-20161028144708-bfb81bf58cb1 h1:1ui53h0HCYjy
github.com/GitbookIO/diskache v0.0.0-20161028144708-bfb81bf58cb1/go.mod h1:TTHndD25/UJVOyBl/vOq2g5RIg4bidGlmtzb+4Zr+Nw=
github.com/GitbookIO/syncgroup v0.0.0-20200915204659-4f0b2961ab10 h1:G9KsBi5RxXROehPm+TSvTrFXShD613GLKrv9ctY1hFE=
github.com/GitbookIO/syncgroup v0.0.0-20200915204659-4f0b2961ab10/go.mod h1:QEGLOlzj5q/UbkPM0viAulgbdRUpsU3/6HVA9YUA9BU=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
151 changes: 109 additions & 42 deletions ms_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,82 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
"time"
)

// MsTeam is MessageCard for Team notification
// MsTeam is Adaptive Card for Team notification
type MsTeam struct {
Type string `json:"@type"`
Context string `json:"@context"`
Summary string `json:"summary"`
ThemeColor string `json:"themeColor"`
Title string `json:"title"`
Sections []SectionStruct `json:"sections"`
Type string `json:"type"`
Attachments []attachment `json:"attachments"`
}

// SectionStruct is sub-struct of MsTeam
type SectionStruct struct {
ActivityTitle string `json:"activityTitle"`
ActivitySubtitle string `json:"activitySubtitle"`
ActivityImage string `json:"activityImage"`
Facts []FactStruct `json:"facts"`
type attachment struct {
ContentType string `json:"contentType"`
ContentURL *string `json:"contentUrl"`
Content cardContent `json:"content"`
}

// FactStruct is sub-struct of SectionStruct
type FactStruct struct {
Name string `json:"name"`
type cardContent struct {
Schema string `json:"$schema"`
Type string `json:"type"`
Version string `json:"version"`
AccentColor string `json:"accentColor"`
Body []interface{} `json:"body"`
Actions []action `json:"actions"`
MSTeams msTeams `json:"msteams"`
}

type textBlock struct {
Type string `json:"type"`
Text string `json:"text"`
ID string `json:"id,omitempty"`
Size string `json:"size,omitempty"`
Weight string `json:"weight,omitempty"`
Color string `json:"color,omitempty"`
}

type fact struct {
Title string `json:"title"`
Value string `json:"value"`
}

type factSet struct {
Type string `json:"type"`
Facts []fact `json:"facts"`
ID string `json:"id"`
}

type codeBlock struct {
Type string `json:"type"`
CodeSnippet string `json:"codeSnippet"`
FontType string `json:"fontType"`
Wrap bool `json:"wrap"`
}

type action struct {
Type string `json:"type"`
Title string `json:"title"`
URL string `json:"url"`
}

type msTeams struct {
Width string `json:"width"`
}

// NewMsTeam is used to create MsTeam
func NewMsTeam(err error, expandos *Expandos) MsTeam {
title := os.Getenv("ALERT_CARD_SUBJECT")
summary := os.Getenv("MS_TEAMS_CARD_SUBJECT")
errMsg := fmt.Sprintf("%+v", err)
hostname, err := os.Hostname()
if err != nil {
hostname = "hostname_unknown"
}
hostname += " " + os.Getenv("APP_NAME")
// apply expandos on card
if expandos != nil {
if expandos.MsTeamsAlertCardSubject != "" {
Expand All @@ -54,31 +94,58 @@ func NewMsTeam(err error, expandos *Expandos) MsTeam {
}
}

notificationCard := MsTeam{
Type: "MessageCard",
Context: "http://schema.org/extensions",
Summary: summary,
ThemeColor: os.Getenv("ALERT_THEME_COLOR"),
Title: title,
Sections: []SectionStruct{
SectionStruct{
ActivityTitle: summary,
ActivitySubtitle: fmt.Sprintf("error has occured on %v", os.Getenv("APP_NAME")),
ActivityImage: "",
Facts: []FactStruct{
FactStruct{
Name: "Environment:",
Value: os.Getenv("APP_ENV"),
return MsTeam{
Type: "message",
Attachments: []attachment{
{
ContentType: "application/vnd.microsoft.card.adaptive",
ContentURL: nil,
Content: cardContent{
Schema: "http://adaptivecards.io/schemas/adaptive-card.json",
Type: "AdaptiveCard",
Version: "1.4",
AccentColor: "bf0000",
Body: []interface{}{
textBlock{
Type: "TextBlock",
Text: title,
ID: "title",
Size: "large",
Weight: "bolder",
Color: "accent",
},
factSet{
Type: "FactSet",
Facts: []fact{
{
Title: "Title:",
Value: title,
},
{
Title: "Summary:",
Value: summary,
},
{
Title: "Hostname:",
Value: hostname,
},
},
ID: "acFactSet",
},
codeBlock{
Type: "CodeBlock",
CodeSnippet: errMsg,
FontType: "monospace",
Wrap: true,
},
},
FactStruct{
Name: "ERROR",
Value: errMsg,
MSTeams: msTeams{
Width: "Full",
},
},
},
},
}
return notificationCard
}

// Send is implementation of interface AlertNotification's Send()
Expand Down Expand Up @@ -125,12 +192,12 @@ func (card *MsTeam) Send() (err error) {

defer resp.Body.Close()

respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if string(respBody) != "1" {
return errors.New("cannot push to MSTeams")
if resp.StatusCode != http.StatusAccepted {
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1024*1024))
if err != nil {
return err
}
return fmt.Errorf("unexpected response from webhook: %s", string(respBody))
}
return
}

0 comments on commit ae31bd9

Please sign in to comment.