fix: 飞书告警改用交互卡片展示 + 通知渠道与联系人合并为通知管理Tab
- 飞书: sendFeishuCard 改用 msg_type=interactive 卡片格式(header+template+fields+note) - 飞书: splitLines 处理 \n 转义 → 卡片字段逐行渲染 lark_md - 飞书: header.template 按级别着色(red/orange/green/blue) - 飞书: 测试通知改为卡片格式: 测试时间/来源/状态 - UI: 合并通知渠道+报警联系人为一个「通知管理」Tab(4列Tab栏) - UI: 通知管理下分【渠道配置】+【报警联系人】两个区域
This commit is contained in:
1 parent
e1cfbaf45e
commit
e1f75f0bc4
2 files changed
+124
-67
No files matched your search
@@ -10,6 +10,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ezops/internal/models"
|
||||
@@ -236,7 +237,7 @@ func sendFeishuTest(configJSON string) error {
|
||||
}
|
||||
|
||||
title := "✅ AIOps 告警测试通知"
|
||||
content := fmt.Sprintf("通知渠道连通性测试成功\\n测试时间:%s\\n来源:AIOps 智能运维平台", time.Now().Format("2006-01-02 15:04:05"))
|
||||
content := fmt.Sprintf("**测试时间**:%s\\n**来源**:AIOps 智能运维平台\\n**状态**:连通性测试成功 🎉", time.Now().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return sendFeishuCard(cfg.URL, cfg.Secret, title, content, "green")
|
||||
}
|
||||
@@ -283,16 +284,70 @@ func sendFeishuRecovery(configJSON, title, content string) error {
|
||||
return sendFeishuCard(cfg.URL, cfg.Secret, title, content, "green")
|
||||
}
|
||||
|
||||
// sendFeishuCard 发送飞书消息(优先使用卡片格式,出错回退到文本)
|
||||
// sendFeishuCard 发送飞书交互卡片消息
|
||||
func sendFeishuCard(webhookURL, secret, title, content, color string) error {
|
||||
// 先尝试文本格式(最可靠)
|
||||
textMsg := map[string]interface{}{
|
||||
"msg_type": "text",
|
||||
"content": map[string]interface{}{
|
||||
"text": title + "\n\n" + content,
|
||||
// 解析 content 中的字段(格式: 多行 key: value)
|
||||
lines := splitLines(content)
|
||||
var fields []map[string]interface{}
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
fields = append(fields, map[string]interface{}{
|
||||
"is_short": false,
|
||||
"text": map[string]interface{}{
|
||||
"tag": "lark_md",
|
||||
"content": line,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
template := color
|
||||
if template == "red" {
|
||||
template = "red"
|
||||
} else if template == "orange" || template == "warning" {
|
||||
template = "orange"
|
||||
} else if template == "green" {
|
||||
template = "green"
|
||||
} else if template == "blue" || template == "info" {
|
||||
template = "blue"
|
||||
} else {
|
||||
template = "red"
|
||||
}
|
||||
|
||||
card := map[string]interface{}{
|
||||
"msg_type": "interactive",
|
||||
"card": map[string]interface{}{
|
||||
"header": map[string]interface{}{
|
||||
"title": map[string]interface{}{
|
||||
"tag": "plain_text",
|
||||
"content": title,
|
||||
},
|
||||
"template": template,
|
||||
},
|
||||
"elements": []map[string]interface{}{
|
||||
{
|
||||
"tag": "div",
|
||||
"fields": fields,
|
||||
},
|
||||
{
|
||||
"tag": "hr",
|
||||
},
|
||||
{
|
||||
"tag": "note",
|
||||
"elements": []map[string]interface{}{
|
||||
{
|
||||
"tag": "plain_text",
|
||||
"content": fmt.Sprintf("⏰ %s | AIOps 智能运维平台", time.Now().Format("2006-01-02 15:04:05")),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
body, _ := json.Marshal(textMsg)
|
||||
|
||||
body, _ := json.Marshal(card)
|
||||
|
||||
req, err := http.NewRequest("POST", webhookURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
@@ -300,7 +355,7 @@ func sendFeishuCard(webhookURL, secret, title, content, color string) error {
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// 如果有 secret,生成签名
|
||||
// 如果有 secret,追加签名到 URL query
|
||||
if secret != "" {
|
||||
timestamp := time.Now().Unix()
|
||||
sign := genFeishuSign(timestamp, secret)
|
||||
@@ -322,7 +377,6 @@ func sendFeishuCard(webhookURL, secret, title, content, color string) error {
|
||||
return fmt.Errorf("飞书返回错误 (HTTP %d): %s", resp.StatusCode, string(respBody))
|
||||
}
|
||||
|
||||
// 检查飞书业务返回码
|
||||
var result struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
@@ -335,6 +389,13 @@ func sendFeishuCard(webhookURL, secret, title, content, color string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// splitLines splits a string by newlines, handling both actual \n and escaped \\n
|
||||
func splitLines(s string) []string {
|
||||
// First replace literal \n (from Go format strings) with actual newlines
|
||||
s = strings.ReplaceAll(s, "\\n", "\n")
|
||||
return strings.Split(s, "\n")
|
||||
}
|
||||
|
||||
// buildFeishuCard 构建飞书卡片消息体(保留备用,当前使用文本格式)
|
||||
func buildFeishuCard(title, content, color string) models.FeishuCardMessage {
|
||||
return models.FeishuCardMessage{
|
||||
|
||||
Reference in new issue
Block a user