fix: 解决钉钉图片问题
This commit is contained in:
@@ -261,12 +261,12 @@ func (p *Platform) onMessage(data *chatbot.BotCallbackDataModel, richText *richT
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download images from pictureDownloadCode
|
// Download images from downloadCode
|
||||||
var images []core.ImageAttachment
|
var images []core.ImageAttachment
|
||||||
for _, code := range imageCodes {
|
for _, code := range imageCodes {
|
||||||
imgData, mimeType, err := p.downloadImageByCode(code)
|
imgData, mimeType, err := p.downloadImageByCode(code)
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
images = append(images, core.ImageAttachment{
|
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.
|
// 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.
|
// 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) {
|
func extractRichText(content interface{}) (string, []string) {
|
||||||
m, ok := content.(map[string]interface{})
|
m, ok := content.(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -352,7 +353,13 @@ func extractRichText(content interface{}) (string, []string) {
|
|||||||
if text, ok := item["text"].(string); ok {
|
if text, ok := item["text"].(string); ok {
|
||||||
b.WriteString(text)
|
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)
|
imageCodes = append(imageCodes, code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -505,7 +512,7 @@ func (p *Platform) handleImageMessage(data *chatbot.BotCallbackDataModel, sessio
|
|||||||
p.handler(p, msg)
|
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) {
|
func (p *Platform) downloadImageByCode(downloadCode string) ([]byte, string, error) {
|
||||||
downloadURL, err := p.getDownloadURL(downloadCode)
|
downloadURL, err := p.getDownloadURL(downloadCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -605,7 +612,8 @@ func (p *Platform) getDownloadURL(downloadCode string) (string, error) {
|
|||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
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
|
var result downloadResponse
|
||||||
|
|||||||
Reference in New Issue
Block a user