Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat(wecom): implement quote extraction and populate in handleCallback

nullkey3587ddb1c70c
diff --git a/internal/im/wecom/longconn.go b/internal/im/wecom/longconn.go
index bf9a2b582..d67348951 100644
--- a/internal/im/wecom/longconn.go
+++ b/internal/im/wecom/longconn.go
@@ -569,6 +569,18 @@ func (c *LongConnClient) handleCallback(ctx context.Context, frame wsFrame) {
return
}
+ // Populate quote context if the incoming message has a quoted/replied message.
+ if incoming != nil && msg.Quote != nil {
+ incoming.Quote = buildQuotedMessage(msg.Quote, msg.AiBotID)
+ if incoming.Quote != nil {
+ logger.Infof(ctx, "[WeCom] Quote detected: msgid=%s sender=%s is_bot=%v content_len=%d",
+ msg.Quote.MsgID, msg.Quote.From.UserID, incoming.Quote.IsBotMessage, len(incoming.Quote.Content))
+ // Debug: log raw IDs for bot identity verification during initial rollout
+ logger.Debugf(ctx, "[WeCom] Quote identity debug: quote.from.userid=%q quote.aibotid=%q msg.aibotid=%q",
+ msg.Quote.From.UserID, msg.Quote.AiBotID, msg.AiBotID)
+ }
+ }
+
if err := c.handler(ctx, incoming); err != nil {
logger.Errorf(ctx, "[WeCom] Handle message error: %v", err)
}
diff --git a/internal/im/wecom/quote.go b/internal/im/wecom/quote.go
new file mode 100644
index 000000000..a8a27ac51
--- /dev/null
+++ b/internal/im/wecom/quote.go
@@ -0,0 +1,72 @@
+package wecom
+
+import (
+ "strings"
+
+ "github.com/Tencent/WeKnora/internal/im"
+)
+
+// extractQuoteContent extracts text content from a quoted botMessage.
+// For non-text message types, returns a descriptive placeholder.
+func extractQuoteContent(quote *botMessage) string {
+ if quote == nil {
+ return ""
+ }
+ switch quote.MsgType {
+ case "text":
+ return quote.Text.Content
+ case "voice":
+ if quote.Voice.Content != "" {
+ return quote.Voice.Content
+ }
+ return "[语音消息]"
+ case "mixed":
+ var parts []string
+ for _, item := range quote.Mixed.MsgItem {
+ if item.MsgType == "text" && item.Text.Content != "" {
+ parts = append(parts, item.Text.Content)
+ } else if item.MsgType == "image" {
+ parts = append(parts, "[图片]")
+ }
+ }
+ return strings.Join(parts, "\n")
+ case "image":
+ return "[图片]"
+ case "file":
+ return "[文件]"
+ case "video":
+ return "[视频]"
+ default:
+ return "[消息]"
+ }
+}
+
+// isQuoteFromBot determines whether a quoted message was sent by the bot.
+// Uses two comparison strategies since WeCom's field mapping is undocumented.
+func isQuoteFromBot(quote *botMessage, aiBotID string) bool {
+ if quote.From.UserID != "" && aiBotID != "" && quote.From.UserID == aiBotID {
+ return true
+ }
+ if quote.AiBotID != "" && quote.AiBotID == aiBotID {
+ return true
+ }
+ return false
+}
+
+// buildQuotedMessage constructs a QuotedMessage from a botMessage quote.
+// Returns nil if the quote is nil or has no extractable content.
+func buildQuotedMessage(quote *botMessage, aiBotID string) *im.QuotedMessage {
+ if quote == nil {
+ return nil
+ }
+ content := extractQuoteContent(quote)
+ if content == "" {
+ return nil
+ }
+ return &im.QuotedMessage{
+ MessageID: quote.MsgID,
+ Content: content,
+ SenderID: quote.From.UserID,
+ IsBotMessage: isQuoteFromBot(quote, aiBotID),
+ }
+}
diff --git a/internal/im/wecom/quote_test.go b/internal/im/wecom/quote_test.go
new file mode 100644
index 000000000..742bb6a58
--- /dev/null
+++ b/internal/im/wecom/quote_test.go
@@ -0,0 +1,182 @@
+package wecom
+
+import (
+ "testing"
+)
+
+func TestExtractQuoteContent(t *testing.T) {
+ tests := []struct {
+ name string
+ msg *botMessage
+ want string
+ }{
+ {
+ name: "nil quote",
+ msg: nil,
+ want: "",
+ },
+ {
+ name: "text message",
+ msg: &botMessage{MsgType: "text", Text: struct{ Content string `json:"content"` }{Content: "hello world"}},
+ want: "hello world",
+ },
+ {
+ name: "voice with STT content",
+ msg: &botMessage{MsgType: "voice", Voice: struct{ Content string `json:"content"` }{Content: "speech text"}},
+ want: "speech text",
+ },
+ {
+ name: "voice without STT content",
+ msg: &botMessage{MsgType: "voice"},
+ want: "[语音消息]",
+ },
+ {
+ name: "image",
+ msg: &botMessage{MsgType: "image"},
+ want: "[图片]",
+ },
+ {
+ name: "file",
+ msg: &botMessage{MsgType: "file"},
+ want: "[文件]",
+ },
+ {
+ name: "video",
+ msg: &botMessage{MsgType: "video"},
+ want: "[视频]",
+ },
+ {
+ name: "unknown type",
+ msg: &botMessage{MsgType: "location"},
+ want: "[消息]",
+ },
+ {
+ name: "mixed text and image",
+ msg: &botMessage{
+ MsgType: "mixed",
+ Mixed: struct{ MsgItem []botMixedItem `json:"msg_item"` }{
+ MsgItem: []botMixedItem{
+ {MsgType: "text", Text: struct{ Content string `json:"content"` }{Content: "part1"}},
+ {MsgType: "image"},
+ {MsgType: "text", Text: struct{ Content string `json:"content"` }{Content: "part2"}},
+ },
+ },
+ },
+ want: "part1\n[图片]\npart2",
+ },
+ {
+ name: "mixed with only images",
+ msg: &botMessage{
+ MsgType: "mixed",
+ Mixed: struct{ MsgItem []botMixedItem `json:"msg_item"` }{
+ MsgItem: []botMixedItem{
+ {MsgType: "image"},
+ },
+ },
+ },
+ want: "[图片]",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := extractQuoteContent(tt.msg)
+ if got != tt.want {
+ t.Errorf("extractQuoteContent() = %q, want %q", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestIsQuoteFromBot(t *testing.T) {
+ tests := []struct {
+ name string
+ quote *botMessage
+ aiBotID string
+ want bool
+ }{
+ {
+ name: "matching from.userid",
+ quote: &botMessage{From: struct{ UserID string `json:"userid"` }{UserID: "bot123"}, AiBotID: ""},
+ aiBotID: "bot123",
+ want: true,
+ },
+ {
+ name: "matching aibotid",
+ quote: &botMessage{From: struct{ UserID string `json:"userid"` }{UserID: "other"}, AiBotID: "bot123"},
+ aiBotID: "bot123",
+ want: true,
+ },
+ {
+ name: "no match",
+ quote: &botMessage{From: struct{ UserID string `json:"userid"` }{UserID: "user456"}, AiBotID: ""},
+ aiBotID: "bot123",
+ want: false,
+ },
+ {
+ name: "empty aiBotID",
+ quote: &botMessage{From: struct{ UserID string `json:"userid"` }{UserID: "bot123"}},
+ aiBotID: "",
+ want: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := isQuoteFromBot(tt.quote, tt.aiBotID)
+ if got != tt.want {
+ t.Errorf("isQuoteFromBot() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestBuildQuotedMessage(t *testing.T) {
+ t.Run("nil quote returns nil", func(t *testing.T) {
+ if got := buildQuotedMessage(nil, "bot1"); got != nil {
+ t.Errorf("expected nil, got %+v", got)
+ }
+ })
+
+ t.Run("text quote builds correctly", func(t *testing.T) {
+ msg := &botMessage{
+ MsgID: "msg-001",
+ MsgType: "text",
+ AiBotID: "bot1",
+ From: struct{ UserID string `json:"userid"` }{UserID: "bot1"},
+ Text: struct{ Content string `json:"content"` }{Content: "hello"},
+ }
+ got := buildQuotedMessage(msg, "bot1")
+ if got == nil {
+ t.Fatal("expected non-nil")
+ }
+ if got.MessageID != "msg-001" {
+ t.Errorf("MessageID = %q, want %q", got.MessageID, "msg-001")
+ }
+ if got.Content != "hello" {
+ t.Errorf("Content = %q, want %q", got.Content, "hello")
+ }
+ if got.SenderID != "bot1" {
+ t.Errorf("SenderID = %q, want %q", got.SenderID, "bot1")
+ }
+ if !got.IsBotMessage {
+ t.Error("IsBotMessage = false, want true")
+ }
+ })
+
+ t.Run("user quote is not bot", func(t *testing.T) {
+ msg := &botMessage{
+ MsgID: "msg-002",
+ MsgType: "text",
+ From: struct{ UserID string `json:"userid"` }{UserID: "user456"},
+ Text: struct{ Content string `json:"content"` }{Content: "question"},
+ }
+ got := buildQuotedMessage(msg, "bot1")
+ if got == nil {
+ t.Fatal("expected non-nil")
+ }
+ if got.IsBotMessage {
+ t.Error("IsBotMessage = true, want false")
+ }
+ })
+}