← Back to projects
Project 12/22/2025

Intelligent "Human-after-the-loop" Customer Service Bot with n8n for Chatwoot

Working at JualSFP, I noticed a pattern. Our sales team was acting less like consultants and more like human search engines. Their days were dominated by the same three questions: "Do you have stock?", "Is this compatible with Cisco?", and "How much for 10 units?"

I love our customers, but I hated seeing the team become a bottleneck. When they were busy with other tasks, customers waited. And in the world of networking hardware, speed is everything.

I realized we didn't need a generic chatbot that just says "Please wait for an agent." We needed an assistant that could actually do the work—check real inventory, calculate prices, and answer technical specs but know exactly when to step back and let a human close the deal.

Here’s how I built it using n8n, Chatwoot, and a little bit of "Human-after-the-loop" logic.

The Problem: "Is this compatible?"

Our catalog has hundreds of SFP transceivers. SFP compatibility is a huge mess. A module that works on a MikroTik switch might not work on a Cisco Nexus.

My goal was to build a system that could:

  • Understand fuzzy inputs (e.g., "SFP 10G LC 10km" instead of a part number).
  • Check real-time stock from our Google Sheets inventory (because no one wants to update a custom admin panel just for this).
  • Answer deep technical questions using our database.
  • Hand off to the sales team when money is on the table.

The Stack

I chose a low-code/no-code approach to iterate fast and prove the concept.

  • Brain: n8n (self-hosted). This is where all the logic lives.
  • Face: Chatwoot (self-hosted). The best open-source customer engagement platform.
  • Input Channel: WhatsApp Cloud API.
  • Memory: Supabase. To handle message queues and session state.
  • Knowledge: Google Sheets & DigitalOcean AI.

The "Aha!" Moment: Handling Message Bursts

One of the first walls I hit was how people actually text. They don't send one perfect paragraph. They send:

"Hi"
"Bro"
"Stock for GLC-T?"
"Need 5"

My first bot iteration fired 4 separate AI responses. It was a disaster. The "Hi" triggered a generic greeting, while the actual "Stock" query got lost in the noise.

The Solution: The 7.5-Second Pause

I built a "buffer" system in n8n. When a message comes in, I push it to Supabase and wait. I literally put a Wait node for 7.5 seconds. After the wait, I pull all messages from that user in the last window, aggregate them into one block of text, and then send it to the AI.

Now, the bot sees: "Hi Bro Stock for GLC-T? Need 5" and responds perfectly.

Core Architecture: The Logic Flow

The workflow isn't a straight line; it's a decision tree. Here is the exact logic path for every message:

The Gatekeeper (Inbox Routing)

When a webhook hits, we first identify the source. Using a Switch Node, we route the payload based on inbox_id. This allows us to test new features in a sandbox inbox without affecting the live WhatsApp flow.

The "Thought Collector" (Message Aggregation)

We don't process messages one-by-one:

  1. Log & Wait: Messages go to a Supabase message_queue. The workflow freezes for 7.5 seconds.
  2. The Race Condition Check: If a newer execution starts for this user, the current one terminates. Only the last one survives.
  3. Fetch & Merge: The surviving execution pulls all user messages, merges them into one prompt, and clears the queue.

This turns a spammy WhatsApp user into a single, coherent query for the AI.

The Context Builder (Session Check)

We check a user_sessions table to build context:

  • New User? Create session and send the Welcome Menu.
  • Inactive > 24h? Re-engage by treating as a new session.
  • Active Session? Forward to the AI with existing message memory.

The AI Agent (The Brain)

The AI is configured with a specific persona: "Bot Customer Service JualSFP". I gave it access to two powerful tools:

The Pricing Tool (Google Sheets)

I connected the AI directly to our inventory spreadsheet. I gave it a tool called Stock_Price. But before looking up data, I implemented a Normalization Layer. Customers use all kinds of slang:

  • "SFP" becomes "1G"
  • "SFP+" becomes "10G"
  • "bidi" becomes "bx"
  • "rj45" becomes "base-t"

This pre-processing ensures that a search for "rj45 10g" correctly maps to our "10G BASE-T" products.

The Tech Expert (DigitalOcean Agent Platform)

For the hard questions, I hooked up a secondary AI model using DigitalOcean's Agent Platform. It leverages a knowledge base that indexes our technical documentation directly from a DigitalOcean Space (S3). It isn't just an LLM guessing; it retrieves answer chunks from our actual PDF manuals before generating a response.

The "Human-after-the-loop" Philosophy

I didn't want the AI to replace the sales team; I wanted it to empower them.

Session Management

We implemented a strict 24-hour window:

  • Active: Bot handles chat.
  • Expired: User chooses "Chat with Bot" or "Chat with Support Team".

Seamless Handover

The AI hands over the baton if:

  • The user intends to "Order" or "Checkout".
  • The user tries to sell equipment to us.
  • Negative sentiment is detected.

When this happens, the conversation is assigned to Team 1 (JualSFP Support) in Chatwoot. A 24-hour countdown starts where the bot completely shuts up, ensuring it doesn't interrupt the human agent. After 24 hours of silence, the "lock" is released, and the bot takes over again.

Under the Hood: Engineering for Scale

To make this resilient, we built it with Layered Architecture:

  • Routing Layer: Differentiates customer chats from system notifications and agent replies to prevent loops.
  • Batching Layer: High-speed buffering in Supabase with Concurrency Control.
  • Resilience Layer: Sentiment guardrails and "auto-healing" synonym maps for better data retrieval.

Conclusion

It’s been running for a few weeks now, and it’s changed how the team works. We only step in when it matters—when there's a deal to close or a complex problem to solve.

This project proves that with n8n, Chatwoot, and Supabase, a small team can build enterprise-grade AI automation without an enterprise budget.

If you're working at a small business, don't just look for a chatbot. Look for an Agent. Build something that understands your data, your slang, and your workflow. It’s easier than you think.