Skip to content

fix(feishu): preserve sender identity and resolve mentions in merge_forward messages#39001

Open
xiaoxinpan wants to merge 1 commit intoopenclaw:mainfrom
xiaoxinpan:fix/feishu-merge-forward-sender-mentions
Open

fix(feishu): preserve sender identity and resolve mentions in merge_forward messages#39001
xiaoxinpan wants to merge 1 commit intoopenclaw:mainfrom
xiaoxinpan:fix/feishu-merge-forward-sender-mentions

Conversation

@xiaoxinpan
Copy link
Copy Markdown

Summary

When users forward merged chat messages (合并转发) to a Feishu bot, the formatted output currently loses two critical pieces of information:

  1. Sender identity — each sub-message's sender.id (open_id) is available in the API response but discarded during formatting
  2. @mention resolution — Feishu replaces real user references with @_user_N placeholders in forwarded content, but the API provides a mentions array on each item that maps these placeholders back to real names and IDs

Before

[Merged and Forwarded Messages]
- 这个方案可行吗?
- @_user_1 @_user_2 帮忙看一下
- 我觉得没问题,可以推进

After

[Merged and Forwarded Messages]
- [ou_aaa111] 这个方案可行吗?
- [ou_bbb222] @张三(ou_ccc333) @李四(ou_ddd444) 帮忙看一下
- [ou_eee555] 我觉得没问题,可以推进

Changes

extensions/feishu/src/bot.tsparseMergeForwardContent()

  • Added mentions to the item type definition (the Feishu im.message.get API already returns this field, it was just not declared)
  • Prepend [sender.id] to each sub-message line
  • Resolve @_user_N placeholders using each item's mentions array, displaying as @name(open_id)

How it works

The Feishu API (/im/v1/messages/:message_id) returns merge_forward items with this structure:

{
  "sender": { "id": "ou_aaa111" },
  "mentions": [
    { "key": "@_user_1", "id": "ou_ccc333", "name": "张三" },
    { "key": "@_user_2", "id": "ou_ddd444", "name": "李四" }
  ],
  "body": { "content": "..." },
  "msg_type": "text"
}

The mentions array maps each @_user_N placeholder back to the real user. This PR uses that mapping to restore the original user references.

Test plan

  • Tested with real Feishu merge_forward messages containing @mentions — placeholders correctly resolved to @name(open_id)
  • Tested with merge_forward messages without @mentions — no regression, sender ID still shown
  • Verified mentions field structure via debug logging against live Feishu API responses

🤖 Generated with Claude Code

@openclaw-barnacle openclaw-barnacle bot added channel: feishu Channel integration: feishu size: XS labels Mar 7, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 7, 2026

Greptile Summary

This PR enhances the Feishu merge-forward message formatter by prepending each sub-message line with its sender's open_id and resolving @_user_N mention placeholders back to real names via the mentions array already present in the Feishu API response. The overall approach is sound and the added type declaration matches the documented API shape.

Issues found:

  • Mention-replacement ordering bug (logic): Using replaceAll(m.key, …) with a plain string on an unordered list of keys means a shorter key like @_user_1 will partially replace the longer key @_user_10, leaving stray digits and mis-resolving the second mention. This is reproducible any time a single sub-message mentions 10+ distinct users. The fix is to sort item.mentions by key.length descending before iterating, so longer tokens are replaced first.

Confidence Score: 3/5

  • Safe to merge for the common case, but contains a logic bug that produces incorrect output for merge-forward messages with 10+ distinct mentions.
  • The change is small and well-scoped. The sender-ID prepend works correctly. The mention-resolution logic is almost right but has a key-prefix collision bug: replaceAll('@_user_1', …) on a string that also contains @_user_10 will corrupt the longer token. This is an edge case (requires ≥10 unique mentions in a single sub-message) but is a real logical error worth fixing before merge.
  • extensions/feishu/src/bot.ts — the mention-replacement loop (lines 416–420)

Last reviewed commit: a3a2ba4

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a3a2ba4174

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@xiaoxinpan xiaoxinpan force-pushed the fix/feishu-merge-forward-sender-mentions branch from efabb23 to 5f27a4f Compare March 7, 2026 16:53
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5f27a4f915

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@xiaoxinpan xiaoxinpan force-pushed the fix/feishu-merge-forward-sender-mentions branch from de9d3c6 to 0ad05ef Compare March 7, 2026 18:12
@openclaw-barnacle openclaw-barnacle bot added commands Command implementations size: S and removed size: XS labels Mar 7, 2026
@xiaoxinpan xiaoxinpan force-pushed the fix/feishu-merge-forward-sender-mentions branch from 6297723 to 4f91d64 Compare March 7, 2026 18:44
@openclaw-barnacle openclaw-barnacle bot added size: XS and removed commands Command implementations size: S labels Mar 7, 2026
@xiaoxinpan xiaoxinpan force-pushed the fix/feishu-merge-forward-sender-mentions branch 4 times, most recently from de16aa0 to 4fbbce1 Compare March 14, 2026 02:52
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4fbbce1815

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@xiaoxinpan xiaoxinpan force-pushed the fix/feishu-merge-forward-sender-mentions branch from 4fbbce1 to dd72cb5 Compare March 21, 2026 06:38
@xiaoxinpan
Copy link
Copy Markdown
Author

xiaoxinpan commented Mar 21, 2026

Rebased onto latest main (1e98dbc) — single commit: 7e45868.

What changed:

  • Each sub-message line now includes the sender's open_id: - [ou_xxx] message text
  • @_user_N mention placeholders resolved to real names via mentions array
  • Mentions sorted by key length descending to prevent @_user_1/@_user_10 collision
  • m.id normalized for string and object shapes (with union_id fallback)
  • Uses callback replacement to avoid $ substitution in mention names

All previous review feedback has been incorporated. The remaining bot review comments above are outdated — they reference the old code structure (before upstream extracted bot-content.ts from bot.ts).

@xiaoxinpan xiaoxinpan force-pushed the fix/feishu-merge-forward-sender-mentions branch from dd72cb5 to d0fb87f Compare March 21, 2026 06:45
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d0fb87fe01

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

…orward messages

Prepend each sub-message with its sender open_id and resolve @_user_N
mention placeholders back to real names via the mentions array in the
Feishu API response.

- Sort mentions by key length descending to prevent partial-match
  collisions (e.g. @_user_1 matching @_user_10)
- Normalize mention IDs for both string and object shapes, with
  union_id fallback
- Use callback replacement to avoid $ substitution in display names

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@xiaoxinpan xiaoxinpan force-pushed the fix/feishu-merge-forward-sender-mentions branch from 7f40ea2 to 7e45868 Compare March 22, 2026 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: feishu Channel integration: feishu size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant