Skip to content

Commit

Permalink
Merge pull request #13 from Stan1987/feature/add_expandos_for_alert
Browse files Browse the repository at this point in the history
support for ms teams as well
  • Loading branch information
kevincobain2000 authored Oct 31, 2022
2 parents abba029 + dac4d09 commit 29206a7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
26 changes: 21 additions & 5 deletions ms_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -56,7 +72,7 @@ func NewMsTeam(err error) MsTeam {
},
FactStruct{
Name: "ERROR",
Value: fmt.Sprintf("%+v", err),
Value: errMsg,
},
},
},
Expand Down

0 comments on commit 29206a7

Please sign in to comment.