From b798a0579dad20b753962d01b04569826eef932d Mon Sep 17 00:00:00 2001 From: Kaxi <1042864399@qq.com> Date: Tue, 2 Jun 2026 23:55:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E9=92=89=E9=92=89?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform/dingtalk/dingtalk.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/platform/dingtalk/dingtalk.go b/platform/dingtalk/dingtalk.go index adce3f9..f4ddb8b 100644 --- a/platform/dingtalk/dingtalk.go +++ b/platform/dingtalk/dingtalk.go @@ -261,12 +261,12 @@ func (p *Platform) onMessage(data *chatbot.BotCallbackDataModel, richText *richT return } - // Download images from pictureDownloadCode + // Download images from downloadCode var images []core.ImageAttachment for _, code := range imageCodes { imgData, mimeType, err := p.downloadImageByCode(code) if err != nil { - slog.Error("dingtalk: failed to download richText image", "error", err, "code", code) + slog.Error("dingtalk: failed to download richText image", "error", err) continue } images = append(images, core.ImageAttachment{ @@ -331,8 +331,9 @@ func (p *Platform) onMessage(data *chatbot.BotCallbackDataModel, richText *richT } // extractRichText extracts plain text and image download codes from a DingTalk richText content payload. -// The expected structure is: {"richText": [{"text": "..."}, {"pictureDownloadCode": "..."}, ...]} +// The expected structure is: {"richText": [{"text": "..."}, {"downloadCode": "...", "pictureDownloadCode": "...", "type": "picture"}, ...]} // Returns text and a list of download codes for images found in the rich text. +// Prefers "downloadCode" over "pictureDownloadCode" (the former is used by the messageFiles/download API). func extractRichText(content interface{}) (string, []string) { m, ok := content.(map[string]interface{}) if !ok { @@ -352,7 +353,13 @@ func extractRichText(content interface{}) (string, []string) { if text, ok := item["text"].(string); ok { b.WriteString(text) } - if code, ok := item["pictureDownloadCode"].(string); ok && code != "" { + // Prefer downloadCode (used by handleImageMessage and messageFiles/download API). + // Fall back to pictureDownloadCode if downloadCode is not present. + code, _ := item["downloadCode"].(string) + if code == "" { + code, _ = item["pictureDownloadCode"].(string) + } + if code != "" { imageCodes = append(imageCodes, code) } } @@ -505,7 +512,7 @@ func (p *Platform) handleImageMessage(data *chatbot.BotCallbackDataModel, sessio p.handler(p, msg) } -// downloadImageByCode downloads an image using a pictureDownloadCode from richText messages. +// downloadImageByCode downloads an image using a downloadCode from richText messages. func (p *Platform) downloadImageByCode(downloadCode string) ([]byte, string, error) { downloadURL, err := p.getDownloadURL(downloadCode) if err != nil { @@ -605,7 +612,8 @@ func (p *Platform) getDownloadURL(downloadCode string) (string, error) { defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return "", fmt.Errorf("api returned status %d", resp.StatusCode) + respBody, _ := io.ReadAll(resp.Body) + return "", fmt.Errorf("api returned status %d: %s", resp.StatusCode, string(respBody)) } var result downloadResponse