Skip to main content
Level: Intermediate · Tags: WhatsApp, Telegram, Plugins, Hooks Features shown: definePlugin() · before('message') hooks · ctx.abort() · ctx.onDispose() and unplug · plugin config

Prerequisites

Node.js 22.12 or newer and a folder with "type": "module" (see Getting started). Then install:

The complete bot

Sample transcript

Captured with npx botmesh dev bot.js; the fifth message is dropped silently, so the user is warned only once per window:

How it works

  • definePlugin({ name, install }) packages the feature; install gets the plugin context and the config passed to bot.plug(plugin, config). It must be synchronous.
  • ctx.bot.before('message', (msg, next) => ...) runs before routing. Call next() to continue; msg.abort() stops the message, so no handler, flow or catch-all sees it.
  • Per sender. msg.sender is the user’s stable id, so in a group each participant has their own counter.
  • Cleanup. Hooks and handlers registered through ctx.bot are undone by the function bot.plug() returns; timers and outside resources go in ctx.onDispose().
  • Outbound too. A before('send') hook sees every message the bot sends ({ chatId, content }) and can abort() it in the same way. For WhatsApp anti-ban pacing, @botmesh/baileys already rate-limits sends (see Rate limiting).

Run it on WhatsApp or Telegram

node bot.js prints a QR: scan it in WhatsApp → Linked devices. For Telegram, install @botmesh/telegram and swap the provider as shown in Or connect Telegram; the plugin works the same on every channel.

Next steps