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.
|
// CardStreaming updates the content of a card instance identified by cardInstanceID.
|
||||||
func (c *Client) CardStreaming(ctx context.Context, cardInstanceID, content string) error {
|
func (c *Client) CardStreaming(ctx context.Context, cardInstanceID, content string) error {
|
||||||
id, err := uuid.NewUUID()
|
guid := uuid.NewString()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
body := map[string]any{
|
body := map[string]any{
|
||||||
"outTrackId": cardInstanceID,
|
"outTrackId": cardInstanceID,
|
||||||
"guid": id.String(),
|
"guid": guid,
|
||||||
"key": c.cardTemplateContentKey,
|
"key": c.cardTemplateContentKey,
|
||||||
"content": content,
|
"content": content,
|
||||||
"isFull": true,
|
"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 {
|
if group {
|
||||||
openSpaceID = "dtv1.card//IM_GROUP." + chatbot.ConversationId
|
openSpaceID = "dtv1.card//IM_GROUP." + chatbot.ConversationId
|
||||||
imRobotOpenDeliverModel = map[string]any{}
|
imRobotOpenDeliverModel = map[string]any{}
|
||||||
imGroupOpenDeliverModel["robotCode"] = c.robotCode
|
imGroupOpenDeliverModel["robotCode"] = c.robotCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outTrackId := uuid.NewString()
|
||||||
body := map[string]any{
|
body := map[string]any{
|
||||||
"cardTemplateId": c.cardTemplateID,
|
"cardTemplateId": c.cardTemplateID,
|
||||||
"outTrackId": id.String(),
|
"outTrackId": outTrackId,
|
||||||
"cardData": map[string]any{},
|
"cardData": map[string]any{},
|
||||||
"openSpaceId": openSpaceID,
|
"openSpaceId": openSpaceID,
|
||||||
"userIdType": 1,
|
"userIdType": 1,
|
||||||
|
|
@ -198,7 +192,7 @@ func (c *Client) CardCreateAndDeliver(ctx context.Context, chatbot *chatbot.BotC
|
||||||
} `json:"result"`
|
} `json:"result"`
|
||||||
Success bool `json:"success"`
|
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
|
return "", err
|
||||||
}
|
}
|
||||||
if resp.Success {
|
if resp.Success {
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,16 @@ func (c *DingTalkChannel) Stop(ctx context.Context) error {
|
||||||
c.streamClient.Close()
|
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)
|
c.SetRunning(false)
|
||||||
logger.InfoC("dingtalk", "DingTalk channel stopped")
|
logger.InfoC("dingtalk", "DingTalk channel stopped")
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -117,7 +127,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.LoadAndDelete(msg.ChatID)
|
cardInstanceIDRaw, ok := c.cardInstanceIDs.Load(msg.ChatID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return c.SendDirectReply(ctx, msg)
|
return c.SendDirectReply(ctx, msg)
|
||||||
}
|
}
|
||||||
|
|
@ -202,12 +212,17 @@ func (c *DingTalkChannel) onChatBotMessageReceived(
|
||||||
if c.config.CardTemplateID != "" {
|
if c.config.CardTemplateID != "" {
|
||||||
// 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 cardID, err := c.tryCardCreateAndDeliver(ctx, data); err != nil {
|
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
|
// Store the session webhook for this chat so we can reply later
|
||||||
c.sessionWebhooks.Store(chatID, data.SessionWebhook)
|
c.sessionWebhooks.Store(chatID, data.SessionWebhook)
|
||||||
} else {
|
} else {
|
||||||
chatID = data.MsgId
|
chatID = data.MsgId
|
||||||
c.cardInstanceIDs.Store(chatID, cardID)
|
c.cardInstanceIDs.Store(chatID, cardID)
|
||||||
|
c.sessionWebhooks.Store(chatID, data.SessionWebhook)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Card feature not configured; just store the session webhook for direct replies
|
// Card feature not configured; just store the session webhook for direct replies
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue