forked from neo09sumedh/AI_ChatBot_Resource_Utilization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIToLexHandler.py
More file actions
39 lines (33 loc) · 1.04 KB
/
APIToLexHandler.py
File metadata and controls
39 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import json
import boto3
# Initialize AWS Lex client
lex = boto3.client("lexv2-runtime")
# Lex Bot Details
LEX_BOT_ID = "2C5KLYWSCK"
LEX_ALIAS_ID = "TSTALIASID"
def lambda_handler(event, context):
try:
print("Event received: ", json.dumps(event))
# Step 1: Parse incoming request
body = json.loads(event["body"]) if "body" in event else event
user_message = body.get("message", "")
sessionId=body.get("sessionId", "")
# Send user input to Lex
lex_response = lex.recognize_text(
botId=LEX_BOT_ID,
botAliasId=LEX_ALIAS_ID,
localeId="en_US",
#sessionId="user-session",
sessionId=sessionId,
text=user_message
)
# Return Lex's response back to API Gateway
return {
"statusCode": 200,
"body": json.dumps({"LexResponse": lex_response})
}
except Exception as e:
return {
"statusCode": 500,
"body": json.dumps({"error": str(e)})
}