Build AI WhatsApp Bots with Pure Python
In this guide, we'll explore how to build a WhatsApp bot using Meta’s Cloud API with pure Python and Flask. We’ll also dive into integrating webhook events to receive messages in real-time and using OpenAI for AI-driven responses.
Prerequisites
A Meta developer account. Create one here.
A business app. Learn to create one here.
Familiarity with Python.
Table of Contents
Get Started
In this section, we’ll begin setting up your app environment, integrating the WhatsApp API, and preparing for real-time communication via webhook events.
Step 1: Select Phone Numbers
To get started, you'll need to add a phone number to your Meta app.
Go to the API Setup in your Meta Developer dashboard.
Choose a test number for sending messages to up to 5 WhatsApp contacts, including your own.
Verify your phone number by entering the code sent to your WhatsApp app.
Step 2: Send Messages with the API
With a test number ready, you can send your first message via the WhatsApp API.
Obtain a 24-hour access token from the API access section.
Convert the provided curl example into a Python function using the
requests
library.Update your
.env
file with necessary environment variables likeAPP_ID
,APP_SECRET
,ACCESS_TOKEN
, etc.Send a "Hello World" message. You should see the message within 60–120 seconds.
For more persistent access, set up a System User to generate longer-lived access tokens (e.g., 60 days or non-expiring).
Step 3: Configure Webhooks to Receive Messages
Webhook configuration is crucial for receiving messages in real time.
Install required dependencies (
pip install -r requirements.txt
) and start your Flask app.Use
ngrok
to expose your local environment to the web for webhook testing.Download the ngrok agent and authenticate with your token.
Start
ngrok
withngrok http 8000 --domain
your-domain.ngrok-free.app
.
In the Meta App Dashboard, go to WhatsApp > Configuration.
Enter your
ngrok
URL followed by/webhook
in the Callback URL field.Set a verification token of your choice and ensure it matches the environment variable
VERIFY_TOKEN
.
Test your setup by sending a message to your app, ensuring it logs and responds appropriately.
Step 4: Understanding Webhook Security
Meta ensures that webhook requests are secure through verification processes.
Verification Requests: When a webhook is first configured, Meta sends a GET request that must be verified by responding with the
hub.challenge
value.Validating Payloads: All event notifications are signed using SHA256. Your app should validate this signature to ensure data integrity.
Step 5: Learn about the API and Build Your App
Familiarize yourself with the WhatsApp API by referring to the official documentation. Here, you’ll learn how to send various types of messages, including text, images, and template messages.
Step 6: Integrate AI into the Application
Now that your WhatsApp bot is functioning, we can make it more intelligent by integrating AI.
Create a function
generate_response()
using OpenAI’s API.Update the environment with
OPENAI_API_KEY
andOPENAI_ASSISTANT_ID
.Modify
whatsapp_
utils.py
to integrate your AI response mechanism, so the bot can generate dynamic replies based on user inputs.
Step 7: Add a Phone Number
When you’re ready for production, add a dedicated phone number to send messages.
You can either migrate an existing WhatsApp number to the business platform or use a new number.
Options for testing include using virtual phone numbers, dual SIMs, or temporary number services.
Ensure that your chosen number is correctly configured in the WhatsApp Business API to avoid service disruptions.
By following this guide, you’ll be able to set up a fully functional AI-powered WhatsApp bot using Python, Flask, and the Meta Cloud API. Now you can build, test, and deploy creative chatbot applications for various use cases!
Subscribe to my newsletter
Read articles from Martins Ugwu's Corner directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Martins Ugwu's Corner
Martins Ugwu's Corner
I have a strong background in GIS, software development skills, and over six years of experience in energy and resource management, leveraging integrated programming and advanced geospatial technologies to enhance decision-making and operations efficiency.