Merge pull request #1138 from ccfos/mm_notification

mm notification support at someone
This commit is contained in:
kongfei605 2022-08-31 23:05:28 +08:00 committed by GitHub
commit fc4c4b96bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package sender
import ( import (
"net/url" "net/url"
"strings"
"time" "time"
"github.com/didi/nightingale/v5/src/pkg/poster" "github.com/didi/nightingale/v5/src/pkg/poster"
@ -19,6 +20,14 @@ type mm struct {
Text string `json:"text"` Text string `json:"text"`
} }
func MapStrToStr(arr []string, fn func(s string) string) []string {
var newArray = []string{}
for _, it := range arr {
newArray = append(newArray, fn(it))
}
return newArray
}
func SendMM(message MatterMostMessage) { func SendMM(message MatterMostMessage) {
for i := 0; i < len(message.Tokens); i++ { for i := 0; i < len(message.Tokens); i++ {
@ -33,6 +42,13 @@ func SendMM(message MatterMostMessage) {
} }
channels := v["channel"] // do not get channels := v["channel"] // do not get
txt := ""
atuser := v["atuser"]
if len(atuser) != 0 {
txt = strings.Join(MapStrToStr(atuser, func(u string) string {
return "@" + u
}), ",") + "\n"
}
username := v.Get("username") username := v.Get("username")
if err != nil { if err != nil {
logger.Errorf("mm_sender: failed to parse error=%v", err) logger.Errorf("mm_sender: failed to parse error=%v", err)
@ -43,7 +59,7 @@ func SendMM(message MatterMostMessage) {
body := mm{ body := mm{
Channel: channel, Channel: channel,
Username: username, Username: username,
Text: message.Text, Text: txt + message.Text,
} }
res, code, err := poster.PostJSON(ur, time.Second*5, body, 3) res, code, err := poster.PostJSON(ur, time.Second*5, body, 3)