feat(dingtalk): land the finished answer through the plain card update
Streamed card content is replayed by the DingTalk client with a typewriter animation it drives itself, so an answer that is already complete on the server can still be typed out - and a client that leaves the foreground pauses that animation, which reads as 'streaming stopped, it continues when I switch back' long after the final frame was pushed. Add DINGTALK_CARD_FINAL_WRITE (streaming default | update): in update mode the streaming finalize is followed by one plain PUT /v1.0/card/instances write with the identical template variables, so the terminal content has no typewriter queue behind it. The follow-up write never fails a delivered answer.
This commit is contained in:
@@ -22,3 +22,25 @@ func cardPartialUpdatesMode() dingtalk.CardPartialUpdatesMode {
|
||||
log.Printf("DingTalk card partial updates mode=%s", mode)
|
||||
return mode
|
||||
}
|
||||
|
||||
// cardFinalWriteMode returns the configured terminal-write mode for a finished
|
||||
// answer (see dingtalk.CardFinalWriteMode):
|
||||
//
|
||||
// unset / streaming → the finished answer is written with the card streaming
|
||||
// API and isFinalize=true (historical behaviour)
|
||||
// update / plain → the same answer is written once more through the plain
|
||||
// card update API (PUT /v1.0/card/instances), so the
|
||||
// terminal content carries no client-side typewriter queue
|
||||
//
|
||||
// DingTalk renders streamed card content with a typewriter animation the client
|
||||
// drives itself, so an answer that is already complete on the server can still be
|
||||
// replayed character by character - and a client that is not in the foreground
|
||||
// pauses that animation, which reads as "streaming stopped, it continues when I
|
||||
// switch back". The default stays "streaming" so an environment without the
|
||||
// variable behaves exactly as before; DINGTALK_CARD_FINAL_WRITE selects the other
|
||||
// mode. The resolved mode is logged so the startup log always states what ran.
|
||||
func cardFinalWriteMode() dingtalk.CardFinalWriteMode {
|
||||
mode := dingtalk.ParseCardFinalWriteMode(os.Getenv("DINGTALK_CARD_FINAL_WRITE"))
|
||||
log.Printf("DingTalk card final write mode=%s", mode)
|
||||
return mode
|
||||
}
|
||||
|
||||
@@ -29,3 +29,27 @@ func TestCardPartialUpdatesMode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardFinalWriteMode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
want dingtalk.CardFinalWriteMode
|
||||
}{
|
||||
{"unset keeps the streaming finalize", "", dingtalk.CardFinalWriteStreaming},
|
||||
{"streaming stays streaming", "streaming", dingtalk.CardFinalWriteStreaming},
|
||||
{"unknown keeps the streaming finalize", "hammer", dingtalk.CardFinalWriteStreaming},
|
||||
{"update", " update ", dingtalk.CardFinalWriteUpdate},
|
||||
{"plain alias", "plain", dingtalk.CardFinalWriteUpdate},
|
||||
{"on alias", "ON", dingtalk.CardFinalWriteUpdate},
|
||||
{"1 alias", "1", dingtalk.CardFinalWriteUpdate},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Setenv("DINGTALK_CARD_FINAL_WRITE", tt.raw)
|
||||
if got := cardFinalWriteMode(); got != tt.want {
|
||||
t.Fatalf("cardFinalWriteMode(%q)=%q want %q", tt.raw, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,6 +298,7 @@ func main() {
|
||||
cardTemplateID,
|
||||
nil,
|
||||
dingtalk.WithCardDebugUserID(cardDebugUserID),
|
||||
dingtalk.WithCardFinalWrite(cardFinalWriteMode()),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("DingTalk card sender init failed: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user