Python 生成引出槽响应时出现问题
我想为某个插槽创建一个dialogHook,更好的说法是验证类型的东西。如果插槽返回true,那么只有我会启动我的elicit插槽,否则它会像往常一样。请帮助我的方法。我对lex比较陌生 我曾尝试在childExists上制作dialoghook,但它不起作用Python 生成引出槽响应时出现问题,python,amazon-web-services,aws-lambda,amazon-lex,Python,Amazon Web Services,Aws Lambda,Amazon Lex,我想为某个插槽创建一个dialogHook,更好的说法是验证类型的东西。如果插槽返回true,那么只有我会启动我的elicit插槽,否则它会像往常一样。请帮助我的方法。我对lex比较陌生 我曾尝试在childExists上制作dialoghook,但它不起作用 def lambda_handler(event,context): os.environ['TZ']='America/New_York' time.tzset(); logger.debug('event.bo
def lambda_handler(event,context):
os.environ['TZ']='America/New_York'
time.tzset();
logger.debug('event.bot.name={}'.format(event['bot']['name']))
return dispatch(event);
def dispatch(intent_request):
intent_name=intent_request['currentIntent']['name']
if intent_name=='HotelReservation':
return book_hotel(intent_request)
def book_hotel(intent_request):
slots=intent_request['currentIntent']['slots']
welcome=intent_request['currentIntent']['slots']['welcome']
location=intent_request['currentIntent']['slots']['Location']
fromDate=intent_request['currentIntent']['slots']['FromDate']
adultCount=intent_request['currentIntent']['slots']['adultCount']
nights=intent_request['currentIntent']['slots']['nights']
childExists=intent_request['currentIntent']['slots']['childExists']
source=intent_request['invocationSource']
session_attributes={}
if source=='DialogCodeHook'and childExists.lower()=='yes':
session_attributes={}
return elicit_slot (
session_attributes,
'HotelReservation',
'childCount',
'AMAZON.NUMBER',
{
'contentType':'PlainText',
'content':'Please enter number of Children'
}
)
elif source=='DialogCodeHook':
output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
return delegate(output_session_attributes, intent_request['currentIntent']['slots'])
else:
return close (
session_attributes,
'Fulfilled',{
'contentType':'PlainText',
'content':'Here is the temparature in'
}
)
#for fullfillment function
def close(session_attributes,fulfillment_state,message):
response={
'sessionAttributes':session_attributes,
'dialogAction':{
'type':'Close',
'fulfillmentState':fulfillment_state,
'message':message
}
}
return response
#for elicit slot return
def elicit_slot(session_attributes, intent_name,slots,slot_to_elicit,message):
response= {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'ElicitSlot',
'intentName': intent_name,
'slots': slots,
'slotToElicit': slot_to_elicit,
'message': message
}
}
return response;
def delegate(session_attributes, slots):
return {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'Delegate',
'slots': slots
}
}
实际上,我的插槽应该像往常一样运行,但在存在childslot之后,我想发送一个elicit响应
根据我的理解,您正在询问用户
您是否有子女
,并将响应存储在childExists
插槽中,如果回答是是,则您需要询问子女数量
因此,根据我的说法,您需要有一个额外的槽childCount
来存储孩子的数量。由于此插槽并非总是必需的,不要在amazon lex控制台中标记此必需的
现在,您将在对话框codehook
中对此进行检查,并仅当childExists==“yes”
中没有值时才相应地询问用户。我们使用这些条件的组合是为了确保它不会无限期地运行
def book_hotel(intent_request):
slots = intent_request['currentIntent']['slots']
welcome = slots['welcome']
location = slots['Location']
fromDate = slots['FromDate']
adultCount = slots['adultCount']
nights = slots['nights']
childExists = slots['childExists']
childCount = slots['childCount']
source = intent_request['invocationSource']
if source == 'DialogCodeHook':
output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
if childExists.lower() == 'yes':
if not childCount:
return elicit_slot (
output_session_attributes,
'HotelReservation',
slots,
'childCount',
{
'contentType':'PlainText',
'content':'Please enter number of Children'
}
)
return delegate(output_session_attributes, intent_request['currentIntent']['slots'])
if source == 'FulfillmentCodeHook':
return close (
output_session_attributes,
'Fulfilled',{
'contentType':'PlainText',
'content':'Here is the temparature in'
}
)
希望有帮助。请仔细检查调用的
elicit\u slot
函数,因为您的参数不匹配。您确实需要通过插槽
,而不需要“AMAZON.NUMBER”
。另外,您正试图在intentHotelReservation
中获取插槽childCount
,但您的图像显示您在该intent中没有该插槽。但是我如何才能获得正常的插槽流,并且只有当childExist到来时才启动lambda并感谢您的帮助返回elicit_插槽(session_属性,'HotelReservation',slot,'childCount',{'contentType':'PlainText','content':'Please enter of Children'})尽管如此,我还是得到了错误响应,这更好,但它可能会出错,因为您还没有创建childCount
作为插槽。顺便说一句,您的Lambda是用每个用户输入初始化的,您得到的是“正常流”感谢您的Lambda返回委托
,这将引出下一个所需的插槽。这就是@sid8491的答案告诉您创建插槽而不是按要求标记插槽的原因。他的答案应该可以做到这一点。您好,感谢您的帮助,我已经在我的lex控制台中创建了childCount插槽,但我仍然收到以下错误:发生了错误ed:无效的Lambda响应:收到来自Lambda的错误响应:未处理我还有一张与欢迎槽相关联的响应卡,其中要求用户提供插槽类型,如您想预订酒店吗?插槽类型的按钮选项中的是或否我甚至还未选中欢迎槽以及childCount。我仍在使用g这个error@GourabKonar可能会发生很多错误,这很难说。最好将记录器放在代码中,并在中观察它们cloudwatch@GourabKonar您已选中标记并将lambda函数放入初始化和验证中
?在填充所有插槽后,将调用lamda函数标记为必需,所以您的欢迎槽没有问题我猜是的,我已经检查了初始化和验证