Skip to content

fix(matrix): detect mentions in formatted_body matrix.to links#16941

Merged
vincentkoc merged 2 commits intoopenclaw:mainfrom
zerone0x:fix/matrix-formatted-body-mentions
Feb 19, 2026
Merged

fix(matrix): detect mentions in formatted_body matrix.to links#16941
vincentkoc merged 2 commits intoopenclaw:mainfrom
zerone0x:fix/matrix-formatted-body-mentions

Conversation

@zerone0x
Copy link
Contributor

@zerone0x zerone0x commented Feb 15, 2026

Summary

Matrix mention detection was failing when clients use formatted_body with matrix.to links instead of the newer m.mentions field.

Problem

Many Matrix clients (including Element) send mentions using HTML in formatted_body:

{
  "body": "Bot: hello",
  "formatted_body": "<a href=\\"https://matrix.to/#/@bot:matrix.org\\">Bot</a>: hello",
  "m.mentions": null
}

The resolveMentions() function only checked:

  1. m.mentions.room - room mention flag
  2. m.mentions.user_ids - explicit user ID array
  3. Regex patterns on plain body text

It did NOT check formatted_body for matrix.to mention links.

Solution

Added checkFormattedBodyMention() helper that:

  • Detects matrix.to links in formatted_body
  • Handles both plain and URL-encoded user IDs (@bot:matrix.org vs %40bot%3Amatrix.org)
  • Properly escapes special regex characters in user IDs

Testing

Added comprehensive test coverage for:

  • m.mentions field detection
  • formatted_body link detection (plain and URL-encoded)
  • Regex pattern matching
  • No false positives on partial matches

Fixes #6982

Greptile Summary

This PR fixes Matrix mention detection when clients use formatted_body HTML with matrix.to links instead of (or without) the newer m.mentions field. A new checkFormattedBodyMention() helper is added that parses formatted_body for matrix.to user links, handling both plain and URL-encoded user IDs. The change integrates cleanly into the existing resolveMentions() flow in handler.ts.

  • Adds formatted_body detection for matrix.to mention links alongside existing m.mentions and regex-based detection
  • Handles both plain (@bot:matrix.org) and URL-encoded (%40bot%3Amatrix.org) user IDs in href attributes
  • Comprehensive test file covering all detection paths, including false-positive guards
  • Minor bug: the URL-encoded regex path does not escape . from encodeURIComponent, which could theoretically cause false-positive matches

Confidence Score: 4/5

  • This PR is safe to merge with one minor regex escaping fix recommended.
  • The change is well-scoped, adds a clear feature (formatted_body mention detection) with comprehensive tests. The one issue found — missing regex escaping on the URL-encoded user ID — is unlikely to cause real-world false positives due to the constrained pattern (requires a quote immediately after), but is straightforward to fix. No security concerns, no breaking changes to existing behavior.
  • extensions/matrix/src/matrix/monitor/mentions.ts — the encodedUserId variable on line 32 needs regex-special-character escaping applied.

Last reviewed commit: f1ccf1e

@openclaw-barnacle openclaw-barnacle bot added channel: matrix Channel integration: matrix size: S experienced-contributor Contributor with 10+ merged PRs labels Feb 15, 2026
@steipete steipete closed this Feb 16, 2026
@steipete steipete reopened this Feb 17, 2026
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Many Matrix clients (including Element) send mentions using HTML links
in formatted_body instead of or in addition to the m.mentions field:

```json
{
  "formatted_body": "<a href=\"https://matrix.to/#/@bot:matrix.org\">Bot</a>: hello",
  "m.mentions": null
}
```

This change adds detection for matrix.to links in formatted_body,
supporting both plain and URL-encoded user IDs.

Changes:
- Add checkFormattedBodyMention() helper function
- Check formatted_body in resolveMentions()
- Add comprehensive test coverage

