How to Build an AI Chatbot for Your Website: A Step-by-Step Tutorial for Businesses
From choosing the chatbot’s job to writing the system prompt, connecting your knowledge base, capturing leads, handing over to humans and measuring results — a practical tutorial with a working backend example.

Table of contents
Step 1: Give the chatbot one clear job
Chatbots fail when they try to do everything. Choose the main job first, because it decides the data, prompts and success metric:
- Answer questions: product, pricing, policies — success is fewer support emails.
- Capture leads: qualify visitors and collect contact details — success is booked calls.
- Take actions: check order status, book appointments — success is tasks completed without staff.

Step 2: Prepare the knowledge base
The chatbot can only be as good as the information you give it. Collect your FAQs, service pages, pricing rules, policies and past support replies. Clean them: remove outdated content, resolve contradictions and write short, factual answers for the top 30 questions customers ask.
For larger content, use retrieval-augmented generation (RAG): split documents into chunks, store them with embeddings in a vector database, and at question time retrieve the most relevant chunks and include them in the prompt.
Step 3: Write the system prompt
The system prompt is the chatbot’s job description. A good one covers role, scope, tone and boundaries:
You are the website assistant for Acme Dental Clinic in Indore.
Answer only using the CONTEXT provided. If the answer is not in the
context, say you are not sure and offer to connect the visitor to staff.
Keep replies under 80 words, friendly and clear. Reply in the visitor's
language (English or Hindi).
Never give medical diagnoses. For pain or emergencies, share the clinic
phone number immediately.
When a visitor wants an appointment, collect name, phone and preferred
time, then call the book_appointment tool.Step 4: Build the backend
Never call an AI API directly from the browser — your API key would be exposed. The website widget sends messages to your own backend, which adds context, calls the model and returns the reply.
app.post("/api/chat", rateLimit({ windowMs: 60000, max: 20 }), async (req, res) => {
const { sessionId, message } = req.body
if (!message || message.length > 1000) return res.status(400).end()
const history = await sessions.get(sessionId) // last few turns
const context = await knowledgeBase.search(message, 5) // RAG retrieval
const reply = await llm.chat({
system: SYSTEM_PROMPT + "\n\nCONTEXT:\n" + context.join("\n---\n"),
messages: [...history, { role: "user", content: message }],
maxTokens: 300,
})
await sessions.append(sessionId, message, reply.text)
res.json({ reply: reply.text })
})Tip: Add rate limiting and a maximum message length from day one. Public chatbots attract spam and prompt-injection attempts.
Step 5: Lead capture and human hand-off

- 1
Detect intent: when a visitor asks about pricing, demos or bookings, the bot asks for name and phone or email.
- 2
Save to CRM: push the lead and the conversation summary to your CRM or Google Sheet automatically.
- 3
Notify instantly: send the sales team a WhatsApp or Slack alert so a human can follow up within minutes.
- 4
Offer a human: a visible “Talk to a person” option at any time builds trust.
Step 6: Test, launch and improve
“Most chatbot improvements come from better knowledge, not a better model. Read the transcripts every week.”
- Test set: write 50 real questions (including tricky and off-topic ones) and check answers before launch.
- Review weekly: read conversations where the bot said “I’m not sure” and add missing knowledge.
- Measure: resolution rate, leads captured, hand-off rate and visitor satisfaction (thumbs up/down).
- Privacy: show a short notice, avoid collecting sensitive data in chat, and set a retention period.
Topics
Umer Hasan
Co-Founder & CTO at Zapprex
Leads engineering, cloud infrastructure and AI automation at Zapprex Technologies, with 7+ years of full-stack and mobile experience.
Keep reading
All articlesHow AI Voice Agents Work: Speech-to-Text, LLMs, Text-to-Speech and Latency Explained
Learn the architecture behind AI phone agents — telephony, streaming speech recognition, the language model, voice synthesis, interruptions and latency budgets — plus where voice agents work well in Indian and Gulf businesses.
Voice AI Integration Tutorial: Connect an AI Voice Agent to Your Phone Line (Twilio, Exotel, Plivo)
Hands-on tutorial: route a phone number to your server, stream call audio over WebSockets, connect speech-to-text, an LLM and text-to-speech, handle interruptions, and hand calls to humans — with Node.js code.
How WhatsApp Business Chatbots Drive 3x More Conversions & Automate Support in 2026
With open rates exceeding 98%, WhatsApp has evolved into the highest-converting customer channel. Discover how official WhatsApp Cloud API chatbots automate lead qualification, sales, and 24/7 support.
Questions readers ask
A focused FAQ and lead-capture chatbot can be live in 2–4 weeks. Chatbots that take actions in your systems, such as bookings or order lookups, typically take 4–8 weeks.
It can if it is not grounded. Restricting it to retrieved context, instructing it to admit uncertainty and testing against real questions keeps wrong answers rare.
Yes. The backend logic and knowledge base can be shared; only the channel changes. WhatsApp requires the official WhatsApp Business Platform.
Have a project in mind?
Let's build something that moves your business forward.
Share your idea, process or problem — we will come back with a clear plan, timeline and cost range. No technical spec needed.
- Founder reply within 1 business day
- NDA on request · 100% code ownership
- Talk directly to the engineers
