Skip to content

Commit

Permalink
Fixed notification handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Amele9 committed Jan 13, 2024
1 parent 693d208 commit 45c8689
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
22 changes: 12 additions & 10 deletions whatsapp_api_webhook_server_python/webhooks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import json
from json import JSONDecodeError, loads
from typing import Any, Callable, Optional


class Webhooks():
def handle_notification(
notification: Optional[bytes],
notification_handler: Callable[[str, dict], Any]
) -> None:
try:
data = loads(notification)
except JSONDecodeError:
return None

def webhookProccessing(dataText, onEvent):
try:
data = json.loads(dataText)
except Exception as error:
print(error)
webhook_type = data["typeWebhook"]

return
typeWebhook = data['typeWebhook']
onEvent(typeWebhook, data)
notification_handler(webhook_type, data)
15 changes: 4 additions & 11 deletions whatsapp_api_webhook_server_python/webhooksHandler.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import tornado.ioloop
import tornado.web

from whatsapp_api_webhook_server_python.webhooks import Webhooks
from .webhooks import handle_notification


class WebhooksHandler(tornado.web.RequestHandler):

def get(self):
length = self.request.headers.get('Content-Length')
if length is not None:
dataBytes = self.request.body
dataText = dataBytes.decode("utf-8", 'ignore')
Webhooks.webhookProccessing(dataText, self.onEvent)
handle_notification(self.request.body, self.onEvent)
else:
self.set_status(200)
self.set_header('Content-type', 'text/html')

def post(self):
length = self.request.headers.get('Content-Length')
if length is not None:
dataBytes = self.request.body
dataText = dataBytes.decode("utf-8", 'ignore')
Webhooks.webhookProccessing(dataText, self.onEvent)
handle_notification(self.request.body, self.onEvent)

def delete(self):
length = self.request.headers.get('Content-Length')
if length is not None:
dataBytes = self.request.body
dataText = dataBytes.decode("utf-8", 'ignore')
Webhooks.webhookProccessing(dataText, self.onEvent)
handle_notification(self.request.body, self.onEvent)


class Application(tornado.web.Application):
Expand Down

0 comments on commit 45c8689

Please sign in to comment.