diff --git a/extensions/feishu/src/bot.checkBotMentioned.test.ts b/extensions/feishu/src/bot.checkBotMentioned.test.ts index 197e5ba5aa2..00f20ad57be 100644 --- a/extensions/feishu/src/bot.checkBotMentioned.test.ts +++ b/extensions/feishu/src/bot.checkBotMentioned.test.ts @@ -140,6 +140,17 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { expect(ctx.mentionedBot).toBe(false); }); + it.each([undefined, "", " "])( + "does not create mention-forward targets when botOpenId is %j", + (botOpenId) => { + const event = makeEvent("p2p", [ + { key: "@_user_1", name: "Alice", id: { open_id: "ou_alice" } }, + ]); + const ctx = parseFeishuMessageEvent(event, botOpenId); + expect(ctx.mentionTargets).toBeUndefined(); + }, + ); + it("returns mentionedBot=false when botOpenId is empty string (probe failed)", () => { const event = makeEvent("group", [ { key: "@_user_1", name: "Alice", id: { open_id: "ou_alice" } }, diff --git a/extensions/feishu/src/bot.ts b/extensions/feishu/src/bot.ts index 8f0b48ec469..a14d4dfcac2 100644 --- a/extensions/feishu/src/bot.ts +++ b/extensions/feishu/src/bot.ts @@ -319,8 +319,9 @@ export function parseFeishuMessageEvent( }; // Detect mention forward request: message mentions bot + at least one other user - if (isMentionForwardRequest(event, botOpenId)) { - const mentionTargets = extractMentionTargets(event, botOpenId); + const mentionForwardBotOpenId = botOpenId?.trim(); + if (mentionForwardBotOpenId && isMentionForwardRequest(event, mentionForwardBotOpenId)) { + const mentionTargets = extractMentionTargets(event, mentionForwardBotOpenId); if (mentionTargets.length > 0) { ctx.mentionTargets = mentionTargets; } diff --git a/extensions/feishu/src/mention.ts b/extensions/feishu/src/mention.ts index 5a567e3e32c..00eb3c3dbd4 100644 --- a/extensions/feishu/src/mention.ts +++ b/extensions/feishu/src/mention.ts @@ -28,7 +28,7 @@ export function isFeishuBroadcastMention(mention: FeishuMentionLike): boolean { */ export function extractMentionTargets( event: FeishuMessageEvent, - botOpenId?: string, + botOpenId: string, ): MentionTarget[] { const mentions = event.message.mentions ?? []; @@ -38,7 +38,7 @@ export function extractMentionTargets( return false; } // Exclude the bot itself - if (botOpenId && m.id.open_id === botOpenId) { + if (m.id.open_id === botOpenId) { return false; } // Must have open_id @@ -62,17 +62,21 @@ export function isMentionForwardRequest(event: FeishuMessageEvent, botOpenId?: s if (mentions.length === 0) { return false; } + const normalizedBotOpenId = botOpenId?.trim(); + if (!normalizedBotOpenId) { + return false; + } const isDirectMessage = !isFeishuGroupChatType(event.message.chat_type); const userMentions = mentions.filter((m) => !isFeishuBroadcastMention(m)); - const hasOtherMention = userMentions.some((m) => m.id.open_id !== botOpenId); + const hasOtherMention = userMentions.some((m) => m.id.open_id !== normalizedBotOpenId); if (isDirectMessage) { // DM: trigger if any non-bot user is mentioned return hasOtherMention; } // Group: need to mention both bot and other users - const hasBotMention = userMentions.some((m) => m.id.open_id === botOpenId); + const hasBotMention = userMentions.some((m) => m.id.open_id === normalizedBotOpenId); return hasBotMention && hasOtherMention; }