Build a 24/7 WhatsApp AI Assistant in 4 Simple Steps
In the last article, we discussed the game-changing impact of having an AI assistant that handles customer queries and closes deals on WhatsApp while you sleep. The response was overwhelming, with one common question: "How do I actually build it?"
| Image by Author | Created with AI |
This guide will demystify the process. While it involves a few technical steps, the tools available today have made it more accessible than ever. We're going to build a foundational version of your AI assistant that you can then expand and customize over time.
The procedure may be divided into three fundamental parts:
- "Brain": The Large Language Model (LLM) that produces responses (e.g., OpenAI's GPT).
- "Mouth": The official WhatsApp Business API for sending and receiving messages.
- "Nervous System": The code that links the brain to the mouth.
Prerequisites: What You'll Need
Before we start, let’s collect the required elements:
-
A dedicated phone number — It must not already be used by any WhatsApp personal or business account.
-
A Meta for Developers Account — Free to create at developers.facebook.com.
-
An OpenAI API Account — Needed to get your API key; you can create one at platform.openai.com.
-
A Platform for Your Code — We’ll use Replit, a free service that includes both a server and code editor.
Step 1: Set Up Your "Mouth" (WhatsApp Business API)
First, we need to connect to WhatsApp. We’ll start by using a temporary test number provided by Meta — perfect for development.
-
Create a Meta App: Visit developers.facebook.com, go to “My Apps,” then “Create App.” Choose “Other” as the use case and select “Business.”
-
Add the WhatsApp Product: On your new app’s dashboard, scroll down to find “WhatsApp” and click “Set up.”
-
Get Your Credentials: After setup, you’ll land on the “API Setup” page. Keep it open — it includes three essential details:
-
A test phone number for sending messages
-
A Phone Number ID (unique to your bot’s number — required for sending replies)
-
A temporary Access Token (your app’s password)
-
You can now use this interface to send a test message to your personal WhatsApp number and verify that it’s working.
Step 2: Prepare Your AI "Brain" (OpenAI)
Now it’s time to give your AI the ability to think and talk.
-
Get Your OpenAI API Key: Log in to platform.openai.com, go to “API Keys,” create a new secret key, and store it securely.
-
Create Your System Prompt: This is crucial — it defines your AI’s personality and behavior. Write a clear, specific instruction that shapes how your assistant responds.
Example System Prompt:
You are a warm and efficient support and sales assistant for GlowUp Skincare, a brand that sells organic, vegan skincare products.
Your tone is friendly, informative, and a little enthusiastic.
You answer product questions, help customers choose the right items, and share order updates.
If you’re unsure or the customer is upset, say: “That’s a great question, I’ll transfer you to a human team member who can help.”
Step 3: Build the "Nervous System" (The Code)
This connects everything. The following corrected code handles text messages only, ignoring images or stickers to prevent crashes.
-
Set Up on Replit: Create a free account, then start a new “Python (with Flask)” project.
-
Store Your Secrets: In Replit, use the Secrets tab (on the left sidebar) to safely save your credentials:
-
OPENAI_API_KEY: Your OpenAI key -
WHATSAPP_TOKEN: Your temporary Access Token -
WHATSAPP_PHONE_NUMBER_ID: Your Meta phone number ID
-
-
Replace the Default Code in
main.pywith:
Step 4: Connect and Test!
Time to bring your bot to life:
-
Run your Replit app — click the green “Run” button. Replit will generate a public URL (e.g.,
https://your-project-name.replit.dev). -
Set Up Your Webhook — go back to your Meta App’s WhatsApp dashboard:
-
Click “Configuration.”
-
In the “Webhook” section, click “Edit.”
-
Add your Replit URL (ending with
/webhook). -
Enter your exact
VERIFY_TOKENvalue.
-
-
Subscribe to Messages — click “Manage” next to the webhook and enable the “messages” field.
-
Test It! — Send a WhatsApp message to your test number.
If everything is configured properly, your AI assistant will respond within seconds! The try...except block ensures your bot won’t crash if an error occurs.