首页 > 解决方案 > 为特定插槽提供插槽值作为响应并恢复对话

问题描述

我正在研究 lex 并希望在响应中给出槽值,只会询问用户是否在前一个槽值中输入特定输入。我正在尝试一些事情,但我不知道我做得对与否。

我在 lex 中有以下插槽。

  1. 出发城市
  2. 到达城市
  3. 出发(单程或往返)
  4. 归期
  5. 日期(出发日期)
  6. 航班时刻表

例如,如果用户选择往返然后询问返回日期,否则跳过该插槽并通过询问剩余插槽的值来继续流程

这是我为实现这种情况而正在做的一段代码。

"use strict";

const lexResponses = require("./lexResponse");

const depart = ["one-way", "oneway"];

const buildValidationResult = (isValid, violatedSlot, messageContent) => {
  if (messageContent == null) {
    return {
      isValid: isValid,
      violatedSlot: violatedSlot,
    };
  }
  return {
    isValid: isValid,
    violatedSlot: violatedSlot,
    message: { contentType: "PlainText", content: messageContent },
  };
};

function validateBookaflight(
  Departing,
  ReturnDate
) {
  if (Departing && depart.indexOf(Departing.toLowerCase()) === -1) {
   
     return {
          dialogAction: {
            type: "ElicitSlot",
            intentName: "Bookaflight",
            slots: {
              Departure_city: Departure_city,
              Arrival_city: Arrival_city,
              Departing: Departing,
              ReturnDate: ReturnDate,
            },
            slotToElicit: "ReturnDate",
            message: {
              contentType: "PlainText",
              content: "Please enter return date,(yyyy-mm-dd)",
            },
          },
        }
    };
     return buildValidationResult(true, null, null);
}

function buildFulfilmentResult(fullfilmentState, messageContent) {
  return {
    fullfilmentState,
    message: { contentType: "PlainText", content: messageContent },
  };
}

错误:

An error has occurred: Invalid Lambda 
Response: Received invalid response from 
Lambda: Can not construct instance of 
ElicitSlotDialogAction, problem: 
slotToElicit must not be blank in ElicitSlot 
dialog action at 
[Source: {"sessionAttributes":{},"dialogAction":{"type":"ElicitSlot","intentName":"Bookaflight",
"slots":{"ReturnDate":null,"Departure_city":"london","Flight_schedule":null,"Arrival_city":"lahore","Date":null,
"Departing":"roundtrip",
"undefined":null}}}; line: 1, column: 241]

在此处输入图像描述

请告诉我做错了什么,或者如果您在理解我的要求方面有任何问题。

标签: javascriptamazon-web-servicesamazon-lex

解决方案


您看到的问题似乎是由于slotToElicit某种原因未将参数返回给 Lex 造成的。要确认 Lambda 返回给 Lex 的内容,请尝试使用 Lex 机器人传递的相同输入运行函数的测试调用。

此外,在 Lambda 响应中返回槽的值时,如果您不返回其他槽的值,Lex 会将它们视为 null。因此确保返回的所有槽值都不是null并且包含用户输入的值。


推荐阅读