我现在正在构建 AWS Lex 聊天机器人,并在 lambda 函数设置上遇到了一些问题。根据示例代码,它在对话结束时使用了这个 lambda 函数。这就是为什么代码是这样的:function close(.....)
'use strict';
// Close dialog with the customer, reporting fulfillmentState of Failed
or Fulfilled ("Thanks, your pizza will arrive in 20 minutes")
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
};
}
但是我想做的是使用 DialogCodeHook 而不是这个 FulfillmentCodeHook。
Lex 内部最简单的逻辑是问问题 1-->得到答案 1-->问问题 2-->得到答案 2-->问问题 2-->得到答案 3;我想做的是问问题1-允许的响应值是1.1、1.2如果响应值=值1.1问问题2如果响应值=值1.2问问题3问问题4-值4.1、值4.2 ..等等
在 AWS 论坛上,一个答案是这样的:是的,您可以使用 Lambda 来实现决策树。Lambda 允许您设置特定消息并使用“dialogAction”引出一个槽。
对于这个特定的对话流程
if (response_value = 1.1) {
// set dialogAction.message = "Question 2"
...
// set type = ElicitSlot
...
// slotToElicit = answer2"
}
同样,您可以定义条件来询问问题 3、4 等。
但我不确定我应该把这个 If..... 放在哪里以及如何使用这个 ElicitSlot 函数。
close 函数的完整版示例代码为:
'use strict';
// Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled ("Thanks, your pizza will arrive in 20 minutes")
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
};
}
// --------------- Events -----------------------
function dispatch(intentRequest, callback) {
console.log('request received for userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.intentName}');
const sessionAttributes = intentRequest.sessionAttributes;
const slots = intentRequest.currentIntent.slots;
const crust = slots.crust;
const size = slots.size;
const pizzaKind = slots.pizzaKind;
callback(close(sessionAttributes, 'Fulfilled',
{'contentType': 'PlainText', 'content': `Okay, I have ordered your ${size} ${pizzaKind} pizza on ${crust} crust`}));
}
// --------------- Main handler -----------------------
// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
try {
dispatch(event,
(response) => {
callback(null, response);
});
} catch (err) {
callback(err);
}
};
希望有人能帮忙!太感谢了!!!!!!!!!!!!!!!!!!!!!