feat: support DingTalk rich text images

This commit is contained in:
姚章浩
2026-07-20 18:58:29 +08:00
parent ea54b229ee
commit 6bfa16e5f9
4 changed files with 512 additions and 1 deletions
+108
View File
@@ -59,6 +59,9 @@ func (c StreamConfig) Validate() error {
// For picture messages the downloadCode is extracted from the SDK Content
// field (json marshal/unmarshal round-trip); malformed or missing codes
// produce an empty DownloadCode but never downgrade the Msgtype.
// For richText messages the Content field is parsed for text+picture items;
// if a picture item is present Msgtype becomes "picture", otherwise text-only
// richText is promoted to "text". Malformed/empty richText keeps "richtext".
func BotCallbackFromSDK(data *chatbot.BotCallbackDataModel) (BotCallback, error) {
if data == nil {
return BotCallback{}, errors.New("dingtalk stream: callback data is nil")
@@ -76,12 +79,117 @@ func BotCallbackFromSDK(data *chatbot.BotCallbackDataModel) (BotCallback, error)
SessionWebhook: data.SessionWebhook,
Text: data.Text.Content,
}
if msgtype == "richtext" {
rtMsgtype, rtText, rtCode := parseRichText(data.Content)
cb.Msgtype = rtMsgtype
cb.Text = rtText
cb.DownloadCode = rtCode
return cb, nil
}
if msgtype == "picture" {
cb.DownloadCode = extractDownloadCode(data.Content)
}
return cb, nil
}
// parseRichText parses the SDK Content field for richText messages.
// It handles three content shapes:
// 1. Direct object: {"richText": [...]}
// 2. Bare richText array (compatibility)
// 3. Nested envelope: {"content":{"richText":[...]}}
//
// Picture items are identified by a "type" field (case-insensitive trimmed)
// equal to "picture". The first non-empty downloadCode is used; subsequent
// picture items are ignored. Text items are joined with newlines.
//
// Returns: (msgtype, text, downloadCode). Msgtype is "picture" if any picture
// item exists, "text" if only text items, "richtext" if malformed/empty.
func parseRichText(raw interface{}) (string, string, string) {
items := extractRichTextItems(raw)
if len(items) == 0 {
return "richtext", "", ""
}
var textParts []string
var downloadCode string
hasPicture := false
for _, item := range items {
itemType := strings.ToLower(strings.TrimSpace(item.Type))
if itemType == "picture" {
hasPicture = true
if downloadCode == "" {
code := strings.TrimSpace(item.DownloadCode)
if code != "" {
downloadCode = code
}
}
continue
}
txt := strings.TrimSpace(item.Text)
if txt != "" {
textParts = append(textParts, txt)
}
}
text := strings.Join(textParts, "\n")
if hasPicture {
return "picture", text, downloadCode
}
if text != "" {
return "text", text, ""
}
return "richtext", "", ""
}
// richTextItem is a single item in a richText array.
type richTextItem struct {
Type string `json:"type"`
DownloadCode string `json:"downloadCode"`
Text string `json:"text"`
}
// extractRichTextItems extracts the richText item array from the raw Content
// field. It handles direct object, bare-array, and nested-envelope shapes.
func extractRichTextItems(raw interface{}) []richTextItem {
if raw == nil {
return nil
}
b, err := json.Marshal(raw)
if err != nil {
return nil
}
// Real DingTalk SDK shape: Content is already {"richText": [...]}.
var object struct {
RichText []richTextItem `json:"richText"`
}
if json.Unmarshal(b, &object) == nil && len(object.RichText) > 0 {
return object.RichText
}
// Accept a bare array for transport compatibility.
var direct []richTextItem
if json.Unmarshal(b, &direct) == nil && len(direct) > 0 {
return direct
}
// Accept a preserved callback envelope: {"content":{"richText":[...]}}.
var nested struct {
Content struct {
RichText []richTextItem `json:"richText"`
} `json:"content"`
}
if json.Unmarshal(b, &nested) == nil && len(nested.Content.RichText) > 0 {
return nested.Content.RichText
}
return nil
}
// extractDownloadCode attempts to pull content.downloadCode from the SDK
// Content field (interface{}) via a json marshal/unmarshal round-trip. It
// never panics on malformed input and returns an empty string for any