Skip to content

Feature Request: Group Chat Mention Detection #13

@adam91holt

Description

@adam91holt

Summary

Currently warelay responds to every message. For group chats, it should only respond when explicitly mentioned.

Proposed Config

{
  "inbound": {
    "groupChat": {
      "mentionPatterns": [
        "@mybot",
        "\\bmybot\\b"
      ],
      "requireMention": true,
      "historyLimit": 50
    }
  }
}

Implementation

In monitorWebProvider, track group history and check for mentions:

// Track message history per group for context when mentioned
// Only stores messages SINCE the last bot response to avoid duplication
const groupHistorySinceLastReply = new Map<string, Array<{
  sender: string;
  body: string;
  timestamp: number;
}>>();
const MAX_HISTORY_PER_GROUP = 50;

// Mention patterns from config
const mentionPatterns = (cfg.inbound?.groupChat?.mentionPatterns ?? [])
  .map(p => new RegExp(p, 'i'));

In onMessage handler:

const isGroupChat = msg.from.endsWith("@g.us");
if (isGroupChat) {
  if (!groupHistorySinceLastReply.has(msg.from)) {
    groupHistorySinceLastReply.set(msg.from, []);
  }
  const history = groupHistorySinceLastReply.get(msg.from)!;

  history.push({
    sender: msg.senderName || "Unknown",
    body: msg.body,
    timestamp: msg.timestamp || Date.now(),
  });

  while (history.length > MAX_HISTORY_PER_GROUP) {
    history.shift();
  }

  const wasMentioned = mentionPatterns.some(pattern => pattern.test(msg.body));
  if (!wasMentioned) {
    logVerbose(`Group message not mentioning bot, storing for context only`);
    return;
  }
}

When responding, include history as context:

if (isGroupChat && groupHistorySinceLastReply.has(msg.from)) {
  const history = groupHistorySinceLastReply.get(msg.from)!;
  const contextMessages = history.slice(0, -1);
  if (contextMessages.length > 0) {
    const historyText = contextMessages
      .map(m => `${m.sender}: ${m.body}`)
      .join("\n");
    bodyForCommand = `[Chat messages since your last reply - for context]\n${historyText}\n\n[Current message - respond to this]\n${senderPrefix}${msg.body}`;
  }
  groupHistorySinceLastReply.set(msg.from, []);
}

Skip heartbeats for group chats:

if (lastInboundMsg.from.endsWith("@g.us")) {
  console.log(success("heartbeat: skipped (group chat)"));
  return;
}

Use Case

Running an AI assistant in group chats where multiple humans are conversing - the bot should only chime in when directly addressed, not on every message. When mentioned, it has context of recent messages to understand the conversation.

Happy to contribute a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions