feat(dingtalk): enable secure multimodal image QA
This commit is contained in:
@@ -2,6 +2,7 @@ package dingtalk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"
|
||||
@@ -71,7 +72,11 @@ func (c *LongConnClient) onChatBotMessage(ctx context.Context, data *chatbot.Bot
|
||||
userID = data.SenderId
|
||||
}
|
||||
|
||||
content := strings.TrimSpace(data.Text.Content)
|
||||
messageType, content, fileKey := parseDingTalkStreamPayload(data.Msgtype, data.Content, data.Text.Content)
|
||||
fileName := ""
|
||||
if messageType == im.MessageTypeImage {
|
||||
fileName = data.MsgId + ".png"
|
||||
}
|
||||
|
||||
incoming := &im.IncomingMessage{
|
||||
Platform: im.PlatformDingtalk,
|
||||
@@ -80,8 +85,10 @@ func (c *LongConnClient) onChatBotMessage(ctx context.Context, data *chatbot.Bot
|
||||
ChatID: chatID,
|
||||
ChatType: chatType,
|
||||
MessageID: data.MsgId,
|
||||
MessageType: im.MessageTypeText,
|
||||
MessageType: messageType,
|
||||
Content: content,
|
||||
FileKey: fileKey,
|
||||
FileName: fileName,
|
||||
Extra: map[string]string{
|
||||
"session_webhook": data.SessionWebhook,
|
||||
},
|
||||
@@ -93,3 +100,31 @@ func (c *LongConnClient) onChatBotMessage(ctx context.Context, data *chatbot.Bot
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func parseDingTalkStreamPayload(msgType string, rawContent interface{}, text string) (im.MessageType, string, string) {
|
||||
if !strings.EqualFold(strings.TrimSpace(msgType), "picture") {
|
||||
return im.MessageTypeText, strings.TrimSpace(text), ""
|
||||
}
|
||||
var payload []byte
|
||||
switch value := rawContent.(type) {
|
||||
case string:
|
||||
payload = []byte(value)
|
||||
case []byte:
|
||||
payload = value
|
||||
case json.RawMessage:
|
||||
payload = value
|
||||
default:
|
||||
var err error
|
||||
payload, err = json.Marshal(rawContent)
|
||||
if err != nil {
|
||||
return im.MessageTypeImage, "", ""
|
||||
}
|
||||
}
|
||||
var picture struct {
|
||||
DownloadCode string `json:"downloadCode"`
|
||||
}
|
||||
if json.Unmarshal(payload, &picture) != nil {
|
||||
return im.MessageTypeImage, "", ""
|
||||
}
|
||||
return im.MessageTypeImage, "", strings.TrimSpace(picture.DownloadCode)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user