Skip to main content
Level: Advanced · Tags: AI, Flows, Intent routing, WhatsApp, Telegram Features shown: 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_KEY environment variable (or another model provider, see below).

The bot

How it works

  • The model classifies, your code decides. classify() asks for one label with ctx.tools.ai.generate(), a call without chat history or persona. The answer is cleaned and checked against INTENTS; anything unexpected falls back to chat, 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 to await from 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. With autoReply: false the 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'); maxTokens includes the thinking tokens of reasoning models, so do not set it too low.

Try it

A run of the code above in the terminal simulator. The model (the classifier labels and the chit-chat answer) is scripted by the test that runs this page (packages/ai/tests/docs-examples-ai.test.ts); the flows are the real ones.

Other model providers

Change the aiPlugin options; effort is ignored by providers other than Anthropic.OpenAI (npm install openai, key in OPENAI_API_KEY):
Gemini (npm install @google/genai, key in GEMINI_API_KEY):
Ollama on your machine (no package, no key; run 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