fix: finalize DingTalk AI cards
This commit is contained in:
@@ -17,7 +17,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
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.
|
// CardRequest is the input for sending a final AI card.
|
||||||
@@ -35,6 +36,7 @@ type CardSender struct {
|
|||||||
cardTemplateID string
|
cardTemplateID string
|
||||||
tokenURL string
|
tokenURL string
|
||||||
createURL string
|
createURL string
|
||||||
|
streamURL string
|
||||||
apiClient *http.Client
|
apiClient *http.Client
|
||||||
allowTestURL bool
|
allowTestURL bool
|
||||||
|
|
||||||
@@ -47,15 +49,16 @@ type CardSender struct {
|
|||||||
// after trimming whitespace. A nil http.Client installs a hardened default
|
// after trimming whitespace. A nil http.Client installs a hardened default
|
||||||
// (no redirects, Proxy=nil, 15s timeout).
|
// (no redirects, Proxy=nil, 15s timeout).
|
||||||
func NewCardSender(clientID, clientSecret, cardTemplateID string, client *http.Client) (*CardSender, error) {
|
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.
|
// newCardSenderForTest is used by tests to inject httptest URLs.
|
||||||
func newCardSenderForTest(clientID, clientSecret, cardTemplateID string, client *http.Client, tokenURL, createURL string) (*CardSender, error) {
|
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)
|
clientID = strings.TrimSpace(clientID)
|
||||||
clientSecret = strings.TrimSpace(clientSecret)
|
clientSecret = strings.TrimSpace(clientSecret)
|
||||||
cardTemplateID = strings.TrimSpace(cardTemplateID)
|
cardTemplateID = strings.TrimSpace(cardTemplateID)
|
||||||
@@ -94,6 +97,7 @@ func newCardSender(clientID, clientSecret, cardTemplateID string, client *http.C
|
|||||||
cardTemplateID: cardTemplateID,
|
cardTemplateID: cardTemplateID,
|
||||||
tokenURL: tokenURL,
|
tokenURL: tokenURL,
|
||||||
createURL: createURL,
|
createURL: createURL,
|
||||||
|
streamURL: streamURL,
|
||||||
apiClient: &apiClient,
|
apiClient: &apiClient,
|
||||||
allowTestURL: allowTest,
|
allowTestURL: allowTest,
|
||||||
}, nil
|
}, nil
|
||||||
@@ -147,6 +151,9 @@ func (s *CardSender) SendFinalCard(ctx context.Context, req CardRequest) (string
|
|||||||
if err := s.postCard(ctx, body, token); err != nil {
|
if err := s.postCard(ctx, body, token); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
if err := s.finalizeCard(ctx, outTrackID, req.Content, token); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
return outTrackID, nil
|
return outTrackID, nil
|
||||||
}
|
}
|
||||||
@@ -235,13 +242,35 @@ func (s *CardSender) postCard(ctx context.Context, body map[string]any, token st
|
|||||||
return nil
|
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) {
|
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)
|
body, err := json.Marshal(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("dingtalk card: encode API request failed")
|
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 {
|
if err != nil {
|
||||||
return nil, errors.New("dingtalk card: create API request failed")
|
return nil, errors.New("dingtalk card: create API request failed")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,9 +20,13 @@ type cardTestServer struct {
|
|||||||
server *httptest.Server
|
server *httptest.Server
|
||||||
tokenCalls int
|
tokenCalls int
|
||||||
createCalls int
|
createCalls int
|
||||||
|
streamCalls int
|
||||||
lastTokenBody map[string]string
|
lastTokenBody map[string]string
|
||||||
lastCreateBody map[string]any
|
lastCreateBody map[string]any
|
||||||
lastCreateHeader string
|
lastCreateHeader string
|
||||||
|
lastStreamBody map[string]any
|
||||||
|
lastStreamHeader string
|
||||||
|
lastStreamMethod string
|
||||||
// configurable responses
|
// configurable responses
|
||||||
tokenResponse any
|
tokenResponse any
|
||||||
tokenStatus int
|
tokenStatus int
|
||||||
@@ -68,6 +72,14 @@ func newCardTestServer() *cardTestServer {
|
|||||||
json.NewDecoder(r.Body).Decode(&ts.lastCreateBody)
|
json.NewDecoder(r.Body).Decode(&ts.lastCreateBody)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(ts.createResponse)
|
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:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
@@ -157,6 +169,20 @@ func TestCardSendDirectPayload(t *testing.T) {
|
|||||||
if ts.lastCreateHeader != "test-access-token" {
|
if ts.lastCreateHeader != "test-access-token" {
|
||||||
t.Fatalf("create header token=%q", ts.lastCreateHeader)
|
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
|
// Verify create body
|
||||||
body := ts.lastCreateBody
|
body := ts.lastCreateBody
|
||||||
|
|||||||
Reference in New Issue
Block a user