# How I built a WhatsApp and Telegram AI support bot with Claude in under 400 lines

URL: https://shahid09.is-a.dev/engineering/whatsapp-ai-support-bot-claude-nodejs/
Author: Muhammad Shahid, Senior AI Software Engineer & Backend Developer (https://shahid09.is-a.dev/)
Published: 2026-10-31

**Quick answer:** The bot receives messages from the WhatsApp Cloud API webhook or Telegram long polling, sends them to Claude with the business's own documents in a cached system prompt, and lets the model call two tools: save_lead and handoff_to_human. It is plain Node.js with one dependency, under 400 lines, and open source under the MIT license.

- **Channels:** WhatsApp Cloud API and Telegram
- **AI:** Claude, through the official Anthropic SDK
- **Knowledge:** Markdown files, loaded into a cached system prompt
- **Actions:** save_lead and handoff_to_human tools
- **Size:** Node.js, one dependency, under 400 lines, MIT license

## What does the bot do?

Small businesses answer the same questions all day: opening hours, prices, bookings, policies. The bot answers those from files the business writes, saves a lead when someone wants to buy or book, and hands the chat to a person when the customer asks or the question is outside what it knows. The code is on GitHub: [ShahidKhan09/whatsapp-telegram-ai-bot](https://github.com/ShahidKhan09/whatsapp-telegram-ai-bot).

## How does the bot know about the business?

Everything in a `knowledge/` folder (Markdown or text) is read once at startup and placed in the system prompt, marked for prompt caching. For a typical small business, a few pages of FAQs and prices, this is simpler and more accurate than a vector database: the model sees all of it on every message, and caching makes resending it cheap. For a much larger knowledge base you would add a retrieval step first.

The instructions tell the model to answer only from that information, to say when it is not sure instead of guessing, and never to invent prices or promises.

## How do leads and human handover work?

Claude can call two tools, both declared with `strict: true` so their arguments always match the schema:

- `save_lead` with a name, a contact and what the customer wants. The bot appends it to a file and alerts the owner on Telegram.

- `handoff_to_human` with a reason. The bot alerts the owner and stays quiet in that chat for an hour, so the person can take over.

The loop is the standard tool-use loop: send the conversation, run any tool calls, send all the results back in one message, repeat until the model replies with text.

## Why is the chat history append-only?

Newer Claude models think before they answer, and their thinking blocks stay valid only while everything before them in the conversation is unchanged. So the bot never edits or trims old messages. When a chat gets long, it starts a fresh conversation instead; the customer does not notice, because the business knowledge is in every request anyway. The system prompt and tool list are also fixed at startup, which keeps the prompt cache warm.

## How are WhatsApp webhooks secured?

Meta signs every webhook with your app secret. The bot recomputes the HMAC-SHA256 of the raw body and compares it in constant time, rejecting anything that does not match:

```js
const expected = "sha256=" + createHmac("sha256", appSecret).update(rawBody).digest("hex")
const ok = header.length === expected.length &&
  timingSafeEqual(Buffer.from(header), Buffer.from(expected))
```

It also answers Meta immediately and sends the reply separately, and it ignores duplicate message IDs, because Meta can deliver the same message twice.

## Why Telegram as well as WhatsApp?

Telegram needs no business verification and works with long polling, so the bot runs on a laptop with no public URL. That makes it the fastest way to try the bot on a real phone before setting up WhatsApp.

## What would you add for production?

- Store conversations in Redis or a database, so a restart does not forget chats.

- Connect save_lead to a real CRM or booking system.

- Add a retrieval step if the knowledge base grows beyond a few hundred pages.

- Track answer quality on real conversations and adjust the knowledge files.

## Frequently asked questions

### Is the WhatsApp AI bot free?

The code is free and open source under the MIT license. You pay for the AI model usage and for WhatsApp conversations under Meta's pricing.

### Does it use the official WhatsApp API?

Yes. It uses the WhatsApp Business Platform Cloud API with signed webhooks, not an unofficial WhatsApp Web tool.

### Do I need a vector database?

Not for a small business knowledge base. The documents fit in a cached system prompt; add retrieval only when the knowledge base is very large.

### Can the bot reply in Hebrew or other languages?

Yes. It is instructed to reply in the customer's language.

### How does the bot hand a chat to a person?

It calls the handoff_to_human tool, which alerts the owner on Telegram and keeps the bot quiet in that chat for an hour.

### Can you build and run this for my business?

Yes. I set it up on your WhatsApp number, connect it to your CRM or booking system and tune it on your real questions. See the WhatsApp & Telegram AI bot service page.

See the related service: https://shahid09.is-a.dev/hire/whatsapp-telegram-ai-bot/
