Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 2 min read

Build a WhatsApp AI Customer Service Bot with a Cheap OpenAI-Compatible API

Build a WhatsApp AI customer-service bot You can run an AI customer-service agent on WhatsApp by wiring three pieces together: the WhatsApp Cloud API (to receive and send messages), a small webhook server (your glue),

Build a WhatsApp AI customer-service bot

You can run an AI customer-service agent on WhatsApp by wiring three pieces together: the WhatsApp
Cloud API (to receive and send messages), a small webhook server (your glue), and an
OpenAI-compatible LLM for the replies. APIVAI provides the LLM step β€” GPT-5.5 is a great fit for
natural, multilingual support β€” at a fraction of official price, which matters when you're paying
per customer message.

This guide is the architecture plus the core code.

Architecture

Customer on WhatsApp
   β”‚
   β–Ό
WhatsApp Cloud API (Meta)  ──webhook──▢  Your server
                                            β”‚  (build prompt + history)
                                            β–Ό
                                   APIVAI  /v1/chat/completions  (GPT-5.5)
                                            β”‚  reply text
                                            β–Ό
WhatsApp Cloud API  ◀──send message──  Your server

1. Get WhatsApp Cloud API access

Create a Meta app, add the WhatsApp product, and get: a phone number ID, a permanent access
token, and a webhook verify token. Point the webhook at your server's /webhook URL.

2. Webhook server (Node.js)

import express from "express";
import OpenAI from "openai";

const app = express();
app.use(express.json());

const ai = new OpenAI({ apiKey: process.env.APIVAI_API_KEY, baseURL: "https://api.apivai.com/v1" });
const SYSTEM = "You are a friendly customer-service agent for our store. Answer concisely in the customer's language. If unsure, offer to connect a human.";

// Meta webhook verification
app.get("/webhook", (req, res) => {
  if (req.query["hub.verify_token"] === process.env.VERIFY_TOKEN) return res.send(req.query["hub.challenge"]);
  res.sendStatus(403);
});

// Incoming messages
app.post("/webhook", async (req, res) => {
  res.sendStatus(200); // ack fast
  const msg = req.body?.entry?.[0]?.changes?.[0]?.value?.messages?.[0];
  if (!msg?.text) return;

  const reply = await ai.chat.completions.create({
    model: "gpt-5.5",
    messages: [{ role: "system", content: SYSTEM }, { role: "user", content: msg.text.body }],
    max_tokens: 300,
  });

  await sendWhatsApp(msg.from, reply.choices[0].message.content);
});

async function sendWhatsApp(to, text) {
  await fetch(`https://graph.facebook.com/v20.0/${process.env.PHONE_ID}/messages`, {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.WA_TOKEN}`, "Content-Type": "application/json" },
    body: JSON.stringify({ messaging_product: "whatsapp", to, text: { body: text } }),
  });
}

app.listen(8080);

3. Add memory and product knowledge

  • Conversation memory: store the last few messages per customer (by phone number) and pass them in the messages array so replies stay in context.
  • Product knowledge: prepend key facts (hours, shipping, return policy, catalog highlights) to the system prompt, or retrieve relevant snippets from your docs and inject them (RAG).
  • Human handoff: detect low-confidence or escalation phrases and route to a human inbox.

4. Pick the model

GPT-5.5 is the recommended default for support β€” natural tone, strong multilingual handling, low
latency, and cheap per message through APIVAI. For very high volume, route simple FAQs to a
smaller model and reserve GPT-5.5 for nuanced questions.

Cost note

Support bots send many short messages. APIVAI's OpenAI-compatible pricing at a fraction of list,
pay-as-you-go, keeps the per-conversation cost low β€” and you can pay with crypto/USDT/Alipay.

FAQ

Does this work for Facebook/Instagram DMs too? Yes β€” same pattern with the Messenger Platform
webhook; only the send/receive API differs. The APIVAI call is identical.

Which model for customer service? GPT-5.5 for natural multilingual replies; a smaller model for
bulk/simple FAQs.

Do I need to change code to switch models? No β€” change the model string; the endpoint is
OpenAI-compatible.

Get started

Get an APIVAI key, drop it into the webhook above, and connect the WhatsApp Cloud API. Examples:
APIVAI examples repo.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.