Created
January 3, 2023 15:26
-
-
Save skolo-online/0eb35837ef73a9e58cec764c4f8bbab8 to your computer and use it in GitHub Desktop.
Receive WhatsApp Message via webhook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.views.decorators.csrf import csrf_exempt | |
from django.http import HttpResponse | |
import json | |
@csrf_exempt | |
def whatsAppWebhook(request): | |
if request.method == 'GET': | |
VERIFY_TOKEN = 'enter-your-verify-token-here' | |
mode = request.GET['hub.mode'] | |
token = request.GET['hub.verify_token'] | |
challenge = request.GET['hub.challenge'] | |
if mode == 'subscribe' and token == VERIFY_TOKEN: | |
return HttpResponse(challenge, status=200) | |
else: | |
return HttpResponse('error', status=403) | |
if request.method == 'POST': | |
data = json.loads(request.body) | |
if 'object' in data and 'entry' in data: | |
if data['object'] == 'whatsapp_business_account': | |
for entry in data['entry']: | |
phoneId = entry['changes'][0]['value']['metadata']['phone_number_id'] # | |
profileName = entry['changes'][0]['value']['contacts'][0]['profile']['name'] | |
whatsAppId = entry['changes'][0]['value']['contacts'][0]['wa_id'] | |
fromId = entry['changes'][0]['value']['messages'][0]['from'] # | |
text = entry['changes'][0]['value']['messages'][0]['text']['body'] # | |
handleWhatsAppChat(fromId, profileName, phoneId, text) | |
else: | |
pass | |
else: | |
pass | |
return HttpResponse('success', status=200) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment