Amazon Lex SDK可以使用AWS Lambda集成,在Lambda函数中可以定义多个命令。以下是一个通过使用AWS Lambda函数集成Amazon Lex SDK来定义多个命令的示例:
import boto3
import json
def dispatch(intent_request):
intent_name = intent_request['currentIntent']['name']
if intent_name == 'GetWeather':
return get_weather(intent_request)
elif intent_name == 'BookFlight':
return book_flight(intent_request)
else:
return close(intent_request)
def get_weather(intent_request):
# 获取用户意图槽位值
location = intent_request['currentIntent']['slots']['Location']
# 根据用户输入的地点,查询相应天气并返回
return {
"sessionAttributes": {},
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": f"The weather in {location} is sunny today."
}
}
}
def book_flight(intent_request):
# 获取用户意图槽位值
destination = intent_request['currentIntent']['slots']['Destination']
departure_date = intent_request['currentIntent']['slots']['DepartureDate']
# 根据用户输入的信息,发起预定机票操作并返回
return {
"sessionAttributes": {},
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": f"Your flight to {destination} on {departure_date} has been booked."
}
}
}
def close(intent_request):
# 无法识别用户意图时,结束对话
return {
"sessionAttributes": {},
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": "Sorry, I don't understand what you mean."
}
}
}
def lambda_handler(event, context):
# 根据请求类型,分发请求给不同