feat: add DingTalk picture agent QA
This commit is contained in:
@@ -2,6 +2,7 @@ package dingtalk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -54,16 +55,19 @@ func (c StreamConfig) Validate() error {
|
||||
}
|
||||
|
||||
// BotCallbackFromSDK converts the official SDK callback model into the
|
||||
// channel-internal BotCallback. It is a pure one-to-one field mapping; no
|
||||
// trimming, validation or business logic happens here (Handler owns that).
|
||||
// channel-internal BotCallback. Msgtype is normalized (trimmed, lowercased).
|
||||
// For picture messages the downloadCode is extracted from the SDK Content
|
||||
// field (json marshal/unmarshal round-trip); malformed or missing codes
|
||||
// produce an empty DownloadCode but never downgrade the Msgtype.
|
||||
func BotCallbackFromSDK(data *chatbot.BotCallbackDataModel) (BotCallback, error) {
|
||||
if data == nil {
|
||||
return BotCallback{}, errors.New("dingtalk stream: callback data is nil")
|
||||
}
|
||||
return BotCallback{
|
||||
msgtype := strings.ToLower(strings.TrimSpace(data.Msgtype))
|
||||
cb := BotCallback{
|
||||
ConversationType: data.ConversationType,
|
||||
ConversationID: data.ConversationId,
|
||||
Msgtype: data.Msgtype,
|
||||
Msgtype: msgtype,
|
||||
MsgID: data.MsgId,
|
||||
SenderNick: data.SenderNick,
|
||||
SenderStaffID: data.SenderStaffId,
|
||||
@@ -71,7 +75,41 @@ func BotCallbackFromSDK(data *chatbot.BotCallbackDataModel) (BotCallback, error)
|
||||
IsInAtList: data.IsInAtList,
|
||||
SessionWebhook: data.SessionWebhook,
|
||||
Text: data.Text.Content,
|
||||
}, nil
|
||||
}
|
||||
if msgtype == "picture" {
|
||||
cb.DownloadCode = extractDownloadCode(data.Content)
|
||||
}
|
||||
return cb, nil
|
||||
}
|
||||
|
||||
// extractDownloadCode attempts to pull content.downloadCode from the SDK
|
||||
// Content field (interface{}) via a json marshal/unmarshal round-trip. It
|
||||
// never panics on malformed input and returns an empty string for any
|
||||
// extraction failure.
|
||||
func extractDownloadCode(raw interface{}) string {
|
||||
if raw == nil {
|
||||
return ""
|
||||
}
|
||||
b, err := json.Marshal(raw)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var direct struct {
|
||||
DownloadCode string `json:"downloadCode"`
|
||||
}
|
||||
if json.Unmarshal(b, &direct) == nil && strings.TrimSpace(direct.DownloadCode) != "" {
|
||||
return strings.TrimSpace(direct.DownloadCode)
|
||||
}
|
||||
// Keep compatibility with transports that preserve the callback envelope.
|
||||
var nested struct {
|
||||
Content struct {
|
||||
DownloadCode string `json:"downloadCode"`
|
||||
} `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(b, &nested); err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(nested.Content.DownloadCode)
|
||||
}
|
||||
|
||||
// StreamClient wraps the official DingTalk Stream SDK client. It wires
|
||||
|
||||
Reference in New Issue
Block a user