NODE.JS TUTORIAL

Building a "Human-Like" WhatsApp Sales Bot

How we handle 500+ Real Estate leads/day using Node.js, Firebase Functions, and OpenAI.

At JegoDigital, we automate sales for high-ticket Real Estate agencies. The biggest challenge? Speed. We needed a bot that responds in < 30 seconds, not just with "Hello" , but with context.

The Stack

Step 1: The Webhook

First, we need to listen for incoming messages from WhatsApp.


const functions = require('firebase-functions');
const axios = require('axios');

exports.webhook = functions.https.onRequest(async (req, res) => {
    const message = req.body.entry[0].changes[0].value.messages[0];
    
    // 1. Check if it's a text message
    if (message.type === 'text') {
        const userText = message.text.body;
        const from = message.from;

        // 2. Process with AI
        await processAI(userText, from);
    }
    res.sendStatus(200);
});
            

Step 2: The "Brain" (Context Awareness)

A dumb bot forgets what you said 2 seconds ago. We use Firestore to store the "Thread". This allows the bot to remember: "Oh, the client is looking for a Penthouse in Tulum."


async function processAI(text, userId) {
    // Fetch history
    const history = await db.collection('chats').doc(userId).get();
    
    const response = await openai.createChatCompletion({
        model: "gpt-4o",
        messages: [
            { role: "system", content: "You are a senior Real Estate Broker in Tulum." },
            ...history.data().messages,
            { role: "user", content: text }
        ]
    });
    
    // Send back to WhatsApp
    await sendWhatsApp(response.data.choices[0].message.content, userId);
}
            

Why this matters for Business

We deployed this for a client and reduced their "Lead Response Time" from 4 hours to 12 seconds. You can read the business case study here: Real Estate Automation Impact.

Conclusion

You don't need complex tools like LangChain for a simple sales bot. Pure Node.js + OpenAI is fast, cheap, and scalable.