Remaining:60 min
← Back to Tutorials
⚡ Automation•60 min•Beginner•Nov 16, 2026

1 Hour to Set Up WhatsApp AI Customer Service

Build a 24/7 WhatsApp AI bot that auto-replies to customers using OpenAI + Twilio in 60 min.

#whatsapp#api-integration#automation#customer-service#ai

By the end of this hour, you'll have a working WhatsApp bot that reads customer messages and fires back intelligent AI replies automatically — no human needed, no missed messages at 2 AM.

🎯 What You'll Build

A Node.js webhook server that connects Twilio's WhatsApp API to OpenAI's GPT model. When a customer sends a WhatsApp message, your bot reads it, asks GPT for a smart reply, and sends it back — all within seconds.

Here's the core logic you'll end up with:

// When WhatsApp message arrives → ask GPT → reply instantly
app.post("/webhook", async (req, res) => {
  const customerMessage = req.body.Body;
  const customerPhone   = req.body.From;

  const aiReply = await askGPT(customerMessage);
  await sendWhatsApp(customerPhone, aiReply);

  res.sendStatus(200);
});

Real example flow:

  • Customer: "Do you ship to Canada?"
  • Bot (within 3 seconds): "Yes! We ship to Canada via DHL. Standard delivery takes 7–10 business days. Would you like to place an order?"

⏱️ Time Breakdown

0–10min
Create accounts & grab API keys
10–25min
Set up the Node.js project
25–40min
Build the webhook & AI reply logic
40–55min
Expose the server & connect Twilio
55–60min
Test live on WhatsApp & ship

📋 Prerequisites

  • A laptop with Node.js 18+ installed (node -v should return v18 or higher)
  • A free Twilio account (twilio.com) — takes 2 minutes to sign up
  • An OpenAI account with API access (platform.openai.com)
  • A WhatsApp account on your phone for testing
  • Basic comfort with copy-pasting terminal commands (no coding experience needed)

Step 1: Create Accounts and Grab API Keys (0–10 min)

You need two API keys before writing a single line of code. This step gets both.

Get your OpenAI API key:

  1. Go to platform.openai.com → click your avatar → "API keys"
  2. Click "Create new secret key" → name it "WhatsApp Bot"
  3. Copy the key immediately — you won't see it again

