Skip to content

Commit 45c8689

Browse files
committed
Fixed notification handling
1 parent 693d208 commit 45c8689

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import json
1+
from json import JSONDecodeError, loads
2+
from typing import Any, Callable, Optional
23

34

4-
class Webhooks():
5+
def handle_notification(
6+
notification: Optional[bytes],
7+
notification_handler: Callable[[str, dict], Any]
8+
) -> None:
9+
try:
10+
data = loads(notification)
11+
except JSONDecodeError:
12+
return None
513

6-
def webhookProccessing(dataText, onEvent):
7-
try:
8-
data = json.loads(dataText)
9-
except Exception as error:
10-
print(error)
14+
webhook_type = data["typeWebhook"]
1115

12-
return
13-
typeWebhook = data['typeWebhook']
14-
onEvent(typeWebhook, data)
16+
notification_handler(webhook_type, data)

whatsapp_api_webhook_server_python/webhooksHandler.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
import tornado.ioloop
22
import tornado.web
33

4-
from whatsapp_api_webhook_server_python.webhooks import Webhooks
4+
from .webhooks import handle_notification
55

66

77
class WebhooksHandler(tornado.web.RequestHandler):
8-
98
def get(self):
109
length = self.request.headers.get('Content-Length')
1110
if length is not None:
12-
dataBytes = self.request.body
13-
dataText = dataBytes.decode("utf-8", 'ignore')
14-
Webhooks.webhookProccessing(dataText, self.onEvent)
11+
handle_notification(self.request.body, self.onEvent)
1512
else:
1613
self.set_status(200)
1714
self.set_header('Content-type', 'text/html')
1815

1916
def post(self):
2017
length = self.request.headers.get('Content-Length')
2118
if length is not None:
22-
dataBytes = self.request.body
23-
dataText = dataBytes.decode("utf-8", 'ignore')
24-
Webhooks.webhookProccessing(dataText, self.onEvent)
19+
handle_notification(self.request.body, self.onEvent)
2520

2621
def delete(self):
2722
length = self.request.headers.get('Content-Length')
2823
if length is not None:
29-
dataBytes = self.request.body
30-
dataText = dataBytes.decode("utf-8", 'ignore')
31-
Webhooks.webhookProccessing(dataText, self.onEvent)
24+
handle_notification(self.request.body, self.onEvent)
3225

3326

3427
class Application(tornado.web.Application):

0 commit comments

Comments
 (0)