fix: 解决钉钉图片问题

This commit is contained in:
2026-06-02 23:55:25 +08:00
parent 0bc3f02670
commit b798a0579d
+14 -6
View File
@@ -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