fix(dingtalk): parse rich text image messages

This commit is contained in:
姚章浩
2026-07-16 00:49:26 +08:00
parent c9c4f0aaa5
commit 7ccf3365f0
4 changed files with 119 additions and 14 deletions
+38 -1
View File
@@ -102,9 +102,11 @@ func (c *LongConnClient) onChatBotMessage(ctx context.Context, data *chatbot.Bot
}
func parseDingTalkStreamPayload(msgType string, rawContent interface{}, text string) (im.MessageType, string, string) {
if !strings.EqualFold(strings.TrimSpace(msgType), "picture") {
normalizedType := strings.ToLower(strings.TrimSpace(msgType))
if normalizedType != "picture" && normalizedType != "richtext" {
return im.MessageTypeText, strings.TrimSpace(text), ""
}
var payload []byte
switch value := rawContent.(type) {
case string:
@@ -120,6 +122,41 @@ func parseDingTalkStreamPayload(msgType string, rawContent interface{}, text str
return im.MessageTypeImage, "", ""
}
}
if normalizedType == "richtext" {
var rich struct {
Items []struct {
Text string `json:"text"`
Type string `json:"type"`
DownloadCode string `json:"downloadCode"`
} `json:"richText"`
}
if json.Unmarshal(payload, &rich) != nil {
return im.MessageTypeText, strings.TrimSpace(text), ""
}
var textParts []string
fileKey := ""
hasPicture := false
for _, item := range rich.Items {
if itemText := strings.TrimSpace(item.Text); itemText != "" {
textParts = append(textParts, itemText)
}
if strings.EqualFold(strings.TrimSpace(item.Type), "picture") {
hasPicture = true
if fileKey == "" {
fileKey = strings.TrimSpace(item.DownloadCode)
}
}
}
content := strings.Join(textParts, "\n")
if hasPicture {
return im.MessageTypeImage, content, fileKey
}
return im.MessageTypeText, content, ""
}
var picture struct {
DownloadCode string `json:"downloadCode"`
}