From 214dd2455777abc9b7fce7424812443f6df4b9d7 Mon Sep 17 00:00:00 2001 From: Timothy Makobu Date: Thu, 4 Sep 2025 11:53:49 +0300 Subject: [PATCH 1/2] added a Django example to README.md --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index effd7d0a..a6ad3f81 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,37 @@ def handle_flow_response(_: WhatsApp, flow: types.FlowCompletion): ) ``` +- **Django example** + +```python +from django.http import HttpRequest, HttpResponse +from django.views.decorators.csrf import csrf_exempt +from django.views.decorators.http import require_http_methods + +from .service import WHATSAPP +# Attach handlers to the WHATSAPP instance using available methods +# https://pywa.readthedocs.io/en/latest/content/handlers/overview.html#registering-callback-functions + + +@csrf_exempt +@require_http_methods(request_method_list=["GET", "POST"]) +def whatsapp_endpoint(request: HttpRequest) -> HttpResponse: + if request.method == "GET": + _response = WHATSAPP.webhook_challenge_handler( + vt=request.GET.get("hub.verify_token", ""), + ch=request.GET.get("hub.challenge", ""), + ) + return HttpResponse(content=_response[0], status=_response[1]) + + elif request.method == "POST": + _response = WHATSAPP.webhook_update_handler( + update=request.body, + hmac_header=request.headers.get("X-Hub-Signature-256", ""), + ) + return HttpResponse(content=_response[0], status=_response[1]) + +``` + 🎛 **Installation** -------------------- From 9c8b2afe7e3778c8169f3aaed410601c9109568c Mon Sep 17 00:00:00 2001 From: Timothy Makobu Date: Thu, 4 Sep 2025 12:03:11 +0300 Subject: [PATCH 2/2] made a couple of things clearer --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a6ad3f81..78e486a4 100644 --- a/README.md +++ b/README.md @@ -297,8 +297,9 @@ from django.http import HttpRequest, HttpResponse from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_http_methods -from .service import WHATSAPP -# Attach handlers to the WHATSAPP instance using available methods +from .service.whatsapp import WHATSAPP +# WHATSAPP = WhatsApp(...) +# Attach handlers to the WhatsApp instance using available methods # https://pywa.readthedocs.io/en/latest/content/handlers/overview.html#registering-callback-functions