From fbbe9e603048e6b75f71484f9c7d53ebd33aea8c Mon Sep 17 00:00:00 2001 From: Teknium Date: Sun, 22 Mar 2026 03:50:20 -0700 Subject: [PATCH] feat(gateway): add @-mention-only filter for Mattermost channels The Mattermost adapter now only responds to messages in channels and groups when the bot is @-mentioned. DMs are always processed without filtering. Detection checks both the bot's @username and user ID in the message text, providing a reliable fallback when the structured mentions field is unavailable. Fixes #2174 --- gateway/platforms/mattermost.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gateway/platforms/mattermost.py b/gateway/platforms/mattermost.py index ef1d5b838..a87018206 100644 --- a/gateway/platforms/mattermost.py +++ b/gateway/platforms/mattermost.py @@ -580,6 +580,24 @@ class MattermostAdapter(BasePlatformAdapter): # For DMs, user_id is sufficient. For channels, check for @mention. message_text = post.get("message", "") + # Mention-only mode: skip channel messages that don't @mention the bot. + # DMs (type "D") are always processed. + if channel_type_raw != "D": + mention_patterns = [ + f"@{self._bot_username}", + f"@{self._bot_user_id}", + ] + has_mention = any( + pattern.lower() in message_text.lower() + for pattern in mention_patterns + ) + if not has_mention: + logger.debug( + "Mattermost: skipping non-DM message without @mention (channel=%s)", + channel_id, + ) + return + # Resolve sender info. sender_id = post.get("user_id", "") sender_name = data.get("sender_name", "").lstrip("@") or sender_id