Conversational AI 11 min read

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.

Umer Hasan

Umer Hasan

Co-Founder & CTO at Zapprex

How to Build an AI Chatbot for Your Website: A Step-by-Step Tutorial for Businesses
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.
At a glance: Step 1: Give the chatbot one clear job — Answer questions, Capture leads, Take actions
At a glance: Step 1: Give the chatbot one clear job

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:

Example system prompttext
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.

Minimal Node.js (Express) chat endpoint — provider-agnosticjavascript
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

Step 5: Lead capture and human hand-off: 4 steps
  1. 1

    Detect intent: when a visitor asks about pricing, demos or bookings, the bot asks for name and phone or email.

  2. 2

    Save to CRM: push the lead and the conversation summary to your CRM or Google Sheet automatically.

  3. 3

    Notify instantly: send the sales team a WhatsApp or Slack alert so a human can follow up within minutes.

  4. 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.”

— Umer Hasan, CTO
  • 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

AI ChatbotWebsite ChatbotRAGLead Generation
Umer Hasan
Written by

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.

FAQ

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.

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