From 2e8b3b903bc79e471cdc0f246d70cf52557545b1 Mon Sep 17 00:00:00 2001 From: Till-JS <101404291+Till-JS@users.noreply.github.com> Date: Sat, 14 Feb 2026 13:40:59 +0100 Subject: [PATCH] fix(matrix-bot-common): ignore messages from other bots Prevents infinite message loops when multiple bots are in the same room. Checks if sender has '-bot' in their Matrix user ID localpart. Co-Authored-By: Claude Opus 4.5 --- .../src/base/base-matrix.service.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/matrix-bot-common/src/base/base-matrix.service.ts b/packages/matrix-bot-common/src/base/base-matrix.service.ts index cf82d57ac..40e12806a 100644 --- a/packages/matrix-bot-common/src/base/base-matrix.service.ts +++ b/packages/matrix-bot-common/src/base/base-matrix.service.ts @@ -158,6 +158,18 @@ export abstract class BaseMatrixService implements OnModuleInit, OnModuleDestroy } } + /** + * Check if a sender is a bot (has "-bot" in the localpart) + * Bots should not respond to each other to avoid infinite loops + */ + protected isBot(sender: string): boolean { + // Extract localpart from @user:server format + const match = sender.match(/^@([^:]+):/); + if (!match) return false; + const localpart = match[1].toLowerCase(); + return localpart.includes('-bot') || localpart.endsWith('bot'); + } + /** * Handle incoming room message */ @@ -165,6 +177,9 @@ export abstract class BaseMatrixService implements OnModuleInit, OnModuleDestroy // Ignore own messages if (event.sender === this.botUserId) return; + // Ignore messages from other bots to prevent infinite loops + if (this.isBot(event.sender)) return; + // Check room permissions if (this.allowedRooms.length > 0 && !this.allowedRooms.includes(roomId)) { return;