Skip to main content
Level: Intermediate · Tags: AI, Tools, WhatsApp, Telegram Features shown: AI tool calling · human confirmation (confirm: true) · confirmLines with prices from your code · dedupe of repeated side effects · enum validation of arguments · Yes/No buttons · audit events

Prerequisites

  • Node.js 22.12 or newer.
  • An Anthropic API key in the ANTHROPIC_API_KEY environment variable (or another model with tool calling, see below).

The bot

How it works

  • Tools are plain functions. Each entry of tools has a description the model reads, an optional JSON schema and a handler. The plugin runs the tool loop: it calls the model, runs the tools it asks for and sends the results back until the model writes the answer (maxSteps bounds it).
  • Confirmation before side effects. confirm: true makes the plugin ask the customer and run the tool only after a clear “yes”. The question is asked inside a conversation, so the chat is never blocked while waiting. On channels with buttons (Telegram, WhatsApp Cloud API, the simulator) it shows Yes/No buttons; on WhatsApp through Baileys it asks for a typed yes/no.
  • Your code owns the numbers. confirmLines prints the service and the price from SERVICES, and the handler returns them too, so the model repeats them instead of calculating.
  • No double bookings. dedupe: 'args' skips a call with the same arguments as a booking made in the last 10 minutes; the model is told it was already done. A handler that returns { error } did nothing and is not remembered.
  • Every create has its list and cancel. Without my_appointments and cancel_appointment a model “fixes” a booking by making another one.
  • Audit. Each step of the loop is an 'ai.tool' event; the example logs the runs.

Try it

A run of the code above in the terminal simulator. The model is scripted by the test that runs this page (packages/ai/tests/docs-examples-ai.test.ts); the confirmations, buttons and tool results are the real ones. In the simulator you can also answer a confirmation with :click 1. In the fourth message the model asks for the same booking again: dedupe skips it without asking, and the declined cancellation leaves the appointment as it was.

Other model providers

Tool calling works on every provider; on Ollama pick a model tagged tools.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 qwen3 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 ?? '' }): the confirmation then uses inline Yes/No buttons, which are removed once answered.

Next steps