What You’ll Connect
After this guide, your WhatsApp Business account will respond to customer messages using your own GPU-hosted LLM — delivering intelligent, context-aware replies through the messaging platform your customers already use. The integration connects the WhatsApp Cloud API to your vLLM or Ollama endpoint on dedicated GPU hardware, handling customer inquiries, product questions, and support requests without manual agent intervention.
The integration uses the WhatsApp Cloud API webhooks to receive incoming messages. Your server processes each message through your OpenAI-compatible API, maintains conversation context per phone number, and sends the AI response back via the WhatsApp API. Customer data stays within your infrastructure — the GPU processes the conversation logic, and only the final reply text passes through WhatsApp’s delivery system.
Prerequisites
- A GigaGPU server running a self-hosted LLM (setup guide)
- A Meta Business account with WhatsApp Business API access
- A verified WhatsApp Business phone number
- Python 3.10+ with
fastapiandhttpx
Integration Steps
Set up your WhatsApp Cloud API application in the Meta Developer Portal. Configure the webhook URL pointing to your bot server and subscribe to the messages webhook field. WhatsApp sends incoming messages to your webhook as POST requests with a signed payload.
Build a webhook handler that verifies the signature, extracts the customer message, looks up conversation history for that phone number, calls your GPU endpoint, and sends the AI response back through the WhatsApp API. Store conversation history in a database keyed by phone number for multi-turn context.
Add a handoff mechanism for conversations that need human agents. When the AI detects questions it cannot answer confidently or the customer requests a human, the system routes the conversation to a live agent queue and notifies the support team. The AI resumes after the agent marks the conversation as resolved.
Code Example
WhatsApp Business webhook with AI responses from your self-hosted LLM:
from fastapi import FastAPI, Request
import httpx, json, os
app = FastAPI()
WA_TOKEN = os.environ["WHATSAPP_TOKEN"]
WA_PHONE_ID = os.environ["WHATSAPP_PHONE_ID"]
GPU_URL = os.environ["GPU_API_URL"] + "/v1/chat/completions"
GPU_KEY = os.environ["GPU_API_KEY"]
VERIFY_TOKEN = os.environ["WEBHOOK_VERIFY_TOKEN"]
conversations = {}
SYSTEM_PROMPT = """You are a helpful customer support assistant for
[Your Company]. Be concise, friendly, and helpful. If you cannot
answer a question, say you will connect them with a team member."""
@app.get("/webhook")
async def verify(request: Request):
params = request.query_params
if params.get("hub.verify_token") == VERIFY_TOKEN:
return int(params["hub.challenge"])
return {"error": "Invalid token"}
@app.post("/webhook")
async def handle_message(request: Request):
body = await request.json()
for entry in body.get("entry", []):
for change in entry.get("changes", []):
msg = change["value"].get("messages", [{}])[0]
if msg.get("type") != "text":
continue
phone = msg["from"]
text = msg["text"]["body"]
if phone not in conversations:
conversations[phone] = [
{"role": "system", "content": SYSTEM_PROMPT}
]
conversations[phone].append({"role": "user", "content": text})
# Get AI response
async with httpx.AsyncClient(timeout=30) as client:
resp = await client.post(GPU_URL, json={
"model": "meta-llama/Llama-3-70b-chat-hf",
"messages": conversations[phone][-16:],
"max_tokens": 500, "temperature": 0.3
}, headers={"Authorization": f"Bearer {GPU_KEY}"})
ai_reply = resp.json()["choices"][0]["message"]["content"]
conversations[phone].append(
{"role": "assistant", "content": ai_reply})
# Send reply via WhatsApp
async with httpx.AsyncClient() as client:
await client.post(
f"https://graph.facebook.com/v18.0/{WA_PHONE_ID}/messages",
json={"messaging_product": "whatsapp", "to": phone,
"text": {"body": ai_reply}},
headers={"Authorization": f"Bearer {WA_TOKEN}"}
)
return {"status": "ok"}
Testing Your Integration
Register a test phone number in the WhatsApp Cloud API sandbox. Send a message to your WhatsApp Business number and verify the AI responds within a few seconds. Test multi-turn conversations to confirm context is maintained across messages. Send messages from different numbers to verify conversation isolation.
Test the human handoff flow by triggering the handoff condition and verifying the routing works. Check the WhatsApp API response codes for message delivery confirmation. Test with messages in different languages if your LLM supports multilingual responses.
Production Tips
Move conversation storage from in-memory to a database (PostgreSQL or Redis) for persistence and scalability. Add message templates for common responses that WhatsApp pre-approves for outbound messaging. Implement business hours logic — AI handles messages 24/7, but human handoff only queues during staffed hours.
Monitor response latency to keep it under WhatsApp’s expectations. Customers expect replies within seconds for messaging. Track customer satisfaction by analysing conversation outcomes and handoff rates. Build a comprehensive AI chatbot customer service platform. Explore more tutorials or get started with GigaGPU to power your WhatsApp integration.