WAPI provides comprehensive support for WhatsApp group chats. You can detect group messages, access group metadata, and leverage automatic caching for improved performance.
Group Detection
Identify group messages and filter by chat type
Group Metadata
Access group name, description, participants, and settings
Group JIDs always end with @g.us, while private chat JIDs end with @s.whatsapp.net or @lid. WAPI automatically sets chat.type based on the JID format (see message.ts:52-78).
bot.command("grouponly", async (ctx) => { if (ctx.chat.type !== "group") { await ctx.reply("This command only works in groups."); return; } await ctx.reply("This is a group command!");});
// Create a middleware to filter group messagesconst groupOnly = async (ctx, next) => { if (ctx.chat.type === "group") { await next(); }};// Apply to specific commandsbot.use(groupOnly);bot.command("announce", async (ctx) => { await ctx.reply("📢 Group announcement!");});
bot.command("private", async (ctx) => { if (ctx.chat.type !== "private") { await ctx.reply("This command only works in private chats."); return; } await ctx.reply("This is a private command!");});
The groupMetadata() method retrieves detailed information about a group:
import { isGroup } from "wapi-core/utils";bot.command("groupinfo", async (ctx) => { if (ctx.chat.type !== "group") { await ctx.reply("This command only works in groups."); return; } // Get group metadata const metadata = await ctx.bot.groupMetadata(ctx.chat.jid); if (!metadata) { await ctx.reply("Failed to fetch group information."); return; } const info = [ `*${metadata.subject}*`, ``, `👥 Participants: ${metadata.participants.length}`, `👑 Owner: ${metadata.owner}`, `📅 Created: ${new Date(metadata.creation * 1000).toLocaleDateString()}`, ``, metadata.desc ? `📝 Description:\n${metadata.desc}` : "No description" ].join("\n"); await ctx.reply(info);});
The groupMetadata() method (from bot.ts:332-342) first checks the cache before making a network request. If the metadata is cached, it returns instantly.
WAPI automatically caches group metadata for improved performance. The cache is managed in cache/groups.ts:
import { groups } from "wapi-core/cache";// The cache is a Map<string, GroupMetadata>// It's automatically populated when messages are receivedbot.use(async (ctx, next) => { if (ctx.chat.type === "group") { // Check if group is in cache if (groups.has(ctx.chat.jid)) { const cached = groups.get(ctx.chat.jid); console.log(`Cached group: ${cached.subject}`); } } await next();});
When a message is received from a group, WAPI automatically fetches and caches the metadata if it’s not already cached (see bot.ts:167-173). This means subsequent access is instant.
bot.use(async (ctx, next) => { if (ctx.chat.type !== "group") { await next(); return; } // Check for group participant changes // (You would need to track this via Baileys events) await next();});
2
Admin-Only Commands
Restrict commands to group administrators:
async function isAdmin(ctx): Promise<boolean> { if (ctx.chat.type !== "group") return false; const metadata = await ctx.bot.groupMetadata(ctx.chat.jid); if (!metadata) return false; const participant = metadata.participants.find( p => p.id === ctx.from.jid || p.id === ctx.from.pn ); return participant?.admin === "admin" || participant?.admin === "superadmin";}bot.command("kick", async (ctx) => { if (ctx.chat.type !== "group") { await ctx.reply("This command only works in groups."); return; } if (!(await isAdmin(ctx))) { await ctx.reply("This command is only for administrators."); return; } await ctx.reply("Admin command executed!");});
WAPI provides utility functions for working with group JIDs (from utils/jid.ts):
import { isGroup, decode, normalize } from "wapi-core/utils";bot.use(async (ctx, next) => { // Check if JID is a group if (isGroup(ctx.chat.jid)) { console.log("This is a group chat"); } // Decode JID to get components const decoded = decode(ctx.chat.jid); console.log(decoded.jid); // "123456789" console.log(decoded.server); // "g.us" // Normalize JID format const normalized = normalize(ctx.chat.jid); console.log(normalized); // "[email protected]" await next();});
WAPI automatically handles group updates via Baileys events (see bot.ts:222-237):
// This happens automatically in WAPI// When groups are updated, the cache is refreshedbot.on("error", (error) => { console.error("Bot error:", error);});// Monitor cache updatesimport { groups } from "wapi-core/cache";setInterval(() => { console.log(`Cached groups: ${groups.size}`);}, 60000); // Log every minute