#7842: Fix Matrix mention detection for Element client (formatted_body links)
channel: matrix
stale
Cluster:
Model Reasoning Fixes
Fixes #7840
## Summary
Adds support for detecting mentions in `formatted_body` with `matrix.to` links, enabling mention detection from Element and other clients that don't use the newer `m.mentions` field.
## Problem
Element (the official Matrix client) doesn't send `m.mentions` in message events. Instead, it uses `formatted_body` with HTML links:
```html
<a href="https://matrix.to/#/@bot:server">@bot:server</a> hello
```
OpenClaw only checked `m.mentions`, causing it to miss all mentions from Element users.
## Solution
Added fallback logic to parse `formatted_body` and extract user IDs from `matrix.to` mention links:
```typescript
// Check formatted_body for matrix.to mention links (Element compatibility)
const formatted = content.formatted_body;
if (formatted && content.format === 'org.matrix.custom.html') {
const regex = /https?:\/\/matrix\.to\/#\/@([^"'<>\s]+)/g;
let match;
while ((match = regex.exec(formatted)) !== null) {
mentionedUserIds.add('@' + match[1]);
}
}
```
## Compatibility
- ✅ Works with Element (web, desktop, mobile)
- ✅ Maintains compatibility with clients using `m.mentions`
- ✅ Handles both old and new mention formats
## Testing
Tested with:
- Element Web sending mentions
- Direct mentions: `@bot:server test`
- Inline mentions: `hey @bot:server how are you`
- Multiple mentions in one message
## References
- Matrix spec: https://spec.matrix.org/v1.11/client-server-api/#mentions
- Element behavior: Uses `formatted_body` with HTML links
- OpenClaw issue: #7840
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates Matrix mention detection to support clients (notably Element) that don’t send `m.mentions` by also checking `formatted_body` for `matrix.to` mention links. The change is localized to `extensions/matrix/src/matrix/monitor/mentions.ts`, and the resolved `wasMentioned` flag now includes the formatted-body fallback in addition to room mentions, explicit user_ids, and text-pattern matching.
<h3>Confidence Score: 4/5</h3>
- This PR is generally safe to merge, but the formatted_body matching is strict enough that it may still miss common matrix.to mention link variants.
- Change is small and scoped, with low regression risk because it only adds an additional OR condition. Main concern is correctness coverage: exact-string `href` matching likely fails for URL-encoded fragments and links with query params, meaning the reported fix may be incomplete for some Element/server combinations.
- extensions/matrix/src/matrix/monitor/mentions.ts
<!-- greptile_other_comments_section -->
<sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#7845: Fix Matrix mention detection with URL-encoded user IDs
by emadomedher · 2026-02-03
91.6%
#20286: Matrix: render LaTeX for Element Desktop by emitting data-mx-maths ...
by eloklam · 2026-02-18
77.8%
#20278: Fix/matrix missing bot sdk dependency
by saurav470 · 2026-02-18
76.1%
#12028: fix: normalize // to / for Matrix client escaped commands
by githabideri · 2026-02-08
73.2%
#10721: fix for matrix media: destructure downloadContent return value in m...
by mklasen · 2026-02-06
72.9%
#18655: fix(mattermost): preserve markdown formatting and native tables
by echo931 · 2026-02-16
72.3%
#11774: fix: add guards for undefined mentionRegexes arrays
by ikvyk · 2026-02-08
72.1%
#10606: fix(matrix): keep room IDs without :server suffix as-is during reso...
by majorminors · 2026-02-06
71.9%
#19294: fix: normalize room ID case in Matrix config lookup
by MisterGuy420 · 2026-02-17
71.8%
#22389: Fix: Matrix plugin not sending images from content blocks
by fryccerGit · 2026-02-21
71.7%