fix: finalize DingTalk AI cards

This commit is contained in:
姚章浩
2026-07-20 21:51:00 +08:00
parent c4a942a1ac
commit ade2d076ce
2 changed files with 60 additions and 5 deletions
+34 -5
View File
@@ -17,7 +17,8 @@ import (
)
const (
cardCreateURL = "https://api.dingtalk.com/v1.0/card/instances/createAndDeliver"
cardCreateURL = "https://api.dingtalk.com/v1.0/card/instances/createAndDeliver"
cardStreamingURL = "https://api.dingtalk.com/v1.0/card/streaming"
)
// CardRequest is the input for sending a final AI card.
@@ -35,6 +36,7 @@ type CardSender struct {
cardTemplateID string
tokenURL string
createURL string
streamURL string
apiClient *http.Client
allowTestURL bool
@@ -47,15 +49,16 @@ type CardSender struct {
// after trimming whitespace. A nil http.Client installs a hardened default
// (no redirects, Proxy=nil, 15s timeout).
func NewCardSender(clientID, clientSecret, cardTemplateID string, client *http.Client) (*CardSender, error) {
return newCardSender(clientID, clientSecret, cardTemplateID, client, oauthTokenURL, cardCreateURL, false)
return newCardSender(clientID, clientSecret, cardTemplateID, client, oauthTokenURL, cardCreateURL, cardStreamingURL, false)
}
// newCardSenderForTest is used by tests to inject httptest URLs.
func newCardSenderForTest(clientID, clientSecret, cardTemplateID string, client *http.Client, tokenURL, createURL string) (*CardSender, error) {
return newCardSender(clientID, clientSecret, cardTemplateID, client, tokenURL, createURL, true)
streamURL := strings.TrimSuffix(createURL, "/create") + "/stream"
return newCardSender(clientID, clientSecret, cardTemplateID, client, tokenURL, createURL, streamURL, true)
}
func newCardSender(clientID, clientSecret, cardTemplateID string, client *http.Client, tokenURL, createURL string, allowTest bool) (*CardSender, error) {
func newCardSender(clientID, clientSecret, cardTemplateID string, client *http.Client, tokenURL, createURL, streamURL string, allowTest bool) (*CardSender, error) {
clientID = strings.TrimSpace(clientID)
clientSecret = strings.TrimSpace(clientSecret)
cardTemplateID = strings.TrimSpace(cardTemplateID)
@@ -94,6 +97,7 @@ func newCardSender(clientID, clientSecret, cardTemplateID string, client *http.C
cardTemplateID: cardTemplateID,
tokenURL: tokenURL,
createURL: createURL,
streamURL: streamURL,
apiClient: &apiClient,
allowTestURL: allowTest,
}, nil
@@ -147,6 +151,9 @@ func (s *CardSender) SendFinalCard(ctx context.Context, req CardRequest) (string
if err := s.postCard(ctx, body, token); err != nil {
return "", err
}
if err := s.finalizeCard(ctx, outTrackID, req.Content, token); err != nil {
return "", err
}
return outTrackID, nil
}
@@ -235,13 +242,35 @@ func (s *CardSender) postCard(ctx context.Context, body map[string]any, token st
return nil
}
func (s *CardSender) finalizeCard(ctx context.Context, outTrackID, content, token string) error {
guid, err := generateUUID()
if err != nil {
return errors.New("dingtalk card: generate stream guid failed")
}
body := map[string]any{
"outTrackId": outTrackID,
"guid": guid,
"key": "content",
"content": content,
"isFull": true,
"isFinalize": true,
"isError": false,
}
_, err = s.doJSON(ctx, http.MethodPut, s.streamURL, body, token)
return err
}
func (s *CardSender) postJSON(ctx context.Context, endpoint string, value any, token string) ([]byte, error) {
return s.doJSON(ctx, http.MethodPost, endpoint, value, token)
}
func (s *CardSender) doJSON(ctx context.Context, method, endpoint string, value any, token string) ([]byte, error) {
body, err := json.Marshal(value)
if err != nil {
return nil, errors.New("dingtalk card: encode API request failed")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
req, err := http.NewRequestWithContext(ctx, method, endpoint, bytes.NewReader(body))
if err != nil {
return nil, errors.New("dingtalk card: create API request failed")
}
+26
View File
@@ -20,9 +20,13 @@ type cardTestServer struct {
server *httptest.Server
tokenCalls int
createCalls int
streamCalls int
lastTokenBody map[string]string
lastCreateBody map[string]any
lastCreateHeader string
lastStreamBody map[string]any
lastStreamHeader string
lastStreamMethod string
// configurable responses
tokenResponse any
tokenStatus int
@@ -68,6 +72,14 @@ func newCardTestServer() *cardTestServer {
json.NewDecoder(r.Body).Decode(&ts.lastCreateBody)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(ts.createResponse)
case "/stream":
ts.streamCalls++
ts.lastStreamMethod = r.Method
ts.lastStreamHeader = r.Header.Get("x-acs-dingtalk-access-token")
ts.lastStreamBody = make(map[string]any)
json.NewDecoder(r.Body).Decode(&ts.lastStreamBody)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{"result": "ok"})
default:
http.NotFound(w, r)
}
@@ -157,6 +169,20 @@ func TestCardSendDirectPayload(t *testing.T) {
if ts.lastCreateHeader != "test-access-token" {
t.Fatalf("create header token=%q", ts.lastCreateHeader)
}
if ts.streamCalls != 1 {
t.Fatalf("streamCalls=%d, want 1", ts.streamCalls)
}
if ts.lastStreamMethod != http.MethodPut {
t.Fatalf("stream method=%q, want PUT", ts.lastStreamMethod)
}
if ts.lastStreamHeader != "test-access-token" {
t.Fatalf("stream header token=%q", ts.lastStreamHeader)
}
if ts.lastStreamBody["outTrackId"] != outTrackID || ts.lastStreamBody["key"] != "content" ||
ts.lastStreamBody["content"] != "Hello from AI" || ts.lastStreamBody["isFull"] != true ||
ts.lastStreamBody["isFinalize"] != true || ts.lastStreamBody["isError"] != false {
t.Fatalf("unexpected final stream body: %#v", ts.lastStreamBody)
}
// Verify create body
body := ts.lastCreateBody