refactor(dingtalk): simplify uuid usage and improve session handling
This commit is contained in:
parent
052b8f1b0a
commit
b7b7375ca7
2 changed files with 22 additions and 13 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue