DEV Community

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

Posted 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 80%+ of 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?

Real Results

After deploying this type of setup for businesses, we've consistently seen:

  • 70-80% reduction in response time (from hours to seconds)
  • 40-60% of queries handled fully automatically
  • 30% fewer missed messages (the bot never sleeps)
  • Significant time savings for business owners who previously managed everything manually

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

I've been building WhatsApp automation solutions for businesses for over 3 years. If you have questions about this setup, drop them in the comments — I'll answer everything.

Full guide with more details: WhatsApp Bot Complete Guide

Top comments (0)