Skip to content

Commit 95e459c

Browse files
TOT-JINjinzhanjie
andauthored
🚧 Fix Feishu JsonCard component limitation issue (#225)
* 🚧 fix: update init SQL to store logstore as [] instead of for JSON compatibility * 🚧 fix(feishu): sender failed when posting webhook * 🚧 fix(feishu): JsonCard component limitation * 🚧 fix(feishu): restore original JSON card message template and add v2 version template --------- Co-authored-by: jinzhanjie <jinzhanjie@zhidemai.com>
1 parent 0d3cfbd commit 95e459c

3 files changed

Lines changed: 80 additions & 18 deletions

File tree

‎deploy/helmchart/files/sql/notice_template_examples.sql‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,47 @@ green
478478
}
479479
},
480480
"tag": ""
481+
}','FeiShu'),
482+
('nt-cqh4455d6gvj80netqr0','飞书卡片通知模版v2','发送飞书的高级消息卡片模版,兼容飞书卡片JSON 1.0 / 2.0','',1,'{
483+
"schema": "2.0",
484+
"config": {
485+
"width_mode": "fill",
486+
"enable_forward": true
487+
},
488+
"header": {
489+
"template": "red",
490+
"title": {
491+
"tag": "plain_text",
492+
"content": "【报警中】- WatchAlert 业务系统 🔥"
493+
}
494+
},
495+
"body": {
496+
"elements": [
497+
{ "tag": "markdown", "content": "**🤖 报警类型:** ${rule_name}\n**🫧 报警指纹:** ${fingerprint}\n**📌 报警等级:** ${severity}\n**🖥 报警主机:** ${labels.instance}\n**🕘 开始时间:** ${first_trigger_time_format}\n**👤 值班人员:** ${duty_user}\n**📝 报警事件:** ${annotations}\n[查看事件](http:\/\/localhost:3000\/faultCenter\/detail\/${faultCenterId}?tab=1&query=${rule_name})" },
498+
{ "tag": "hr"},
499+
{ "tag": "markdown", "content": "🧑‍💻 WatchAlert - 运维团队"}
500+
]
501+
}
502+
}','{
503+
"schema": "2.0",
504+
"config": {
505+
"width_mode": "fill",
506+
"enable_forward": true
507+
},
508+
"header": {
509+
"template": "green",
510+
"title": {
511+
"tag": "plain_text",
512+
"content": "【已恢复】- WatchAlert 业务系统 ✨"
513+
}
514+
},
515+
"body": {
516+
"elements": [
517+
{ "tag": "markdown", "content": "**🤖 报警类型:** ${rule_name}\n**🫧 报警指纹:** ${fingerprint}\n**📌 报警等级:** ${severity}\n**🖥 报警主机:** ${labels.instance}\n**🕘 开始时间:** ${first_trigger_time_format}\n**🕘 恢复时间:** ${recover_time_format}\n**👤 值班人员:** ${duty_user}\n**📝 报警事件:** ${annotations}\n[查看事件](http:\/\/localhost:3000\/faultCenter\/detail\/${faultCenterId}?tab=1&query=${rule_name})" },
518+
{ "tag": "hr"},
519+
{ "tag": "markdown", "content": "🧑‍💻 WatchAlert - 运维团队"}
520+
]
521+
}
481522
}','FeiShu'),
482523
('nt-cqh4599d6gvj80netql0','邮件通知模版','发送邮件的普通消息模版','{{ define "Event" -}}
483524
{{- if not .IsRecovered -}}

‎internal/models/template_feishu.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package models
22

3+
// JsonCards 飞书卡片,兼容 1.0 / 2.0 的field
4+
type JsonCards struct {
5+
Schema string `json:"schema,omitempty"`
6+
Config map[string]interface{} `json:"config,omitempty"`
7+
CardLink map[string]interface{} `json:"card_link,omitempty"`
8+
Header map[string]interface{} `json:"header,omitempty"`
9+
Body map[string]interface{} `json:"body,omitempty"`
10+
Elements []map[string]interface{} `json:"elements,omitempty"`
11+
I18nElements map[string]interface{} `json:"i18n_elements,omitempty"`
12+
Fallback map[string]interface{} `json:"fallback,omitempty"`
13+
}
14+
15+
// FeiShuJsonCardMsg 飞书Json卡片消息结构体
16+
type FeiShuJsonCardMsg struct {
17+
MsgType string `json:"msg_type"`
18+
Card JsonCards `json:"card"`
19+
}
20+
321
// FeiShuMsg 飞书
422
type FeiShuMsg struct {
523
MsgType string `json:"msg_type"`

‎pkg/templates/feishuCard.go‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
package templates
22

33
import (
4-
"github.com/bytedance/sonic"
54
"strings"
6-
models "watchAlert/internal/models"
5+
"watchAlert/internal/models"
76
"watchAlert/pkg/tools"
7+
8+
"github.com/bytedance/sonic"
89
)
910

1011
// Template 飞书消息卡片模版
1112
func feishuTemplate(alert models.AlertCurEvent, noticeTmpl models.NoticeTemplateExample) string {
1213

13-
defaultTemplate := models.FeiShuMsg{
14-
MsgType: "interactive",
15-
Card: models.Cards{
16-
Config: models.Configs{
17-
WideScreenMode: true,
18-
EnableForward: true,
19-
},
20-
},
21-
}
22-
2314
var cardContentString string
2415
if *noticeTmpl.EnableFeiShuJsonCard {
25-
var tmplC models.Cards
16+
defaultTemplate := models.FeiShuJsonCardMsg{
17+
MsgType: "interactive",
18+
}
19+
var tmplC models.JsonCards
2620
switch alert.IsRecovered {
2721
case false:
28-
_ = sonic.Unmarshal([]byte(noticeTmpl.TemplateFiring), &tmplC)
22+
cardContentString = noticeTmpl.TemplateFiring
2923
case true:
30-
_ = sonic.Unmarshal([]byte(noticeTmpl.TemplateRecover), &tmplC)
24+
cardContentString = noticeTmpl.TemplateRecover
3125
}
32-
defaultTemplate.Card.Elements = tmplC.Elements
33-
defaultTemplate.Card.Header = tmplC.Header
34-
cardContentString = tools.JsonMarshalToString(defaultTemplate)
3526
cardContentString = ParserTemplate("Card", alert, cardContentString)
27+
_ = sonic.Unmarshal([]byte(cardContentString), &tmplC)
28+
defaultTemplate.Card = tmplC
29+
cardContentString = tools.JsonMarshalToString(defaultTemplate)
3630

3731
} else {
32+
defaultTemplate := models.FeiShuMsg{
33+
MsgType: "interactive",
34+
Card: models.Cards{
35+
Config: models.Configs{
36+
WideScreenMode: true,
37+
EnableForward: true,
38+
},
39+
},
40+
}
3841
cardHeader := models.Headers{
3942
Template: ParserTemplate("TitleColor", alert, noticeTmpl.Template),
4043
Title: models.Titles{

0 commit comments

Comments
 (0)