Fixes openclaw#6982
@zerone0x zerone0x force-pushed the fix/matrix-formatted-body-mentions branch from f1ccf1e to 2526acb Compare February 17, 2026 19:10
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@vincentkoc vincentkoc merged commit 3feb7fc into openclaw:main Feb 19, 2026
12 checks passed
@zerone0x zerone0x deleted the fix/matrix-formatted-body-mentions branch February 19, 2026 11:44
kittipond2365 pushed a commit to kittipond2365/openclaw that referenced this pull request Feb 19, 2026
…law#16941)

* fix(matrix): detect mentions in formatted_body matrix.to links

Many Matrix clients (including Element) send mentions using HTML links
in formatted_body instead of or in addition to the m.mentions field:

```json
{
  "formatted_body": "<a href=\"https://matrix.to/#/@bot:matrix.org\">Bot</a>: hello",
  "m.mentions": null
}
```

This change adds detection for matrix.to links in formatted_body,
supporting both plain and URL-encoded user IDs.

Changes:
- Add checkFormattedBodyMention() helper function
- Check formatted_body in resolveMentions()
- Add comprehensive test coverage

Fixes openclaw#6982

* Update extensions/matrix/src/matrix/monitor/mentions.ts

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

---------

Co-authored-by: zerone0x <[email protected]>
Co-authored-by: Vincent Koc <[email protected]>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
anschmieg pushed a commit to anschmieg/openclaw that referenced this pull request Feb 19, 2026
…law#16941)

* fix(matrix): detect mentions in formatted_body matrix.to links

Many Matrix clients (including Element) send mentions using HTML links
in formatted_body instead of or in addition to the m.mentions field:

```json
{
  "formatted_body": "<a href=\"https://matrix.to/#/@bot:matrix.org\">Bot</a>: hello",
  "m.mentions": null
}
```

This change adds detection for matrix.to links in formatted_body,
supporting both plain and URL-encoded user IDs.

Changes:
- Add checkFormattedBodyMention() helper function
- Check formatted_body in resolveMentions()
- Add comprehensive test coverage

Fixes openclaw#6982

* Update extensions/matrix/src/matrix/monitor/mentions.ts

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

---------

Co-authored-by: zerone0x <[email protected]>
Co-authored-by: Vincent Koc <[email protected]>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
yneth-ray-openclaw pushed a commit to yneth-ray-openclaw/openclaw that referenced this pull request Feb 19, 2026
…law#16941)

* fix(matrix): detect mentions in formatted_body matrix.to links

Many Matrix clients (including Element) send mentions using HTML links
in formatted_body instead of or in addition to the m.mentions field:

```json
{
  "formatted_body": "<a href=\"https://matrix.to/#/@bot:matrix.org\">Bot</a>: hello",
  "m.mentions": null
}
```

This change adds detection for matrix.to links in formatted_body,
supporting both plain and URL-encoded user IDs.

Changes:
- Add checkFormattedBodyMention() helper function
- Check formatted_body in resolveMentions()
- Add comprehensive test coverage

Fixes openclaw#6982

* Update extensions/matrix/src/matrix/monitor/mentions.ts

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

---------

Co-authored-by: zerone0x <[email protected]>
Co-authored-by: Vincent Koc <[email protected]>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
vignesh07 pushed a commit to pahdo/openclaw that referenced this pull request Feb 20, 2026
…law#16941)

* fix(matrix): detect mentions in formatted_body matrix.to links

Many Matrix clients (including Element) send mentions using HTML links
in formatted_body instead of or in addition to the m.mentions field:

```json
{
  "formatted_body": "<a href=\"https://matrix.to/#/@bot:matrix.org\">Bot</a>: hello",
  "m.mentions": null
}
```

This change adds detection for matrix.to links in formatted_body,
supporting both plain and URL-encoded user IDs.

Changes:
- Add checkFormattedBodyMention() helper function
- Check formatted_body in resolveMentions()
- Add comprehensive test coverage

Fixes openclaw#6982

* Update extensions/matrix/src/matrix/monitor/mentions.ts

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

---------

Co-authored-by: zerone0x <[email protected]>
Co-authored-by: Vincent Koc <[email protected]>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: matrix Channel integration: matrix experienced-contributor Contributor with 10+ merged PRs size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Matrix: Mentions not detected when using formatted_body links (without m.mentions)

3 participants

Comments