Skip to content

Commit

Permalink
Use Lark message cards for notification
Browse files Browse the repository at this point in the history
Signed-off-by: Somtochi Onyekwere <[email protected]>
  • Loading branch information
somtochiama committed Aug 25, 2021
1 parent 4893803 commit 7036311
Showing 1 changed file with 77 additions and 9 deletions.
86 changes: 77 additions & 9 deletions internal/notifier/lark.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
package notifier

import "github.com/fluxcd/pkg/runtime/events"
import (
"fmt"
"strings"

"github.com/fluxcd/pkg/runtime/events"
)

type Lark struct {
URL string
}

type LarkPayload struct {
MsgType string `json:"msg_type"`
Content LarkContent `json:"content"`
MsgType string `json:"msg_type"`
Card LarkCard `json:"card"`
}

type LarkCard struct {
Config LarkConfig `json:"config"`

Header LarkHeader `json:"header"`

Elements []LarkElement `json:"elements"`
}

type LarkConfig struct {
WideScreenMode bool `json:"wide_screen_mode"`
}

type LarkHeader struct {
Title LarkTitle `json:"title"`
Template string `json:"template"`
}

type LarkTitle struct {
Tag string `json:"tag"`
Content string `json:"content"`
}

type LarkElement struct {
Tag string `json:"tag"`
Text LarkText `json:"text"`
}

type LarkContent struct {
Text string `json:"text"`
type LarkText struct {
Tag string `json:"tag"`
Content string `json:"content"`
}

func NewLark(address string) *Lark {
Expand All @@ -27,12 +60,47 @@ func (l *Lark) Post(event events.Event) error {
return nil
}

payload := LarkPayload{
MsgType: "text",
Content: LarkContent{
Text: event.Message,
emoji := "💫"
color := "turquoise"
if event.Severity == events.EventSeverityError {
emoji = "🚨"
color = "red"
}

message := fmt.Sprintf("**%s**\n\n", event.Message)
for k, v := range event.Metadata {
message = message + fmt.Sprintf("%s: %s\n", k, v)
}

element := LarkElement{
Tag: "div",
Text: LarkText{
Tag: "lark_md",
Content: message,
},
}

card := LarkCard{
Config: LarkConfig{
WideScreenMode: true,
},
Header: LarkHeader{
Title: LarkTitle{
Tag: "plain_text",
Content: fmt.Sprintf("%s %s/%s.%s", emoji, strings.ToLower(event.InvolvedObject.Kind),
event.InvolvedObject.Name, event.InvolvedObject.Namespace),
},
Template: color,
},
Elements: []LarkElement{
element,
},
}

payload := LarkPayload{
MsgType: "interactive",
Card: card,
}

return postMessage(l.URL, "", nil, payload)
}

0 comments on commit 7036311

Please sign in to comment.