mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-06 07:36:37 +08:00
autoresearch iter 7: share body field extraction across push channels
email, telegram and lark each re-implemented the title/content/level lookup with only their markup differing, and each carried a dead content := "" initialization that every branch overwrote. Three small helpers in template.go now own that logic.
This commit is contained in:
@@ -37,22 +37,8 @@ func (p *EmailPusher) Send(ctx context.Context, cfg Config, target string, body
|
|||||||
return "", errors.New("email: target email address is required")
|
return "", errors.New("email: target email address is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
title := defaultTitle
|
title := bodyTitle(body)
|
||||||
if t, ok := body["title"].(string); ok && t != "" {
|
content := bodyContent(body, "<p><b>%s</b>: %v</p>", "")
|
||||||
title = t
|
|
||||||
}
|
|
||||||
|
|
||||||
content := ""
|
|
||||||
if c, ok := body["content"].(string); ok && c != "" {
|
|
||||||
content = c
|
|
||||||
} else {
|
|
||||||
// 自动格式化 map
|
|
||||||
var parts []string
|
|
||||||
for k, v := range body {
|
|
||||||
parts = append(parts, fmt.Sprintf("<p><b>%s</b>: %v</p>", k, v))
|
|
||||||
}
|
|
||||||
content = strings.Join(parts, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 邮件头和体
|
// 邮件头和体
|
||||||
from := cfg.Key
|
from := cfg.Key
|
||||||
|
|||||||
@@ -154,27 +154,9 @@ func (p *LarkPusher) Send(ctx context.Context, cfg Config, _ string, body map[st
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 2. 如果无模板,默认生成一个精美的飞书互动卡片
|
// 2. 如果无模板,默认生成一个精美的飞书互动卡片
|
||||||
title := defaultTitle
|
title := bodyTitle(body)
|
||||||
if t, ok := body["title"].(string); ok && t != "" {
|
content := bodyContent(body, "**%s**: %v", "\n")
|
||||||
title = t
|
level := bodyLevel(body)
|
||||||
}
|
|
||||||
|
|
||||||
content := ""
|
|
||||||
if c, ok := body["content"].(string); ok && c != "" {
|
|
||||||
content = c
|
|
||||||
} else {
|
|
||||||
// 兜底:如果连 content 都没有,把 body 里的所有值拼成 markdown
|
|
||||||
var parts []string
|
|
||||||
for k, v := range body {
|
|
||||||
parts = append(parts, fmt.Sprintf("**%s**: %v", k, v))
|
|
||||||
}
|
|
||||||
content = strings.Join(parts, "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
level := levelInfo
|
|
||||||
if l, ok := body["level"].(string); ok && l != "" {
|
|
||||||
level = strings.ToUpper(l)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 根据级别确定飞书卡片头部的背景色模板
|
// 根据级别确定飞书卡片头部的背景色模板
|
||||||
headerColor := "blue"
|
headerColor := "blue"
|
||||||
|
|||||||
@@ -55,24 +55,9 @@ func (p *TelegramPusher) Send(ctx context.Context, cfg Config, target string, bo
|
|||||||
}
|
}
|
||||||
baseURL = strings.TrimSuffix(baseURL, "/")
|
baseURL = strings.TrimSuffix(baseURL, "/")
|
||||||
|
|
||||||
title := defaultTitle
|
title := bodyTitle(body)
|
||||||
if t, ok := body["title"].(string); ok && t != "" {
|
content := bodyContent(body, "<b>%s</b>: %v", "\n")
|
||||||
title = t
|
level := bodyLevel(body)
|
||||||
}
|
|
||||||
content := ""
|
|
||||||
if c, ok := body["content"].(string); ok && c != "" {
|
|
||||||
content = c
|
|
||||||
} else {
|
|
||||||
var parts []string
|
|
||||||
for k, v := range body {
|
|
||||||
parts = append(parts, fmt.Sprintf("<b>%s</b>: %v", k, v))
|
|
||||||
}
|
|
||||||
content = strings.Join(parts, "\n")
|
|
||||||
}
|
|
||||||
level := levelInfo
|
|
||||||
if l, ok := body["level"].(string); ok && l != "" {
|
|
||||||
level = strings.ToUpper(l)
|
|
||||||
}
|
|
||||||
|
|
||||||
var text string
|
var text string
|
||||||
if template != "" {
|
if template != "" {
|
||||||
|
|||||||
@@ -76,3 +76,32 @@ func formatValue(v any) string {
|
|||||||
return fmt.Sprintf("%v", v)
|
return fmt.Sprintf("%v", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bodyTitle returns the notification title, falling back to the default.
|
||||||
|
func bodyTitle(body map[string]any) string {
|
||||||
|
if t, ok := body["title"].(string); ok && t != "" {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
return defaultTitle
|
||||||
|
}
|
||||||
|
|
||||||
|
// bodyContent returns the notification body, rendering every entry with format
|
||||||
|
// (a "%s … %v" pair) and joining them with sep when no content field is given.
|
||||||
|
func bodyContent(body map[string]any, format, sep string) string {
|
||||||
|
if c, ok := body["content"].(string); ok && c != "" {
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
parts := make([]string, 0, len(body))
|
||||||
|
for k, v := range body {
|
||||||
|
parts = append(parts, fmt.Sprintf(format, k, v))
|
||||||
|
}
|
||||||
|
return strings.Join(parts, sep)
|
||||||
|
}
|
||||||
|
|
||||||
|
// bodyLevel returns the upper-cased notification level, falling back to INFO.
|
||||||
|
func bodyLevel(body map[string]any) string {
|
||||||
|
if l, ok := body["level"].(string); ok && l != "" {
|
||||||
|
return strings.ToUpper(l)
|
||||||
|
}
|
||||||
|
return levelInfo
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user