How to Build an AI Chatbot for a Small Business Using n8n and OpenAI
A well-built AI chatbot can answer 80% of a small business's customer questions without human intervention — 24/7. In this tutorial, you'll build a complete AI chatbot using n8n and OpenAI that handles FAQs, captures leads, books appointments, and hands off to a human when needed.
This is one of the most in-demand services for local businesses — dental practices, law firms, gyms, restaurants, and home services companies all need this. You can charge $500–$2,000 to set it up and $150–$400/month to maintain it.
What You'll Build
- A webhook-powered chatbot backend in n8n
- Conversation memory using a Google Sheet or database
- FAQ answering trained on the business's actual content
- Lead capture with name, email, and phone collection
- Appointment booking trigger via Calendly link
- Human handoff when the bot can't answer
- Slack or email notification for the business owner
Architecture Overview
The chatbot works on a request-response model via webhooks. Your client's website sends a message to an n8n webhook → n8n processes it through OpenAI → n8n sends back the response → the website displays it.
The frontend widget can be a simple JavaScript snippet embedded on any website, or you can use a service like Tidio, Crisp, or Intercom with a custom webhook backend.
Step 1: Create the Webhook Trigger
Add a Webhook node as your trigger. Configure:
- HTTP Method: POST
- Path: chatbot (your URL will be:
https://your-n8n.com/webhook/chatbot) - Authentication: Header Auth with a secret key (prevents unauthorized calls)
- Response Mode: Last Node
The incoming payload should have: session_id, message, business_id.
Step 2: Load Conversation History
For the chatbot to remember context, you need to store and retrieve conversation history. Use a Google Sheets node to look up previous messages:
- Operation: Read Rows
- Filter:
session_idequals{{$json.session_id}} - Sort by: timestamp ascending
- Limit: last 10 messages (keeps token count manageable)
Your history sheet columns: session_id, role, content, timestamp, business_id.
Step 3: Build the System Prompt Dynamically
Add a Code node to build the system prompt from the business's FAQ data:
- Fetch the business config from a Google Sheet using
business_id - Include: business name, services, hours, pricing, location, common FAQs
- Include the lead capture instructions and handoff triggers
Example system prompt structure:
You are an AI assistant for [Business Name], a [type of business] in [location]. Your job is to answer customer questions, capture lead information, and book appointments. Business hours: [hours]. Services: [services]. Pricing: [pricing].
Lead capture rules: If the user asks about pricing, scheduling, or expresses interest in services, collect their name, email, and phone number before providing details. Ask for one piece of info at a time.
Handoff rule: If you cannot answer a question with certainty, say "Let me connect you with a team member" and set handoff: true in your JSON response.
Always respond with JSON: { "message": "your response", "handoff": false, "lead_captured": false, "lead_data": {} }
Step 4: Construct the Messages Array
Add a Code node to build the messages array for OpenAI:
const history = $('Load History').all().map(item => ({ role: item.json.role, content: item.json.content })); const messages = [ { role: 'system', content: systemPrompt }, ...history, { role: 'user', content: currentMessage } ]; return [{ json: { messages } }];
Step 5: Call OpenAI
Add an OpenAI node configured as:
- Resource: Chat
- Operation: Message a Model
- Model: gpt-4o-mini
- Messages: expression mode —
{{$json.messages}} - Temperature: 0.4 (consistent but not robotic)
- Max Tokens: 500
- Response Format: JSON Object
Step 6: Parse the Response
Add a Code node to parse the JSON response:
const response = JSON.parse($input.first().json.message.content); return [{ json: { message: response.message, handoff: response.handoff || false, lead_captured: response.lead_captured || false, lead_data: response.lead_data || {}, session_id: sessionId, business_id: businessId } }];
Step 7: Save Message to History
Add a Google Sheets node to save the conversation turn:
- Operation: Append Row
- Save both the user message and assistant response in separate rows
- Timestamp:
{{new Date().toISOString()}}
Step 8: Handle Lead Capture
Add an IF node checking if lead_captured === true. On the true branch:
- Add a HubSpot or Google Sheets node to save the lead
- Add a Gmail node to notify the business owner
- Subject:
New Lead Captured: {{$json.lead_data.name}}
Step 9: Handle Human Handoff
Add another IF node for handoff === true. On the true branch:
- Send a Slack message to the business owner with the conversation thread
- Send an SMS via Twilio for urgent cases
- Include the session_id so the human can pick up the conversation
Step 10: Return the Response
Add a Respond to Webhook node as the final step:
- Response Code: 200
- Response Data:
{ "message": "{{$json.message}}", "session_id": "{{$json.session_id}}" }
Deploying the Chat Widget
The simplest way to embed this on a client's site is to use a free chat widget like Tidio or Crisp with a custom webhook backend. Alternatively, build a minimal JavaScript chat widget that makes POST requests to your n8n webhook URL.
A minimal embed snippet looks like this:
<script src="https://your-cdn.com/chatbot.js" data-webhook="https://your-n8n.com/webhook/chatbot" data-business="biz123"></script>
Scaling Across Multiple Clients
The beauty of this architecture is that one workflow handles unlimited clients using the business_id parameter. Each business gets its own config row in your Google Sheet with its own FAQ data, system prompt, and routing rules. You don't need a separate workflow per client.
For more on n8n automation foundations, read our beginner's guide to building AI agents in n8n. And if you're choosing between platforms, see our breakdown of n8n vs Make vs Zapier for AI agents.
Want to learn how to build and sell AI automations? Join our free community. Join the free AI Agency Sprint community.
Join 215+ AI Agency Owners
Get free access to our LinkedIn automation tool, AI content templates, and a community of builders landing clients in days.
