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
+4 -1
View File
@@ -151,7 +151,10 @@ func (h *Handler) Handle(ctx context.Context, cb BotCallback) error {
log.Printf("DingTalk image preparation failed: stage=%s", imageFailureStage(err))
return h.sendReply(ctx, webhookURL, imageErrorReply)
}
req.Query = imageQuery
req.Query = strings.TrimSpace(cb.Text)
if req.Query == "" {
req.Query = imageQuery
}
req.Images = []core.ImageInput{image}
default:
return h.sendReply(ctx, webhookURL, unsupportedMessageReply)
+116
View File
@@ -477,6 +477,122 @@ func TestHandleNonTextMissingSessionWebhookReturnsError(t *testing.T) {
// --- reply send failure ---
// --- F: richText mixed (text + picture) ---
func TestHandleRichTextMixedDownloadsAndCallsQAWithRealQuery(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "图文回答"}}
reply := &fakeReply{}
image := core.ImageInput{DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 4, Width: 1, Height: 1}
downloader := &fakeImageDownloader{image: image}
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
// Simulate richText mixed: text + picture
data := directTextCallback()
data.Msgtype = "picture"
data.DownloadCode = "temporary-download-code"
data.Text = " 请分析这张截图中的错误 "
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle richText mixed: %v", err)
}
if !downloader.called || downloader.code != "temporary-download-code" {
t.Fatalf("download called=%v code=%q", downloader.called, downloader.code)
}
if !qa.called || len(qa.lastReq.Images) != 1 || qa.lastReq.Images[0] != image {
t.Fatalf("QA request did not receive image: %+v", qa.lastReq)
}
// Key assert: mixed uses real query, not default imageQuery
if qa.lastReq.Query != "请分析这张截图中的错误" {
t.Fatalf("richText mixed query=%q, want real query", qa.lastReq.Query)
}
if !reply.called || reply.lastText != "图文回答" {
t.Fatalf("reply=%+v", reply)
}
}
func TestHandleRichTextMixedEmptyQueryFallsBackToDefault(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "默认回答"}}
reply := &fakeReply{}
image := core.ImageInput{DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 4, Width: 1, Height: 1}
downloader := &fakeImageDownloader{image: image}
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
data := directTextCallback()
data.Msgtype = "picture"
data.DownloadCode = "code"
data.Text = " "
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle: %v", err)
}
// When Text is empty after trim, falls back to default imageQuery
if qa.lastReq.Query != imageQuery {
t.Fatalf("query=%q, want default imageQuery=%q", qa.lastReq.Query, imageQuery)
}
}
func TestHandleIndependentPictureStillUsesDefaultQuery(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "图片回答"}}
reply := &fakeReply{}
image := core.ImageInput{DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 4, Width: 1, Height: 1}
downloader := &fakeImageDownloader{image: image}
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
// Independent picture: Text is empty (as A1 produces)
data := directPictureCallback()
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle picture: %v", err)
}
// Independent picture should still use default query
if qa.lastReq.Query != imageQuery {
t.Fatalf("picture query=%q, want default imageQuery=%q", qa.lastReq.Query, imageQuery)
}
}
func TestHandleGroupRichTextMixedWithoutMentionDoesNothing(t *testing.T) {
qa := &fakeQA{}
reply := &fakeReply{}
downloader := &fakeImageDownloader{}
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
data := directTextCallback()
data.Msgtype = "picture"
data.DownloadCode = "code"
data.Text = "忽略这条"
data.ConversationType = conversationTypeGroup
data.IsInAtList = false
data.SessionWebhook = "not even validated"
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle: %v", err)
}
if downloader.called || qa.called || reply.called {
t.Fatalf("unexpected side effects: downloader=%v qa=%v reply=%v", downloader.called, qa.called, reply.called)
}
}
func TestHandleRichTextMixedDownloadFailureFriendlyReplyNoQA(t *testing.T) {
qa := &fakeQA{}
reply := &fakeReply{}
downloader := &fakeImageDownloader{err: errors.New("download failed")}
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
data := directTextCallback()
data.Msgtype = "picture"
data.DownloadCode = "broken-code"
data.Text = "帮我看看这张图"
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle: %v", err)
}
if qa.called {
t.Fatal("QA must not be called on download failure")
}
if !reply.called || reply.lastText != imageErrorReply {
t.Fatalf("friendly reply=%q, want %q", reply.lastText, imageErrorReply)
}
}
func TestHandleReplySendFailureReturnsError(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
reply := &fakeReply{err: errors.New("webhook timeout")}
+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
+284
View File
@@ -258,6 +258,290 @@ func mustTestHandler(t *testing.T) *Handler {
return newTestHandler(t, &fakeQA{}, &fakeReply{})
}
func TestBotCallbackFromSDKRichText(t *testing.T) {
t.Parallel()
t.Run("direct Content richText text+picture", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "richText",
MsgId: "msg-rt-1",
Content: map[string]interface{}{
"richText": []interface{}{
map[string]interface{}{"text": " 问题描述 "},
map[string]interface{}{"type": "picture", "downloadCode": " code-abc "},
},
},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "picture" {
t.Fatalf("Msgtype = %q, want picture", cb.Msgtype)
}
if cb.DownloadCode != "code-abc" {
t.Fatalf("DownloadCode = %q, want code-abc", cb.DownloadCode)
}
if cb.Text != "问题描述" {
t.Fatalf("Text = %q, want 问题描述 (trimmed text segments joined)", cb.Text)
}
})
t.Run("nested content envelope richText", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "richText",
MsgId: "msg-rt-2",
Content: map[string]interface{}{
"content": map[string]interface{}{
"richText": []interface{}{
map[string]interface{}{"text": "描述"},
map[string]interface{}{"type": "picture", "downloadCode": "code-xyz"},
},
},
},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "picture" {
t.Fatalf("Msgtype = %q, want picture", cb.Msgtype)
}
if cb.DownloadCode != "code-xyz" {
t.Fatalf("DownloadCode = %q, want code-xyz", cb.DownloadCode)
}
if cb.Text != "描述" {
t.Fatalf("Text = %q, want 描述", cb.Text)
}
})
t.Run("multi text segments preserve order", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "richText",
MsgId: "msg-rt-3",
Content: []interface{}{
map[string]interface{}{"text": "第一段"},
map[string]interface{}{"text": " 第二段 "},
map[string]interface{}{"type": "picture", "downloadCode": "code-multi"},
},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "picture" {
t.Fatalf("Msgtype = %q, want picture", cb.Msgtype)
}
if cb.Text != "第一段\n第二段" {
t.Fatalf("Text = %q, want joined with newline", cb.Text)
}
})
t.Run("first non-empty downloadCode used, subsequent ignored", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "richText",
MsgId: "msg-rt-4",
Content: []interface{}{
map[string]interface{}{"type": "picture", "downloadCode": " code-first "},
map[string]interface{}{"type": "picture", "downloadCode": "code-second"},
},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.DownloadCode != "code-first" {
t.Fatalf("DownloadCode = %q, want first code-first", cb.DownloadCode)
}
})
t.Run("picture item missing downloadCode still picture", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "richText",
MsgId: "msg-rt-5",
Content: []interface{}{
map[string]interface{}{"text": "看看这张图"},
map[string]interface{}{"type": "picture"},
},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "picture" {
t.Fatalf("Msgtype = %q, want picture (never downgrade)", cb.Msgtype)
}
if cb.DownloadCode != "" {
t.Fatalf("DownloadCode = %q, want empty", cb.DownloadCode)
}
if cb.Text != "看看这张图" {
t.Fatalf("Text = %q, want 看看这张图", cb.Text)
}
})
t.Run("pure text richText becomes text", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "richText",
MsgId: "msg-rt-6",
Content: []interface{}{
map[string]interface{}{"text": "你好"},
map[string]interface{}{"text": " 世界 "},
},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "text" {
t.Fatalf("Msgtype = %q, want text", cb.Msgtype)
}
if cb.Text != "你好\n世界" {
t.Fatalf("Text = %q, want joined text", cb.Text)
}
if cb.DownloadCode != "" {
t.Fatalf("DownloadCode = %q, want empty", cb.DownloadCode)
}
})
t.Run("malformed richText keeps richtext", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "richText",
MsgId: "msg-rt-7",
Content: "not-a-list",
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "richtext" {
t.Fatalf("Msgtype = %q, want richtext (malformed, keep original)", cb.Msgtype)
}
})
t.Run("empty richText keeps richtext", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "richText",
MsgId: "msg-rt-8",
Content: []interface{}{},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "richtext" {
t.Fatalf("Msgtype = %q, want richtext (empty, keep original)", cb.Msgtype)
}
})
t.Run("richText with type case normalization", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: " RICHTEXT ",
MsgId: "msg-rt-9",
Content: []interface{}{
map[string]interface{}{"text": "hello"},
map[string]interface{}{"TYPE": " PICTURE ", "DOWNLOADCODE": "code-case"},
},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "picture" {
t.Fatalf("Msgtype = %q, want picture", cb.Msgtype)
}
if cb.DownloadCode != "code-case" {
t.Fatalf("DownloadCode = %q, want code-case", cb.DownloadCode)
}
if cb.Text != "hello" {
t.Fatalf("Text = %q, want hello", cb.Text)
}
})
t.Run("richText skips empty text segments", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "richText",
MsgId: "msg-rt-10",
Content: []interface{}{
map[string]interface{}{"text": " "},
map[string]interface{}{"text": "有效内容"},
map[string]interface{}{"text": ""},
},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "text" {
t.Fatalf("Msgtype = %q, want text", cb.Msgtype)
}
if cb.Text != "有效内容" {
t.Fatalf("Text = %q, want 有效内容 (empty segments skipped)", cb.Text)
}
})
t.Run("text regression still works", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "text",
MsgId: "msg-t-2",
Text: chatbot.BotCallbackDataTextModel{Content: "hello"},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "text" {
t.Fatalf("Msgtype = %q, want text", cb.Msgtype)
}
if cb.Text != "hello" {
t.Fatalf("Text = %q, want hello", cb.Text)
}
})
t.Run("picture regression still works", func(t *testing.T) {
t.Parallel()
data := &chatbot.BotCallbackDataModel{
ConversationType: "1",
Msgtype: "picture",
MsgId: "msg-p-2",
Content: map[string]interface{}{
"downloadCode": "regression-code",
},
}
cb, err := BotCallbackFromSDK(data)
if err != nil {
t.Fatalf("BotCallbackFromSDK() error = %v", err)
}
if cb.Msgtype != "picture" {
t.Fatalf("Msgtype = %q, want picture", cb.Msgtype)
}
if cb.DownloadCode != "regression-code" {
t.Fatalf("DownloadCode = %q, want regression-code", cb.DownloadCode)
}
})
}
func TestNewStreamClientValidation(t *testing.T) {
t.Parallel()
h := mustTestHandler(t)