Skip to main content
Level: Beginner · Tags: WhatsApp, Telegram, Flows, Forms Features shown: flow() DSL · .ask() with validate · .do() actions · c.state · .timeout() · /cancel escapes a flow

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:

How it works

  • .ask(text, key, { validate }) sends the question, waits for the answer and stores it in c.state[key]. validate returns true to accept, or the text to send before waiting again.
  • .do((ctx, c) => ...) runs code without sending anything: here it saves the user under ctx.sender, their stable id on every channel.
  • .say((ctx, c) => ...) builds the summary from everything captured.
  • .timeout('5m') ends a flow the user abandoned; the bot-wide conversation.timeoutMessage sets what is sent then.
  • Commands win. /cancel (from slashCommandsPlugin) leaves the flow even while it waits for an answer.

Variations

Validate with a schema instead: schema takes any zod-compatible object, and the parsed value lands in c.state (a number below):
To keep the data across restarts, store it with persistent user data instead of a Map. For several fields in one native form on the WhatsApp Cloud API, see Forms.

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