File tree Expand file tree Collapse file tree 2 files changed +16
-21
lines changed
whatsapp_api_webhook_server_python Expand file tree Collapse file tree 2 files changed +16
-21
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 11import tornado .ioloop
22import tornado .web
33
4- from whatsapp_api_webhook_server_python .webhooks import Webhooks
4+ from .webhooks import handle_notification
55
66
77class 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
3427class Application (tornado .web .Application ):
You can’t perform that action at this time.
0 commit comments