Connecting an AI Agent to WhatsApp
How to process WhatsApp messages using LangGraph and Cloud Run
The day is here, my friend.
We’ve made it to the final lesson of Ava, the WhatsApp agent.
It’s been a wild six weeks since we kicked off this open-source course! Along the way, we’ve build LangGraph workflows, implemented short- and long-term memories, and unpacked concepts like VLMs, TTS and STT.
Now, it’s time to bring it all together.
Ava is finally installing WhatsApp 📱
This is the sixth and final lesson of “Ava: The Whatsapp Agent” course. This lesson builds on the theory and code covered in the previous ones, so be sure to check them out if you haven’t already!
To connect Ava to WhatsApp, we need two things:
A Cloud Run service to deploy the LangGraph app.
A Meta App with WhatsApp as a product.
Deploying Ava to Cloud Run
First things first: we need to deploy Ava to the cloud!
We’ll be setting up a FastAPI application that listens for requests from a Meta App (we’ll get into that in the next section).
For now, just know that we’ll have an endpoint, named whatsapp_response, which will handle incoming messages using the LangGraph workflow we covered in previous lessons.
No worries if you see some env variables you don’t recognise - we’ll go over those in the next section.
All the code for the WhatsApp endpoint is inside the interfaces folder. Take a look!
Now that our FastAPI code is ready, it’s time to deploy it to the cloud. We’ll be using Cloud Run, a Google Cloud Platform (GCP) service that lets you deploy containerised applications without dealing with Kubernetes clusters.
Before you proceed with the deployment, you must follow this document to setup your Google Cloud Platform service accounts, docker registry and secrets.
The whole process - building the Docker image, pushing it to Google Artifact Registry, and deploying the Cloud Run service - is automated using Cloud Build. If you are curious, you can check out the Cloud Build YAML file right here.
Just run the following command, and your deployment will kick off!
gcloud builds submit --region=<LOCATION>
Once the Cloud Run service is up and running, you should see something like this in the GCP console.
Next, we need to allow unauthenticated invocations (since the requests we’ll get from Meta won’t be authenticated) to make the API public.
Make sure FastAPI is working as expected by checking out the Swagger docs.
We’ve got this! Now that Ava is running on Cloud Run, let’s move on to setting up the Meta App 👇
Creating a Meta App with the WhatsApp product
First, you’ll need to create a Meta Developers account and set up an app inside it. You can see my app (called Ava) right below.
After that, add the WhatsApp product to your app. Then, click on the API Setup, where you'll find two important pieces of information we need for the WhatsApp connection: the Phone Number ID and the Access Token.
Once you’ve copied these two values, make sure to add them to the .env file in your cloned repo!
Next, under the Configuration tab, you’ll see the following screen.
Here, you’ll need to define two values: the Callback URL and the Verify token.
The Callback URL is where the WhatsApp Webhook will send the data. Simply copy and paste your Cloud Run public URL and append the endpoint the Webhook will use to send messages: {cloud_run_url}/whatsapp_response
The Verify Token is used to ensure that the requests are coming from Meta. This token is set by you, and we’ll use it later in Cloud Run to validate the requests.
Also, make sure to subscribe to the specific Webhook fields you’re interested in. In our case, we’ll only use the messages field. This means the WhatsApp webhook will send the new messages received (basically the messages you’ll send to Ava) to the Callback Service.
Now, click Verify and Save, and you should be all set! Time to meet Ava.
Chatting with Ava!
Now that everything is set up, it’s time to test Ava! Open your WhatsApp and use the Test Number from the WhatsApp API Setup. Just send a “Hey” and see what happens…
And that’s all for today! 🙌
Big thanks to all of you for the awesome support during the Ava course! But hey, this isn’t the end! There’s a lot more coming your way, so keep an eye out for what’s next.
And don’t forget there’s a complementary video lesson on Jesús Copado’s YouTube channel.
We strongly recommend checking out both resources (written lessons and video lessons) to maximize your learning experience! 🙂
Catch you soon, and keep learning! 🙌
can you help setup the whatsapp account, it doesn't work with the instructions, I am unable to receive messages due to 500 error