fix(im/feishu): reply via reply-message API so replies land in the original thread

Previously the Feishu adapter sent replies with the "send message" API
(POST /im/v1/messages) without a root_id. In topic-enabled groups this
spawned a new topic for every reply instead of attaching to the thread
the user @-mentioned the bot from, fragmenting the conversation.

Switch SendReply (text) and sendCardByCardID (streaming card) to the
"reply message" API (POST /im/v1/messages/:message_id/reply), using
incoming.MessageID as the path parameter. Feishu automatically replies
in thread form when the replied-to message is itself a thread message,
so no reply_in_thread flag or session-mode plumbing is needed.

Add sendWithFallback to retry via the plain send-message API on
fallback-eligible error codes (230019 topic gone, 230054 unsupported
message type, 230071 group does not support reply-in-thread), and to
skip straight to the fallback when incoming.MessageID is empty. This
keeps replies deliverable even when the reply API cannot be used.

Also add a debug log in convertEvent (longconn.go) that prints every
raw received event (msg_type/chat_id/msg_id/root_id/parent_id/thread_id/
content) to aid diagnosing intermittent event-delivery issues.

No interface signature changes; other platform adapters untouched.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
养乐多
2026-07-02 17:45:22 +08:00
committed by lyingbug
parent b4b63a0c1f
commit 30f7630ac4
2 changed files with 143 additions and 56 deletions
+21
View File
@@ -97,10 +97,22 @@ func (l *feishuLoggerAdapter) Error(ctx context.Context, args ...interface{}) {
// Supports text and file messages. Returns nil for unsupported types.
func convertEvent(event *larkim.P2MessageReceiveV1) *im.IncomingMessage {
if event == nil || event.Event == nil || event.Event.Message == nil {
logger.Warnf(context.Background(), "[Feishu][RX] event dropped: nil event/event/message")
return nil
}
msg := event.Event.Message
// Debug: log every raw receive event so we can see exactly what Feishu
// delivers (or doesn't). This is the single chokepoint for all message
// types — adding it here catches text/file/image/post uniformly.
logger.Infof(context.Background(),
"[Feishu][RX] msg_type=%q chat_type=%q chat_id=%q msg_id=%q root_id=%q parent_id=%q thread_id=%q content=%q",
ptrStr(msg.MessageType), ptrStr(msg.ChatType), ptrStr(msg.ChatId),
ptrStr(msg.MessageId), ptrStr(msg.RootId), ptrStr(msg.ParentId),
ptrStr(msg.ThreadId), ptrStr(msg.Content),
)
if msg.MessageType == nil {
return nil
}
@@ -304,3 +316,12 @@ func convertPostEvent(msg *larkim.EventMessage, openID, chatID string, chatType
MessageID: messageID,
}
}
// ptrStr safely dereferences a *string for logging; returns "" for nil so the
// log line stays readable instead of printing <nil>.
func ptrStr(s *string) string {
if s == nil {
return ""
}
return *s
}