Setup and authentication
Use the requests library for HTTP calls to Chat API. Create a wrapper client that handles authentication, automatic retries and call logging.
With Django, create a ChatAPIClient class in your app's services.py file. Use Django settings for credentials configuration.
Sending messages with Python
The base function for sending a template message accepts the recipient number, template name, language and parameters. Handle error cases: number not on WhatsApp, template not approved, rate limit reached.
For async tasks in Django, use Celery to queue message sends.
Webhooks with Django
Create a Django view to receive Chat API webhooks. Use csrf_exempt since webhooks don't have CSRF tokens. Verify the webhook signature before processing. Process webhooks asynchronously with Celery.
Testing and local development
To test webhooks locally, use ngrok to expose the Django port to a public URL.
Write unit tests that mock Chat API calls using unittest.mock.