Skip to content

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

@emadomedher

Description

@emadomedher

Bug Report: Matrix mentions not detected when using formatted_body links

Summary

Matrix mention detection fails when clients use the formatted_body with matrix.to links instead of (or without) the newer m.mentions field.

Environment

  • OpenClaw version: 2026.1.30
  • Matrix extension: bundled
  • Homeserver: Synapse (matrix.medher.online)
  • Client: Element Web

Steps to Reproduce

  1. Configure a Matrix room with requireMention: true
  2. In Element, mention the bot using the UI (click on username to create mention)
  3. Send the message
  4. Bot does not respond

Expected Behavior

Bot should detect the mention and respond.

Actual Behavior

Bot logs {"roomId":"...", "reason":"no-mention"} and ignores the message.

Root Cause Analysis

The resolveMentions() function in extensions/matrix/src/matrix/monitor/mentions.ts only checks:

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

However, many Matrix clients (including Element) send mentions using HTML in formatted_body:

{
  "body": "Myka ⚡: hello",
  "formatted_body": "<a href=\"https://matrix.to/#/@myka:matrix.medher.online\">Myka ⚡</a>: hello",
  "m.mentions": null
}

The function does NOT check formatted_body for matrix.to mention links.

Proposed Fix

Add a check for matrix.to links in formatted_body:

// Check formatted_body for matrix.to mention links (legacy/alternative mention format)
let mentionedInFormattedBody = false;
if (params.userId && params.content.formatted_body) {
  const escapedUserId = params.userId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  const matrixToPattern = new RegExp(
    `href=["']https://matrix\\.to/#/${escapedUserId}["']`,
    'i'
  );
  mentionedInFormattedBody = matrixToPattern.test(params.content.formatted_body);
}

Then include mentionedInFormattedBody in the wasMentioned check.

Files to Modify

  • extensions/matrix/src/matrix/monitor/mentions.ts

Additional Context

  • The m.mentions field is part of MSC3952 and may not be sent by all clients
  • Element Web appears to use formatted_body links without m.mentions
  • This affects any Matrix bot using OpenClaw with requireMention: true

Workaround

Set requireMention: false for the room to bypass mention detection entirely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleMarked as stale due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions