Build AI WhatsApp Bots with Pure Python
See the codeThis guide will walk you through the process of creating a WhatsApp bot using the Meta (formerly Facebook) Cloud API with pure Python, and Flask particular. We'll also integrate webhook events to receive messages in real-time and use OpenAI to generate AI responses. For more information on the structure of the Flask application, you can refer to this documentation.
curl command which can be send from the terminal or with a tool like Postman..env files based on example.env and update the required variables. Video example here.Creating an access that works longer then 24 hours
Generate new token and select the app, and then choose how long the access token will be valid. You can choose 60 days or never expire.Now we have to find the following information on the App Dashboard:
You can only send a template type message as your first message to a user. That's why you have to send a reply first before we continue. Took me 2 hours to figure this out.
Please note, this is the hardest part of this tutorial.
pip install -r requirements.txtThe steps below are taken from the ngrok documentation.
You need a static ngrok domain because Meta validates your ngrok domain and certificate!
Once your app is running successfully on localhost, let's get it on the internet securely using ngrok!
ngrok http 8000 --domain your-domain.ngrok-free.app
In the Meta App Dashboard, go to WhatsApp > Configuration, then click the Edit button.
VERIFY_TOKEN environment variable.WEBHOOK_VERIFIED in the terminal.Use the phone number associated to your WhatsApp product or use the test number you copied before.
Below is some information from the Meta Webhooks API docs about verification and security. It is already implemented in the code, but you can reference it to get a better understanding of what's going on in security.py
Anytime you configure the Webhooks product in your App Dashboard, we'll send a GET request to your endpoint URL. Verification requests include the following query string parameters, appended to the end of your endpoint URL. They will look something like this:
GET https://www.your-clever-domain-name.com/webhook?
hub.mode=subscribe&
hub.challenge=1158201444&
hub.verify_token=meatyhamhock
The verify_token, meatyhamhock in the case of this example, is a string that you can pick. It doesn't matter what it is as long as you store in the VERIFY_TOKEN environment variable.
Whenever your endpoint receives a verification request, it must:
WhatsApp signs all Event Notification payloads with a SHA256 signature and include the signature in the request's X-Hub-Signature-256 header, preceded with sha256=. You don't have to validate the payload, but you should.
To validate the payload:
Review the developer documentation to learn how to build your app and start sending messages. See documentation.
Now that we have an end to end connection, we can make the bot a little more clever then just shouting at us in upper case. All you have to do is come up with your own generate_response() function in whatsapp_utils.py.
If you want a cookie cutter example to integrate the OpenAI Assistans API with a retrieval tool, then follow these steps.
OPENAI_API_KEY and OPENAI_ASSISTANT_ID in the environment variables.generate_reponse into whatsapp_utils.pyprocess_whatsapp_message() with the new generate_reponse() function.When you’re ready to use your app for a production use case, you need to use your own phone number to send messages to your users.
To start sending messages to any WhatsApp number, add a phone number. To manage your account information and phone number, see the Overview page. and the WhatsApp docs.
If you want to use a number that is already being used in the WhatsApp customer or business app, you will have to fully migrate that number to the business platform. Once the number is migrated, you will lose access to the WhatsApp customer or business app. See Migrate Existing WhatsApp Number to a Business Account for information.
Once you have chosen your phone number, you have to add it to your WhatsApp Business Account. See Add a Phone Number.
When dealing with WhatsApp Business API and wanting to experiment without affecting your personal number, you have a few options:
Recommendation: If this is for a more prolonged or professional purpose, using a virtual phone number service or purchasing a new SIM card for a dedicated device is advisable. For quick tests, a temporary number might suffice, but always be cautious about security and privacy. Remember that once a number is associated with WhatsApp Business API, it cannot be used with regular WhatsApp on a device unless you deactivate it from the Business API and reverify it on the device.
This document is provided to you by Datalumina. We help data analysts, engineers, and scientists launch and scale a successful freelance business — $100k+ /year, fun projects, happy clients. If you want to learn more about what we do, you can visit our website and subscribe to our newsletter. Feel free to share this document with your data friends and colleagues.
For video tutorials, visit the YouTube channel: youtube.com/@daveebbelaar.
3,001 followers · starred Sep 2025
215 followers · starred Dec 2024
85 followers · starred Aug 2024
66 followers · starred Jun 2024
Build AI WhatsApp Bots with Pure Python
See the codeThis guide will walk you through the process of creating a WhatsApp bot using the Meta (formerly Facebook) Cloud API with pure Python, and Flask particular. We'll also integrate webhook events to receive messages in real-time and use OpenAI to generate AI responses. For more information on the structure of the Flask application, you can refer to this documentation.
curl command which can be send from the terminal or with a tool like Postman..env files based on example.env and update the required variables. Video example here.Creating an access that works longer then 24 hours
Generate new token and select the app, and then choose how long the access token will be valid. You can choose 60 days or never expire.Now we have to find the following information on the App Dashboard:
You can only send a template type message as your first message to a user. That's why you have to send a reply first before we continue. Took me 2 hours to figure this out.
Please note, this is the hardest part of this tutorial.
pip install -r requirements.txtThe steps below are taken from the ngrok documentation.
You need a static ngrok domain because Meta validates your ngrok domain and certificate!
Once your app is running successfully on localhost, let's get it on the internet securely using ngrok!
ngrok http 8000 --domain your-domain.ngrok-free.app
In the Meta App Dashboard, go to WhatsApp > Configuration, then click the Edit button.
VERIFY_TOKEN environment variable.WEBHOOK_VERIFIED in the terminal.Use the phone number associated to your WhatsApp product or use the test number you copied before.
Below is some information from the Meta Webhooks API docs about verification and security. It is already implemented in the code, but you can reference it to get a better understanding of what's going on in security.py
Anytime you configure the Webhooks product in your App Dashboard, we'll send a GET request to your endpoint URL. Verification requests include the following query string parameters, appended to the end of your endpoint URL. They will look something like this:
GET https://www.your-clever-domain-name.com/webhook?
hub.mode=subscribe&
hub.challenge=1158201444&
hub.verify_token=meatyhamhock
The verify_token, meatyhamhock in the case of this example, is a string that you can pick. It doesn't matter what it is as long as you store in the VERIFY_TOKEN environment variable.
Whenever your endpoint receives a verification request, it must:
WhatsApp signs all Event Notification payloads with a SHA256 signature and include the signature in the request's X-Hub-Signature-256 header, preceded with sha256=. You don't have to validate the payload, but you should.
To validate the payload:
Review the developer documentation to learn how to build your app and start sending messages. See documentation.
Now that we have an end to end connection, we can make the bot a little more clever then just shouting at us in upper case. All you have to do is come up with your own generate_response() function in whatsapp_utils.py.
If you want a cookie cutter example to integrate the OpenAI Assistans API with a retrieval tool, then follow these steps.
OPENAI_API_KEY and OPENAI_ASSISTANT_ID in the environment variables.generate_reponse into whatsapp_utils.pyprocess_whatsapp_message() with the new generate_reponse() function.When you’re ready to use your app for a production use case, you need to use your own phone number to send messages to your users.
To start sending messages to any WhatsApp number, add a phone number. To manage your account information and phone number, see the Overview page. and the WhatsApp docs.
If you want to use a number that is already being used in the WhatsApp customer or business app, you will have to fully migrate that number to the business platform. Once the number is migrated, you will lose access to the WhatsApp customer or business app. See Migrate Existing WhatsApp Number to a Business Account for information.
Once you have chosen your phone number, you have to add it to your WhatsApp Business Account. See Add a Phone Number.
When dealing with WhatsApp Business API and wanting to experiment without affecting your personal number, you have a few options:
Recommendation: If this is for a more prolonged or professional purpose, using a virtual phone number service or purchasing a new SIM card for a dedicated device is advisable. For quick tests, a temporary number might suffice, but always be cautious about security and privacy. Remember that once a number is associated with WhatsApp Business API, it cannot be used with regular WhatsApp on a device unless you deactivate it from the Business API and reverify it on the device.
This document is provided to you by Datalumina. We help data analysts, engineers, and scientists launch and scale a successful freelance business — $100k+ /year, fun projects, happy clients. If you want to learn more about what we do, you can visit our website and subscribe to our newsletter. Feel free to share this document with your data friends and colleagues.
For video tutorials, visit the YouTube channel: youtube.com/@daveebbelaar.
3,001 followers · starred Sep 2025
215 followers · starred Dec 2024
85 followers · starred Aug 2024
66 followers · starred Jun 2024