DEV Community

Cover image for Building a WhatsApp Customer Service Bot with n8n: A Step-by-Step Guide
אחיה כהן
אחיה כהן

Posted on • Edited on • Originally published at achiya-automation.com

Building a WhatsApp Customer Service Bot with n8n: A Step-by-Step Guide

WhatsApp handles over 100 billion messages daily, and in markets like Israel, Brazil, and India, it's the primary business communication channel. If your customers are already on WhatsApp, automating responses there isn't optional — it's expected.

In this guide, I'll walk you through building a production-ready WhatsApp customer service bot using n8n (open-source workflow automation) and the WhatsApp Business API.

Why n8n for WhatsApp Automation?

Feature n8n Zapier Make
Self-hosted option
WhatsApp integration ✅ Native ⚠️ Limited ⚠️ Limited
Pricing (self-hosted) Free $20+/mo $9+/mo
Custom code nodes ✅ JavaScript ⚠️ Limited
Webhook support ✅ Built-in
AI/LLM integration ✅ Native ⚠️ ⚠️

n8n gives you full control — self-host it, connect any API, and add custom logic without vendor lock-in.

Architecture Overview

Customer (WhatsApp) → WhatsApp Business API → Webhook → n8n Workflow
                                                              ↓
                                                    [Route by Intent]
                                                     /      |       \
                                               FAQ Bot   Human Agent   AI Response
                                                     \      |       /
                                                    WhatsApp API → Customer
Enter fullscreen mode Exit fullscreen mode

Prerequisites

  • n8n instance (self-hosted or cloud) — n8n.io
  • WhatsApp Business API access (via Meta, or unofficial solutions like Evolution API / WAHA)
  • Basic understanding of webhooks and REST APIs

Step 1: Set Up the WhatsApp Webhook

In n8n, create a new workflow and add a Webhook node:

{
  "node": "Webhook",
  "config": {
    "httpMethod": "POST",
    "path": "whatsapp-bot",
    "responseMode": "lastNode"
  }
}
Enter fullscreen mode Exit fullscreen mode

This creates an endpoint like https://your-n8n.com/webhook/whatsapp-bot that WhatsApp will send incoming messages to.

Step 2: Parse the Incoming Message

Add a Function node to extract the relevant data:

const body = $input.first().json.body;

// Extract message details (Meta Cloud API format)
const entry = body.entry?.[0];
const change = entry?.changes?.[0];
const message = change?.value?.messages?.[0];

if (!message) {
  return [{ json: { skip: true } }];
}

return [{
  json: {
    from: message.from,           // sender phone number
    text: message.text?.body || '',
    type: message.type,           // text, image, document, etc.
    timestamp: message.timestamp,
    messageId: message.id
  }
}];
Enter fullscreen mode Exit fullscreen mode

Step 3: Route by Intent

Use a Switch node to route messages based on content:

// Simple keyword-based routing
const text = $input.first().json.text.toLowerCase();

if (text.includes('price') || text.includes('cost')) {
  return 'pricing';
} else if (text.includes('hours') || text.includes('open')) {
  return 'hours';
} else if (text.includes('human') || text.includes('agent')) {
  return 'human';
} else {
  return 'ai'; // fallback to AI response
}
Enter fullscreen mode Exit fullscreen mode

Step 4: AI-Powered Responses (Optional but Powerful)

Connect an OpenAI or Anthropic node for intelligent responses:

// System prompt for the AI
const systemPrompt = `You are a helpful customer service assistant.
You answer questions about our products, services, pricing, and hours.
Always be friendly and concise. If you don't know the answer,
offer to connect the customer with a human agent.
Respond in the same language the customer writes in.`;
Enter fullscreen mode Exit fullscreen mode

This gives you a bot that can handle common customer queries automatically, in any language.

Step 5: Send the Response

Add an HTTP Request node to send the reply via WhatsApp API:

// Meta Cloud API format
const response = {
  messaging_product: "whatsapp",
  to: $input.first().json.from,
  type: "text",
  text: {
    body: $input.first().json.aiResponse
  }
};

// POST to https://graph.facebook.com/v18.0/{PHONE_ID}/messages
Enter fullscreen mode Exit fullscreen mode

Step 6: Add Business Hours Logic

Don't let your bot respond 24/7 without context. Add a time check:

const now = new Date();
const hour = now.getHours();
const isBusinessHours = hour >= 9 && hour < 18;
const isWeekend = now.getDay() === 6; // Saturday

if (!isBusinessHours || isWeekend) {
  return [{
    json: {
      response: "Thanks for your message! We're currently closed. " +
                "We'll get back to you on the next business day."
    }
  }];
}
Enter fullscreen mode Exit fullscreen mode

Production Checklist

Before going live, make sure you have:

  • [ ] Rate limiting — Don't send more than 1 message/second per number
  • [ ] Error handling — What happens when the API is down?
  • [ ] Logging — Store all conversations for quality control
  • [ ] Human handoff — Clear path to a real person
  • [ ] Opt-out — Respect "STOP" messages immediately
  • [ ] Template messages — For proactive outreach (required by Meta)
  • [ ] Media handling — What happens when someone sends a photo?

What This Gets You

The biggest win is instant response time — every customer gets an acknowledgment in seconds, even outside business hours. The bot handles the routine stuff (pricing, hours, directions), and anything complex gets escalated to a human with full context.

The practical benefit for the business owner: you stop being a bottleneck. Customers don't wait, and you don't spend your morning catching up on overnight messages.

What's Next?

Once your basic bot is running, consider adding:

  1. CRM integration — Log conversations in your CRM (HubSpot, Salesforce, etc.)
  2. Appointment booking — Let customers book directly via WhatsApp
  3. Order tracking — Connect to your e-commerce platform
  4. Multilingual support — Auto-detect language and respond accordingly
  5. Analytics dashboard — Track response times, satisfaction, and common queries

Full guide with more details: WhatsApp Bot Complete Guide


Over to You

Here's the question I still debate with myself: what's the ideal handoff point from bot to human?

I've experimented with three approaches:

  • Confidence scores (< 0.7 → human)
  • Keyword triggers ("speak to agent")
  • Conversation turn limits (> 3 turns → human)

None feel perfect. Confidence scores miss nuance. Keywords feel clunky. Turn limits are arbitrary.

What trigger do you use for bot→human handoff? Or do you let the bot handle everything and only escalate on explicit request? I'd especially love to hear from anyone handling 100+ conversations/day. 👇

Top comments (0)