ctx.tools.ai.generate() ·
replyInChat() with chat history ·
flows with validation and menus ·
starting a flow from code (bot.trigger) ·
catch-all handler ·
cost and latency settings
Prerequisites
- Node.js 22.12 or newer.
- An Anthropic API key in the
ANTHROPIC_API_KEYenvironment variable (or another model provider, see below).
The bot
How it works
- The model classifies, your code decides.
classify()asks for one label withctx.tools.ai.generate(), a call without chat history or persona. The answer is cleaned and checked againstINTENTS; anything unexpected falls back tochat, so a confused model can never start a flow you did not list. - Money stays deterministic. The payment flow validates the account number, offers a fixed menu and builds the confirmation in code. The model never sees the account number or decides the amount.
bot.trigger()starts the flow. It is safe toawaitfrom a handler of the same chat: it runs in that message’s turn. The flow then owns the next messages, exactly as if the user had typed its name.- Chit-chat keeps a history.
replyInChat(chatKeyOf(ctx), text)reads and stores the chat’s AI history and uses the plugin’s persona. WithautoReply: falsethe plugin does not add its own catch-all. - Never silent. A model error (no key, timeout, rate limit, outage) is logged and the customer gets a fixed answer that points to the flows, which still work without the AI. Messages with no text (a photo, a voice note, a location) get a fixed answer too, without a model call.
- Cost. Keywords (“payment”, “order”) and slash commands skip the
classifier. The classifier call is short (
maxTokens: 100,effort: 'low');maxTokensincludes the thinking tokens of reasoning models, so do not set it too low.
Try it
packages/ai/tests/docs-examples-ai.test.ts); the flows are the real ones.
Other model providers
Switch to OpenAI, Gemini or a local Ollama model
Switch to OpenAI, Gemini or a local Ollama model
Change the Gemini (Ollama on your machine (no package, no key; run
aiPlugin options; effort is ignored by providers other than
Anthropic.OpenAI (npm install openai, key in OPENAI_API_KEY):npm install @google/genai, key in GEMINI_API_KEY):ollama pull llama3.1 first):Run it on WhatsApp or Telegram
node bot.js prints a QR code: scan it with WhatsApp (Linked devices).
For Telegram, install @botmesh/telegram and start the factory with
new TelegramProvider({ token: process.env.TELEGRAM_TOKEN ?? '' }).
Next steps
- Conversation flows: every step of the flow DSL
- AI with human escalation: let the model pass the chat to a person
- Examples overview