Get your Twilio credentials:

  1. Go to twilio.com → sign up for a free account
  2. On the dashboard, find "Account SID" and "Auth Token" — copy both
  3. In the left sidebar, go to Messaging → Try it Out → Send a WhatsApp Message
  4. Follow the on-screen steps to join the Twilio Sandbox (you'll send a WhatsApp message like "join flying-tiger" to a Twilio number)
  5. Note the Twilio sandbox WhatsApp number (looks like +1 415 523 8886)

Create a file called .env on your Desktop for now and paste everything in:

OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_WHATSAPP_NUMBER=whatsapp:+14155238886
✅

Checkpoint

You have 3 credentials saved. Which Twilio credential starts with the letters AC?

Step 2: Set Up the Node.js Project (10–25 min)

You'll see a working server running on your machine by the end of this step.

Open your terminal and run these commands one by one:

mkdir whatsapp-ai-bot
cd whatsapp-ai-bot
npm init -y
npm install express twilio openai dotenv

Now create the project structure:

touch index.js .env

Copy your .env contents from Step 1 into the .env file inside the project folder.

Add one more line to .env — your store description that the AI will use to answer questions:

STORE_CONTEXT="We are FreshGlow, an online skincare store. We ship globally. Free shipping over $50. Returns accepted within 30 days. Email: support@freshglow.com"

Replace the FreshGlow text with your own business details. This is how the AI knows what to say.

Open index.js and add the basic server skeleton:

require("dotenv").config();
const express = require("express");
const app     = express();

app.use(express.urlencoded({ extended: false }));
app.use(express.json());

app.get("/", (req, res) => {
  res.send("WhatsApp AI Bot is running!");
});

app.listen(3000, () => {
  console.log("Server started on port 3000");
});

Run it:

node index.js

Open your browser and visit http://localhost:3000. You should see "WhatsApp AI Bot is running!" — your server is alive.

Step 3: Build the Webhook and AI Reply Logic (25–40 min)

This is the heart of the bot. You'll wire up OpenAI and Twilio so they talk to each other.

Replace everything in index.js with this complete file:

require("dotenv").config();
const express  = require("express");
const OpenAI   = require("openai");
const twilio   = require("twilio");

const app    = express();
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const client = twilio(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);

app.use(express.urlencoded({ extended: false }));
app.use(express.json());

// Ask GPT for a customer service reply
async function askGPT(customerMessage) {
  const response = await openai.chat.completions.create({
    model: "gpt-3.5-turbo",
    messages: [
      {
        role: "system",
        content: `You are a helpful customer service agent. 
                  ${process.env.STORE_CONTEXT}
                  Keep replies under 3 sentences. Be friendly and direct.`,
      },
      {
        role: "user",
        content: customerMessage,
      },
    ],
  });
  return response.choices[0].message.content;
}

// Send WhatsApp message via Twilio
async function sendWhatsApp(to, body) {
  await client.messages.create({
    from: process.env.TWILIO_WHATSAPP_NUMBER,
    to:   to,
    body: body,
  });
}

// Main webhook — Twilio calls this when a message arrives
app.post("/webhook", async (req, res) => {
  const customerMessage = req.body.Body;
  const customerPhone   = req.body.From;

  console.log(`Incoming from ${customerPhone}: ${customerMessage}`);

  try {
    const aiReply = await askGPT(customerMessage);
    console.log(`AI reply: ${aiReply}`);
    await sendWhatsApp(customerPhone, aiReply);
    res.sendStatus(200);
  } catch (err) {
    console.error("Error:", err.message);
    res.sendStatus(500);
  }
});

app.get("/", (req, res) => res.send("WhatsApp AI Bot is running!"));

app.listen(3000, () => console.log("Server started on port 3000"));

Restart the server:

node index.js

No errors in the terminal means the logic is wired up correctly.

✅

Checkpoint

After saving and restarting, what message should you see in the terminal?

Step 4: Expose the Server and Connect Twilio (40–55 min)

Right now your server is on your laptop — Twilio can't reach it from the internet. You'll fix that with ngrok, which creates a public URL for your local server in one command.

Install and run ngrok:

# Download from ngrok.com and install, then:
ngrok http 3000

You'll see output like this:

Forwarding   https://abc123def456.ngrok-free.app -> http://localhost:3000

Copy that https:// URL. Keep this terminal window open — closing it kills your public URL.

Point Twilio to your webhook:

  1. Go to twilio.com → Messaging → Settings → WhatsApp Sandbox Settings
  2. Find the field labeled "When a message comes in"
  3. Paste your ngrok URL and add /webhook at the end: https://abc123def456.ngrok-free.app/webhook
  4. Make sure the method dropdown is set to HTTP POST
  5. Click Save

Test that Twilio can reach your server:

# In a new terminal window, simulate a Twilio webhook call:
curl -X POST https://abc123def456.ngrok-free.app/webhook \
  -d "Body=Hello, do you have free shipping?" \
  -d "From=whatsapp:+1234567890"

Your first terminal should log the incoming message and AI reply. If you see both lines, the connection is working.

✅

Checkpoint

After running the curl command, what two lines should appear in your server terminal?

Step 5: Ship It (55–60 min)

Time to test the real thing on your phone.

  1. Open WhatsApp on your phone
  2. Message the Twilio sandbox number you noted in Step 1
  3. Type any customer question — for example: "What's your return policy?"
  4. Wait 2–5 seconds

Your AI bot should reply with an intelligent, context-aware answer pulled from your store description.

What bad looks like (old way):

Customer messages at 11 PM → no reply → customer buys from competitor

What good looks like (now):

Customer messages at 11 PM → AI replies in 3 seconds → customer gets their answer → sale saved

🎉 Your 24/7 WhatsApp AI customer service bot is live. Every message your customers send while you sleep now gets an instant, helpful reply.

🎁 Bonus

  • Add conversation memory: Store each customer's last 5 messages in a simple object and pass them all to GPT so the bot remembers context mid-conversation
  • Route to a human agent: Add a keyword check — if the customer types "human" or "agent," forward their number to your phone via SMS using Twilio's regular SMS API
  • Deploy to a server so ngrok isn't needed: Push the project to Railway.app (free tier) in 5 minutes so the bot runs 24/7 without keeping your laptop open

📚 Next Steps

🔗 Resources