DEV Community

Cover image for How I Built a WhatsApp AI Assistant That Answers Questions From a Live Database
Hussain Mirza
Hussain Mirza

Posted on

How I Built a WhatsApp AI Assistant That Answers Questions From a Live Database

I recently built a system that lets an entire organization query their live database through WhatsApp — no app, no dashboard, no training required. Just type a question and get an answer instantly.

Here's exactly how I built it, what tools I used, and what I learned.


The Problem

Fayz e Husayni is a community organization in Karachi that manages ticketing, finance, properties, and IT systems across multiple departments. Their data was scattered across spreadsheets and manual processes. Team members had to contact specific people just to get basic information like "how many confirmed tickets are there?" or "which properties need maintenance?"

The goal: let any authorized member ask questions in plain English through WhatsApp and get instant answers from live data.


The Solution

A WhatsApp AI bot built on:

  • n8n — workflow automation and middleware
  • WAHA — self-hosted WhatsApp HTTP API
  • Groq (LLaMA 3.1) — free AI inference
  • Google Sheets — mock database
  • Docker — container orchestration

How It Works

Member sends message in WhatsApp group
        ↓
WAHA captures the message via webhook
        ↓
n8n receives it and filters by group ID
        ↓
Fetches live data from all 4 department sheets
        ↓
Is it a report request? Yes → format report
                         No → send to Groq AI with data context
        ↓
Response sent back to WhatsApp group
Enter fullscreen mode Exit fullscreen mode

Step 1 — Setting Up WAHA and n8n with Docker

Both services run as Docker containers. The docker-compose.yml is straightforward:

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n
    ports:
      - "5678:5678"
  waha:
    image: devlikeapro/waha
    ports:
      - "3000:3000"
Enter fullscreen mode Exit fullscreen mode

WAHA gives you a dashboard at localhost:3000/dashboard where you scan a QR code to connect your WhatsApp number. Once connected, it fires a webhook on every incoming message.


Step 2 — Receiving Messages in n8n

In n8n, a Webhook node listens at /webhook/whatsapp-incoming. WAHA posts every incoming message to this URL.

The first thing I do is filter by group ID — the bot should only respond to messages from the designated Fayz group, not personal chats:

IF $json.body.payload.from == "120363428000042923@g.us"
Enter fullscreen mode Exit fullscreen mode

This single filter keeps personal conversations completely private.


Step 3 — Fetching Live Data

Instead of multiple Google Sheets nodes (which hit rate limits), I wrote a single Code node that fetches all 4 sheets in one execution:

const sheets = ['Ticketing', 'Finance', 'Properties', 'IT'];
const results = {};

for (const sheetName of sheets) {
  const url = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${sheetName}?key=${apiKey}`;
  const response = await this.helpers.httpRequest({ method: 'GET', url });
  const rows = response.values;
  const headers = rows[0];
  results[sheetName] = rows.slice(1).map(row => {
    const obj = {};
    headers.forEach((h, i) => { obj[h] = row[i] || ''; });
    return obj;
  });
}

return [{ json: results }];
Enter fullscreen mode Exit fullscreen mode

This runs once and gives the AI complete context across all departments.


Step 4 — The AI Layer

I used Groq's free tier with LLaMA 3.1 8B. The prompt is the key — it needs strict formatting rules otherwise the AI gives verbose responses that look terrible in WhatsApp:

You are a data assistant for Fayz e Husayni.
Answer ONLY from the data provided.
Max 4 lines. No calculation steps. End with one emoji.
Never volunteer unrequested information.
Enter fullscreen mode Exit fullscreen mode

The example-based formatting in the prompt made a huge difference — showing the AI exactly how a correct response looks forced it to follow the format consistently.


Step 5 — Report Generation

When someone types "send me finance report", the system detects the word "report" and routes to a different flow that formats the data as a structured text report:

📊 Finance Report
Generated: 09/06/2026
─────────────────
1. Batch A Collections — PKR 510,000 — Received
2. Batch B Collections — PKR 270,000 — Partial
...
─────────────────
Total records: 8
Enter fullscreen mode Exit fullscreen mode

No AI needed for this — pure data formatting.


Step 6 — Error Handling

If Groq is down or the Sheets API fails, the bot sends a friendly message instead of dying silently:

⚠️ Sorry, I'm unable to process your request right now. Please try again in a moment.
Enter fullscreen mode Exit fullscreen mode

Small detail, big difference in user experience.


What I Learned

WAHA Core is limited. File sending, Business WhatsApp, and multiple sessions require WAHA Plus. Plan your budget accordingly.

Groq free tier burns fast during development. Use it sparingly in testing — each workflow test consumes quota. I switched between accounts twice in one day.

Prompt formatting matters more than the model. LLaMA 3.1 with good formatting instructions outperformed my initial Gemini 2.5 attempts. Concrete examples in the prompt are more effective than abstract rules.

n8n's Code node limitations are real. No fetch, no $http — use this.helpers.httpRequest for external calls.


Production Requirements

This prototype runs on a local machine. For production:

  • VPS server (~$5-10/month) for 24/7 uptime
  • WAHA Plus ($19/month) for Business WhatsApp and file delivery
  • Dedicated WhatsApp number

The Code

Full source code, workflow JSON, docker-compose, and setup guide:

👉 github.com/rajjcreationss-hub/unified-data-intelligence-system


Built this for a client and planning to productize it as a template for other organizations. If you're building something similar or have questions, drop them in the comments.

Top comments (0)