From b7b7375ca7ead92d889f3d7625295fde96e85422 Mon Sep 17 00:00:00 2001 From: zhaoyunxing Date: Wed, 18 Mar 2026 22:53:26 +0800 Subject: [PATCH] refactor(dingtalk): simplify uuid usage and improve session handling --- pkg/channels/dingtalk/client.go | 16 +++++----------- pkg/channels/dingtalk/dingtalk.go | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/pkg/channels/dingtalk/client.go b/pkg/channels/dingtalk/client.go index 40d069dd1..bd4e1caa6 100644 --- a/pkg/channels/dingtalk/client.go +++ b/pkg/channels/dingtalk/client.go @@ -110,13 +110,10 @@ func (c *Client) BatchSendMessages(ctx context.Context, msgType MessageType, use // CardStreaming updates the content of a card instance identified by cardInstanceID. func (c *Client) CardStreaming(ctx context.Context, cardInstanceID, content string) error { - id, err := uuid.NewUUID() - if err != nil { - return err - } + guid := uuid.NewString() body := map[string]any{ "outTrackId": cardInstanceID, - "guid": id.String(), + "guid": guid, "key": c.cardTemplateContentKey, "content": content, "isFull": true, @@ -149,19 +146,16 @@ func (c *Client) CardCreateAndDeliver(ctx context.Context, chatbot *chatbot.BotC } ) - id, err := uuid.NewUUID() - if err != nil { - return "", err - } if group { openSpaceID = "dtv1.card//IM_GROUP." + chatbot.ConversationId imRobotOpenDeliverModel = map[string]any{} imGroupOpenDeliverModel["robotCode"] = c.robotCode } + outTrackId := uuid.NewString() body := map[string]any{ "cardTemplateId": c.cardTemplateID, - "outTrackId": id.String(), + "outTrackId": outTrackId, "cardData": map[string]any{}, "openSpaceId": openSpaceID, "userIdType": 1, @@ -198,7 +192,7 @@ func (c *Client) CardCreateAndDeliver(ctx context.Context, chatbot *chatbot.BotC } `json:"result"` Success bool `json:"success"` }{} - if err = c.httpRequest(ctx, http.MethodPost, createCardAndDeliver, body, &resp); err != nil { + if err := c.httpRequest(ctx, http.MethodPost, createCardAndDeliver, body, &resp); err != nil { return "", err } if resp.Success { diff --git a/pkg/channels/dingtalk/dingtalk.go b/pkg/channels/dingtalk/dingtalk.go index 8e0db8bbf..95e6e9384 100644 --- a/pkg/channels/dingtalk/dingtalk.go +++ b/pkg/channels/dingtalk/dingtalk.go @@ -106,6 +106,16 @@ func (c *DingTalkChannel) Stop(ctx context.Context) error { c.streamClient.Close() } + // Clear session-related state on shutdown + c.sessionWebhooks.Range(func(key, _ any) bool { + c.sessionWebhooks.Delete(key) + return true + }) + c.cardInstanceIDs.Range(func(key, _ any) bool { + c.cardInstanceIDs.Delete(key) + return true + }) + c.SetRunning(false) logger.InfoC("dingtalk", "DingTalk channel stopped") return nil @@ -117,7 +127,7 @@ func (c *DingTalkChannel) Send(ctx context.Context, msg bus.OutboundMessage) err return channels.ErrNotRunning } // Check if we have a card instance ID for this chat (indicating we can send a card reply) - cardInstanceIDRaw, ok := c.cardInstanceIDs.LoadAndDelete(msg.ChatID) + cardInstanceIDRaw, ok := c.cardInstanceIDs.Load(msg.ChatID) if !ok { return c.SendDirectReply(ctx, msg) } @@ -202,12 +212,17 @@ func (c *DingTalkChannel) onChatBotMessageReceived( if c.config.CardTemplateID != "" { // If it fails, log the error but continue with normal message handling if cardID, err := c.tryCardCreateAndDeliver(ctx, data); err != nil { - logger.WarnC("dingtalk", "Failed to create or deliver card, falling back to direct reply") + logger.WarnCF("dingtalk", "Failed to create or deliver card, falling back to direct reply", map[string]any{ + "error": err.Error(), + "sender_id": senderID, + "msg_id": data.MsgId, + }) // Store the session webhook for this chat so we can reply later c.sessionWebhooks.Store(chatID, data.SessionWebhook) } else { chatID = data.MsgId c.cardInstanceIDs.Store(chatID, cardID) + c.sessionWebhooks.Store(chatID, data.SessionWebhook) } } else { // Card feature not configured; just store the session webhook for direct replies