Skip to main content
Level: Intermediate · Tags: WhatsApp, Telegram, Conversations Features shown: bot.conversation() · c.waitText() with timeout and validate · c.choose() · ConversationTimeout / ConversationCancelled · /cancel escapes a conversation

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:
If the user goes silent for two minutes, the bot answers ⏰ Support session expired. Send "support" to start again.

How it works

  • bot.conversation(name, async (c, ctx) => ...) is plain async code: it starts when a message matches name (or the trigger option), ctx is that message and c holds the waits. Use it when the logic does not fit a linear flow().
  • c.waitText({ timeout, validate }) waits for the next text of the same user. A rejected answer sends the validate text and waits again.
  • c.choose(prompt, options) sends a numbered menu and resolves with the chosen key.
  • Errors end the conversation. A silent user makes the wait throw ConversationTimeout; /cancel confirms the cancellation itself and makes the wait throw ConversationCancelled. Catch them to answer or clean up (here, discard the draft ticket); without a catch, a timeout sends the built-in text, which conversation.timeoutMessage customizes.
  • Commands always escape. /cancel and /reset beat an active conversation, so users are never trapped.

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 factory does not change.

Next steps