Skip to content

Filters & Events

Event Filters

Use wa.on() with a filter string to handle specific event types:

ts
// Any message
wa.on("message", async (ctx) => { /* ... */ });

// Text messages only
wa.on("message:text", async (ctx) => {
  console.log(ctx.text); // string, guaranteed
});

// Image messages
wa.on("message:image", async (ctx) => {
  console.log(ctx.message.mimetype);
});

Available Filters

FilterEvent
messageAny message
message:textText messages
message:imageImage messages
message:videoVideo messages
message:audioAudio messages
message:documentDocument messages
message:stickerSticker messages
message:contactContact cards
message:locationLocation shares
message_reactionEmoji reactions
qrQR code for auth
connectedConnected to WhatsApp
disconnectedDisconnected
receiptDelivery/read receipts
presenceOnline/typing status
group_joinBot joined a group
group_leaveNot yet implemented
group_updateGroup name or topic changed

Multiple Filters

Pass an array to match multiple event types:

ts
wa.on(["message:image", "message:video"], async (ctx) => {
  // handles both image and video messages
});

Filter Constants

Use the filters object for autocomplete:

ts
import { filters } from "@arnabxd/whatspurr";

wa.on(filters.text, async (ctx) => {
  await ctx.reply(ctx.text);
});

Pattern Matching

Use hears() to match text messages against a regex:

ts
wa.hears(/^hello/i, async (ctx) => {
  console.log(ctx.match); // RegExp match result
  await ctx.reply("Hi there!");
});

Commands

Use command() to handle /command messages:

ts
wa.command("start", async (ctx) => {
  await ctx.reply("Welcome!");
});

wa.command("echo", async (ctx) => {
  // /echo hello world → ctx.commandArgs = "hello world"
  await ctx.reply(ctx.commandArgs ?? "No arguments");
});

Custom Filters

Use filter() with a predicate function:

ts
// Only handle group messages
wa.filter(
  (ctx) => ctx.isGroup,
  async (ctx) => {
    await ctx.reply("This is a group!");
  }
);

Released under the GPL-3.0 License.