open-wa v5 is alpha. Use v4.76.0 for mature production systems unless you are validating v5.
The Client APIAPI ExplorerLicensing

Group vs DM Handling

Detect group messages, filter by group ID, and exempt admins in your automation.

Filter Wally

Group vs DM Handling

When building automation, you often need different behavior for group messages versus direct messages. This guide covers detection, filtering, and admin exemptions.

Detecting Group Messages

By Chat ID Suffix

Group chat IDs end with @g.us. Direct messages end with @c.us.

client.onMessage(async (message) => {
  const isGroup = message.from.includes('@g.us');
  const isDirect = message.from.includes('@c.us');

  if (isGroup) {
    // Handle group message
  }

  if (isDirect) {
    // Handle direct message
  }
});

Using isGroupMsg

Some message objects include an isGroupMsg field:

client.onMessage(async (message) => {
  if (message.isGroupMsg) {
    // This is a group message
  }
});

Getting the Group ID

The group ID is the from field for group messages:

client.onMessage(async (message) => {
  if (message.from.includes('@g.us')) {
    const groupId = message.from; // e.g., "123456789-1111111@g.us"
    console.log('Message from group:', groupId);
  }
});

Configuring Which Groups to Process

Allowlist

Only process messages from specific groups:

const allowedGroups = new Set([
  '123456789-1111111@g.us',
  '987654321-2222222@g.us',
]);

client.onMessage(async (message) => {
  if (message.from.includes('@g.us') && !allowedGroups.has(message.from)) {
    return; // Skip groups not in allowlist
  }
  // Process the message
});

Blocklist

Skip specific groups:

const blockedGroups = new Set([
  '123456789-1111111@g.us',
]);

client.onMessage(async (message) => {
  if (blockedGroups.has(message.from)) {
    return; // Skip blocked groups
  }
  // Process the message
});

Exempting Admins

Getting Group Admins

const groupAdmins = await client.getGroupAdmins(groupId);

Checking if Sender is Admin

client.onMessage(async (message) => {
  if (!message.from.includes('@g.us')) return;

  const admins = await client.getGroupAdmins(message.from);
  const senderNumber = message.author?.split('@')[0];

  if (admins.includes(senderNumber)) {
    // Sender is an admin, exempt from moderation
    return;
  }

  // Process the message (e.g., moderate it)
});

Config-Based Admin Exemption

const exemptAdmins = new Set(config.adminIds || []);

client.onMessage(async (message) => {
  const senderId = message.author || message.from;
  if (exemptAdmins.has(senderId)) {
    return; // Admin messages are exempt
  }
  // Process the message
});

Group-Specific Behavior

Different Responses per Group

const groupConfig = {
  'group1@g.us': { language: 'en', moderation: true },
  'group2@g.us': { language: 'es', moderation: false },
};

client.onMessage(async (message) => {
  const config = groupConfig[message.from];
  if (!config) return; // Unknown group

  if (config.moderation) {
    // Apply moderation
  }
});

Group Welcome Messages

client.onMessage(async (message) => {
  if (message.type === 'gp2' && message.subtype === 'add') {
    const newMember = message.recipients?.[0];
    await client.sendText(message.from, `Welcome ${newMember}!`);
  }
});
Wally the Walrus typing

Was this helpful?

Wally and his cute companion coffee mug are coding day and night to keep this up-to-date!

On this page