11import json
22try :
33 from .src .module import chat_module
4- from .src .agents .utils .types import JsonType
54except ImportError :
65 from src .module import chat_module
7- from src .agents .utils .types import JsonType
86
9- def handler (event : JsonType , context ):
7+ def handler (event , context ):
108 """
119 Lambda handler function
12- Args:
13- event (JsonType): The AWS Lambda event received by the gateway.
14- context (Any): The AWS Lambda context object.
15-
1610 """
1711 # Log the input event for debugging purposes
18- print ("Received event:" , json .dumps (event , indent = 2 ))
12+ # print("Received event:", " ".join( json.dumps(event, indent=2).splitlines() ))
1913
20- if "body" not in event :
21- return {
22- "statusCode" : 400 ,
23- "body" : "Missing 'body' key in event. Please confirm the key in the json body."
24- }
25- body = json .loads (event ["body" ])
26-
27- if "message" not in body :
14+ if "body" in event :
15+ try :
16+ event = json .loads (event ["body" ])
17+ except json .JSONDecodeError :
18+ return {
19+ "statusCode" : 400 ,
20+ "body" : "Invalid JSON format in the body or body not found. Please check the input."
21+ }
22+
23+ if "message" not in event :
2824 return {
2925 "statusCode" : 400 ,
3026 "body" : "Missing 'message' key in event. Please confirm the key in the json body."
3127 }
32- if "params" not in body :
28+ if "params" not in event :
3329 return {
3430 "statusCode" : 400 ,
3531 "body" : "Missing 'params' key in event. Please confirm the key in the json body. Make sure it contains the necessary conversation_id."
3632 }
3733
38- message = body [ "message" ]
39- params = body [ "params" ]
34+ message = event . get ( "message" , None )
35+ params = event . get ( "params" , None )
4036
4137 try :
4238 chatbot_response = chat_module (message , params )
@@ -49,7 +45,10 @@ def handler(event: JsonType, context):
4945 # Create a response
5046 response = {
5147 "statusCode" : 200 ,
52- "body" : json . dumps ( chatbot_response )
48+ "body" : chatbot_response
5349 }
5450
51+ # Log the response for debugging purposes
52+ print ("Returning response:" , " " .join (json .dumps (response , indent = 2 ).splitlines ()))
53+
5554 return response
0 commit comments