fix(channels): use LoadAndDelete for card instance ID and refactor card logic

This commit is contained in:
zhaoyunxing 2026-03-13 20:54:35 +08:00
parent b6139490eb
commit 316e29f208

View file

@ -117,7 +117,7 @@ func (c *DingTalkChannel) Send(ctx context.Context, msg bus.OutboundMessage) err
return channels.ErrNotRunning return channels.ErrNotRunning
} }
// Check if we have a card instance ID for this chat (indicating we can send a card reply) // Check if we have a card instance ID for this chat (indicating we can send a card reply)
cardInstanceIDRaw, ok := c.cardInstanceIDs.Load(msg.ChatID) cardInstanceIDRaw, ok := c.cardInstanceIDs.LoadAndDelete(msg.ChatID)
if !ok { if !ok {
return c.SendDirectReply(ctx, msg) return c.SendDirectReply(ctx, msg)
} }
@ -199,15 +199,15 @@ func (c *DingTalkChannel) onChatBotMessageReceived(
// Try to create and deliver card (optional feature) // Try to create and deliver card (optional feature)
// If it fails, log the error but continue with normal message handling // If it fails, log the error but continue with normal message handling
if err := c.tryCardCreateAndDeliver(ctx, chatID, data); err != nil { if cardID, err := c.tryCardCreateAndDeliver(ctx, data); err != nil {
logger.WarnCF("dingtalk", "Failed to create or deliver card, falling back to direct reply", map[string]any{ logger.WarnC("dingtalk", "Failed to create or deliver card, falling back to direct reply")
"error": err.Error(), // Store the session webhook for this chat so we can reply later
"chat_id": chatID, c.sessionWebhooks.Store(chatID, data.SessionWebhook)
"sender_id": senderID, } else {
}) chatID = data.MsgId
c.cardInstanceIDs.Store(chatID, cardID)
} }
// Store the session webhook for this chat so we can reply later
c.sessionWebhooks.Store(chatID, data.SessionWebhook)
// Handle the message through the base channel // Handle the message through the base channel
c.HandleMessage(ctx, peer, "", senderID, chatID, content, nil, metadata, sender) c.HandleMessage(ctx, peer, "", senderID, chatID, content, nil, metadata, sender)
@ -257,16 +257,10 @@ func (c *DingTalkChannel) SendCardReply(ctx context.Context, cardInstanceID, con
func (c *DingTalkChannel) tryCardCreateAndDeliver( func (c *DingTalkChannel) tryCardCreateAndDeliver(
ctx context.Context, ctx context.Context,
chatID string,
data *chatbot.BotCallbackDataModel, data *chatbot.BotCallbackDataModel,
) error { ) (string, error) {
if c.config.CardTemplateID == "" { if c.config.CardTemplateID == "" {
return nil return "", fmt.Errorf("card_template_id is not configured, cannot create card")
} }
cardInstanceID, err := c.client.CardCreateAndDeliver(ctx, data) return c.client.CardCreateAndDeliver(ctx, data)
if err != nil {
return err
}
c.cardInstanceIDs.Store(chatID, cardInstanceID)
return nil
} }