fix(dingtalk): parse rich text image messages
This commit is contained in:
@@ -102,9 +102,11 @@ func (c *LongConnClient) onChatBotMessage(ctx context.Context, data *chatbot.Bot
|
||||
}
|
||||
|
||||
func parseDingTalkStreamPayload(msgType string, rawContent interface{}, text string) (im.MessageType, string, string) {
|
||||
if !strings.EqualFold(strings.TrimSpace(msgType), "picture") {
|
||||
normalizedType := strings.ToLower(strings.TrimSpace(msgType))
|
||||
if normalizedType != "picture" && normalizedType != "richtext" {
|
||||
return im.MessageTypeText, strings.TrimSpace(text), ""
|
||||
}
|
||||
|
||||
var payload []byte
|
||||
switch value := rawContent.(type) {
|
||||
case string:
|
||||
@@ -120,6 +122,41 @@ func parseDingTalkStreamPayload(msgType string, rawContent interface{}, text str
|
||||
return im.MessageTypeImage, "", ""
|
||||
}
|
||||
}
|
||||
|
||||
if normalizedType == "richtext" {
|
||||
var rich struct {
|
||||
Items []struct {
|
||||
Text string `json:"text"`
|
||||
Type string `json:"type"`
|
||||
DownloadCode string `json:"downloadCode"`
|
||||
} `json:"richText"`
|
||||
}
|
||||
if json.Unmarshal(payload, &rich) != nil {
|
||||
return im.MessageTypeText, strings.TrimSpace(text), ""
|
||||
}
|
||||
|
||||
var textParts []string
|
||||
fileKey := ""
|
||||
hasPicture := false
|
||||
for _, item := range rich.Items {
|
||||
if itemText := strings.TrimSpace(item.Text); itemText != "" {
|
||||
textParts = append(textParts, itemText)
|
||||
}
|
||||
if strings.EqualFold(strings.TrimSpace(item.Type), "picture") {
|
||||
hasPicture = true
|
||||
if fileKey == "" {
|
||||
fileKey = strings.TrimSpace(item.DownloadCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
content := strings.Join(textParts, "\n")
|
||||
if hasPicture {
|
||||
return im.MessageTypeImage, content, fileKey
|
||||
}
|
||||
return im.MessageTypeText, content, ""
|
||||
}
|
||||
|
||||
var picture struct {
|
||||
DownloadCode string `json:"downloadCode"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user