Send Message with WhatsApp API
import logging
from fastapi import FastAPI, Depends, HTTPException
from fastapi.responses import RedirectResponse
from some_get_request_handler import handle_get_endpoint
from some_post_request_handler import handle_post_endpoint, Data
from whatsapp_api_handler import send_whatsapp_message
from pydantic import BaseModel
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)
app = FastAPI()
class WhatsAppMessageRequest(BaseModel):
# No parameters required as per the latest update
pass
@app.post("/send-whatsapp-message/")
def send_message(request: WhatsAppMessageRequest):
try:
response = send_whatsapp_message()
return {"success": True, "response": response}
except HTTPException as e:
logger.error(f"Failed to send WhatsApp message: {e.detail}")
Send Message with WhatsApp API
Created: | Last Updated:
Introduction to the Send Message with WhatsApp API Template
Welcome to the "Send Message with WhatsApp API" template! This template is designed to help you quickly set up an application that can send WhatsApp messages using the WhatsApp API. It's perfect for businesses or individuals who want to automate their WhatsApp communication through a WhatsApp Business Account. The template uses FastAPI to create an endpoint that interacts with the WhatsApp API, allowing you to send templated messages to specified phone numbers.
Getting Started
To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will set up the template in your Lazy Builder interface, and you can start customizing it to fit your needs.
Initial Setup: Adding Environment Secrets
Before you can test and use the application, you need to set up a few environment secrets. These are necessary for the WhatsApp API to authenticate your requests and send messages to the correct phone number. Here's what you need to do:
- Go to the Environment Secrets tab within the Lazy Builder.
- Add the following secrets with their respective values:
WHATSAPP_API_URL
: The URL endpoint for the WhatsApp API.WHATSAPP_API_TOKEN
: The access token for authenticating with the WhatsApp API.WHATSAPP_PHONE_NUMBER
: The phone number to which you want to send messages, in international format.
- To obtain these values, you will need to set up a WhatsApp Business Account and follow the WhatsApp API documentation to generate your API URL and token.
Test: Pressing the Test Button
Once you have set up the environment secrets, you can press the "Test" button on the Lazy platform. This will deploy your app and launch the Lazy CLI. If the template requires any user input, you will be prompted to provide it at this stage.
Using the App
After deployment, Lazy will provide you with a dedicated server link to use the API. You can interact with your new WhatsApp messaging app via this link. Additionally, since the app uses FastAPI, you will also be provided with a "/docs" link where you can see the auto-generated documentation for your API endpoints and test them directly from your browser.
To send a WhatsApp message using the API, you can make a POST request to the "/send-whatsapp-message/" endpoint. The request doesn't require any parameters, as the phone number and message template are already configured in the environment secrets and the code.
Here's a sample request you might make using a tool like curl:
curl -X POST "http://your-server-link/send-whatsapp-message/" -H "accept: application/json" -H "Content-Type: application/json"
And here's a sample response you might receive:
{
"success": true,
"response": {
"messages": [
{
"id": "gBEGkYiEB1VXAglK1e3FZ3A3Dd"
}
]
}
}
Integrating the App
If you wish to integrate this WhatsApp messaging functionality into another service or frontend, you can use the server link provided by Lazy to make API requests from your external tool. Ensure that you handle the authentication and headers as shown in the provided code template.
For example, if you're integrating with a CRM system, you might add the server link as an endpoint in your CRM's webhook settings, allowing you to trigger WhatsApp messages in response to certain events.
Remember, all the heavy lifting is done by Lazy, so you don't need to worry about deployment details or environment setup. Just focus on how you want to use the WhatsApp messaging capability in your application or workflow.
That's it! You're now ready to send WhatsApp messages programmatically using the Lazy platform and the "Send Message with WhatsApp API" template. Happy building!
Technologies
![Fast API](https://gitlab.com/uploads/-/system/project/avatar/37467835/fastapi.png)