From dac4d09173b89b14f747eeb0fa576e4053a4d65f Mon Sep 17 00:00:00 2001 From: "Xu, Jiannan | Jeff | CNTD" Date: Fri, 28 Oct 2022 18:08:30 +0800 Subject: [PATCH] support for ms teams as well --- README.md | 30 ++++++++++++++++++++++++++++-- alert.go | 9 ++++++--- alert_test.go | 7 +++++-- email.go | 8 ++++---- ms_teams.go | 26 +++++++++++++++++++++----- 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 517bb82..7dca8e1 100644 --- a/README.md +++ b/README.md @@ -59,15 +59,41 @@ This library supports sending alert as email and as message card to Ms Teams' ch ## Usage +### Without customized fields (will load subject or body of mail and teams from configuration) ```golang //import import n "github.com/rakutentech/go-alertnotification" //Create New Alert - alert := n.NewAlert(err, ignoringErr); + alert := n.NewAlert(err, ignoringErr) //Send notification - alert.Notify(); + alert.Notify() + + // To remove all current throttling + alert.RemoveCurrentThrotting() + +``` + +### With customized fields +```golang + //import + import n "github.com/rakutentech/go-alertnotification" + + //Create expandos, can keep the field value as configured by removing that field from expandos + expandos := &n.Expandos{ + EmailBody: "This is the customized email body", + EmailSubject: "This is the customized email subject", + MsTeamsCardSubject: "This is the customized MS Teams card summary", + MsTeamsAlertCardSubject: "This is the customized MS Teams card title", + MsTeamsError: "This is the customized MS Teams card error message", + } + + //Create New Alert + alert := n.NewAlertWithExpandos(err, ignoringErr, expandos) + + //Send notification + alert.Notify() // To remove all current throttling alert.RemoveCurrentThrotting() diff --git a/alert.go b/alert.go index ce762f3..6bfd93c 100644 --- a/alert.go +++ b/alert.go @@ -34,8 +34,11 @@ func NewAlertWithExpandos(err error, doNotAlertErrors []error, expandos *Expando // Expandos struct for body and subject type Expandos struct { - Body string - Subject string + EmailBody string + EmailSubject string + MsTeamsAlertCardSubject string + MsTeamsCardSubject string + MsTeamsError string } // AlertNotification is interface that all send notification function satify including send email @@ -73,7 +76,7 @@ func (a *Alert) dispatch() (err error) { if shouldMsTeams() { fmt.Println("SendTeams") - m := NewMsTeam(a.Error) + m := NewMsTeam(a.Error, a.Expandos) err := m.Send() if err != nil { return err diff --git a/alert_test.go b/alert_test.go index 6567f1f..ea39e76 100644 --- a/alert_test.go +++ b/alert_test.go @@ -70,8 +70,11 @@ func Test_shouldMail(t *testing.T) { func TestAlert_Notify(t *testing.T) { expandos := &Expandos{ - Body: "This is mail body", - Subject: "This is mail subject", + EmailBody: "TEST: This is mail body", + EmailSubject: "TEST: This is mail subject", + MsTeamsAlertCardSubject: "TEST: This is MS Teams card title", + MsTeamsCardSubject: "TEST: This is MS Teams card summary", + MsTeamsError: "TEST: This is MS Teams card error message", } type fields struct { Error error diff --git a/email.go b/email.go index a8db762..003bf37 100644 --- a/email.go +++ b/email.go @@ -59,11 +59,11 @@ func (ec *EmailConfig) Send() error { // update body and subject dynamically if ec.Expandos != nil { - if ec.Expandos.Body != "" { - messageDetail = ec.Expandos.Body + if ec.Expandos.EmailBody != "" { + messageDetail = ec.Expandos.EmailBody } - if ec.Expandos.Subject != "" { - subject = ec.Expandos.Subject + if ec.Expandos.EmailSubject != "" { + subject = ec.Expandos.EmailSubject } } diff --git a/ms_teams.go b/ms_teams.go index 55eec2a..4eefb61 100644 --- a/ms_teams.go +++ b/ms_teams.go @@ -37,16 +37,32 @@ type FactStruct struct { } // NewMsTeam is used to create MsTeam -func NewMsTeam(err error) 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) + // apply expandos on card + if expandos != nil { + if expandos.MsTeamsAlertCardSubject != "" { + title = expandos.MsTeamsAlertCardSubject + } + if expandos.MsTeamsCardSubject != "" { + summary = expandos.MsTeamsCardSubject + } + if expandos.MsTeamsError != "" { + errMsg = expandos.MsTeamsError + } + } + notificationCard := MsTeam{ Type: "MessageCard", Context: "http://schema.org/extensions", - Summary: os.Getenv("MS_TEAMS_CARD_SUBJECT"), + Summary: summary, ThemeColor: os.Getenv("ALERT_THEME_COLOR"), - Title: os.Getenv("ALERT_CARD_SUBJECT"), + Title: title, Sections: []SectionStruct{ SectionStruct{ - ActivityTitle: os.Getenv("MS_TEAMS_CARD_SUBJECT"), + ActivityTitle: summary, ActivitySubtitle: fmt.Sprintf("error has occured on %v", os.Getenv("APP_NAME")), ActivityImage: "", Facts: []FactStruct{ @@ -56,7 +72,7 @@ func NewMsTeam(err error) MsTeam { }, FactStruct{ Name: "ERROR", - Value: fmt.Sprintf("%+v", err), + Value: errMsg, }, }, },