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
+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)