feat: 完成一般消息通知能力的开发

This commit is contained in:
2026-08-12 11:42:08 +08:00
commit 778c46a691
17 changed files with 2470 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
package notice
import (
"encoding/json"
"fmt"
"html"
"strings"
)
type renderedRequest struct {
URL string `json:"url" dc:"最终请求地址"`
Body []byte `json:"body" dc:"平台请求体"`
}
type renderer interface {
Capabilities() Capabilities
Render(config Config, message Message) (renderedRequest, error)
ValidateResponse(response networkResponse) error
}
func marshalRequest(url string, body any) (renderedRequest, error) {
data, err := json.Marshal(body)
if err != nil {
return renderedRequest{}, fmt.Errorf("notice: marshal webhook request: %w", err)
}
return renderedRequest{URL: url, Body: data}, nil
}
func validateHTTP(channel Channel, response networkResponse) error {
if response.StatusCode < 200 || response.StatusCode >= 300 {
return &HTTPError{Channel: channel, StatusCode: response.StatusCode, Body: string(response.Body)}
}
return nil
}
func cardMarkdown(card *CardContent) string {
parts := make([]string, 0, 1+len(card.Fields))
if strings.TrimSpace(card.Markdown) != "" {
parts = append(parts, card.Markdown)
}
for _, field := range card.Fields {
parts = append(parts, fmt.Sprintf("**%s** %s", field.Name, field.Value))
}
return strings.Join(parts, "\n\n")
}
func cardMarkdownFallback(card *CardContent) string {
content := cardMarkdown(card)
if strings.TrimSpace(content) == "" {
return card.Title
}
if strings.TrimSpace(card.Title) == "" {
return content
}
return "## " + card.Title + "\n\n" + content
}
func plainTitle(title, fallback string) string {
if strings.TrimSpace(title) == "" {
return fallback
}
return title
}
func escapeText(value string) string {
value = html.EscapeString(value)
replacer := strings.NewReplacer("\\", "\\\\", "*", "\\*", "_", "\\_", "`", "\\`", "[", "\\[", "]", "\\]")
return replacer.Replace(value)
}