0
Home About Projects Blog Contact
Artifical IntelligenceCloudDjangoPostgreSQLRAGredis

Enterprise WhatsApp CRM & Autonomous AI Sales Engine

A production-ready WhatsApp CRM that bridges the gap between human support and automated sales. Built from scratch using Django and the Meta Cloud API, it provides agents with a high-fidelity WhatsApp Web clone while deploying a strictly-constrained AI agent (OpenAI) to qualify leads, handle bilingual queries (Roman Urdu + English), and collect payments, with a seamless human-in-the-loop handoff.

Enterprise WhatsApp CRM & Autonomous AI Sales Engine

The Challenge

When running high-budget marketing campaigns that route leads directly to a WhatsApp Business number, businesses face a massive drop-off rate due to response latency, especially during off-hours.

Meta’s native tools do not offer permanent custom database logs, granular lead categorization, or custom AI agent logic. Standard AI bots are either too rigid (simple keyword repliers) or too unpredictable (hallucinating GPT bots that invent fake prices or deny payment details).

The goal was to build a system that gives human operators a unified, real-time workspace to manage conversations, while simultaneously using a safe, sandboxed AI bot that can autonomously pitch specific pricing plans, provide payment instructions, and immediately hand off complex queries to humans.

The Solution

I designed and built a self-hosted Django CRM middleware integrated directly with the official Meta Cloud API (v19.0) and OpenAI:

1. Real-Time Webhook Engine: Built a Django endpoint to capture incoming messages, process delivery status pings (Sent, Delivered, Read), and log them securely.
2. Guard-Railed Conversational AI: Integrated GPT-3.5 with strict prompt constraints. I implemented a hybrid routing layer: static keywords (like PRICE, START, TEST) trigger instantaneous rule-based templates, while conversational queries fall back to the AI. The LLM is restricted to a local pricing schema, answers in a Roman Urdu/English blend, and respects a strict 2-line length limit.
3. Media Proxy & Caching System: Meta's media links expire after 24 hours. I built a local proxy that fetches and caches audio, documents, and images on our server, served via a custom HTML5 audio playback script for voice notes.
4. Human-in-the-Loop Mechanism: Created a live-toggle state per contact. If a customer asks a query beyond the bot's data, it outputs a specialized flag (UNKNOWN_QUERY), records it, pauses the bot for that user, and alerts the operator.
5. Optimized UI: Designed a responsive dashboard using Bootstrap 5 and vanilla JavaScript with a 3-second polling state manager, avoiding heavy SPA framework overhead while maintaining a snappy user experience.

Deep Dive

This project is a fully-featured, enterprise-grade Customer Relationship Management (CRM) dashboard designed specifically for high-growth businesses using WhatsApp as their primary sales channel.

Core Engineering Highlights

  • Asynchronous Meta Webhook Middleware: Handled high-throughput inbound traffic from Meta’s servers, ensuring message integrity, parsing payload structures (texts, locations, and attachments), and updating read/delivered ticks instantly.
  • Hybrid Automation Router: Engineered a clean classification system in Python that segments incoming queries. It checks local regex patterns first to minimize OpenAI API cost, falling back to LLM completion only when conversational context is required.
  • Local Media Storage & Streaming: Designed a file proxy that intercepts Meta's media URLs, downloads attachments, caches them in the server filesystem, and serves them locally. This guarantees 100% data retention even after Meta's links expire.
  • Stateful Chat Panel: Developed a responsive Single Page Application (SPA) layout that includes unread badges, lead categorization filters (New Lead, VIP, Customer, Support), a template library for operators, and an interactive audio interface for voice recordings.

Key Architectural Features

  • Global & Local AI Toggle: Operators can disable the AI globally during peak hours or toggle it per conversation with a single click.
  • Bilingual Sales Persona: The bot is trained specifically in Roman Urdu and English syntax, allowing it to converse naturally with South Asian demographics while maintaining professional guidelines.
  • Anti-Hallucination Sandbox: Strict guidelines prevent the bot from fabricating features or pricing plans not defined in the backend configuration database.

Results & Impact

- Built a production-ready, self-hosted alternative to expensive third-party WhatsApp CRM platforms (like Wati or Sirena), eliminating recurring seat-based software fees.
- Reduced off-hours lead response times from 8+ hours to sub-3 seconds using the automated hybrid routing engine.
- Successfully qualified incoming leads automatically, filtering out spam and low-intent traffic before passing high-intent customers to human agents.
- Optimized API costs by routing 40% of standard customer inquiries through the local regex-based keyword engine rather than invoking OpenAI completions.