繁体   English   中英

为什么我的代码没有在 Alexa 开发控制台模拟器上运行 if 语句,即使我说出/输入我的插槽中存在的值?

[英]Why is my code not running the if statement on the Alexa Development Console simulator, even if I utter/type the values that are present in my slot?

我正在尝试创建 Alexa 技能,但遇到了障碍。 我已经编写/编辑了 AWS Lambda 代码并尝试对其进行测试。 现在,我添加了“奥利奥蛋糕”作为我的槽值之一。 当我说出/输入奥利奥蛋糕时,出于某种原因,应该运行的 if 语句却没有。 相反,else 语句会运行。

const GetPrice_Handler = {
    canHandle(handlerInput) {
      const request = handlerInput.requestEnvelope.request;
      return request.type === 'IntentRequest' && request.intent.name === 'GetPrice';
    },
    handle(handlerInput) {
      const request = handlerInput.requestEnvelope.request;
      const responseBuilder = handlerInput.responseBuilder;
      let sessionAttributes = handlerInput.attributesManager.getSessionAttributes();

      let slotStatus = '';
      let resolvedSlot;
      let say = '';

      let slotValues = getSlotValues(request.intent.slots);
      // getSlotValues returns .heardAs, .resolved, and .isValidated for each slot, 
      // according to request slot status codes ER_SUCCESS_MATCH, ER_SUCCESS_NO_MATCH, 
      // or traditional simple request slot without resolutions
      if (slotValues == slotValues.cake) {
        say = `The price of ${slotValues.cake.heardAs} is 800 Indian Rupees. `;
      } else {
        say = 'Sorry, I didnt catch that. Could you please repeat that again? ';
      }

我可能弄错了,但你不能像这样做你的 if 语句:

if (slotValues.cake)

基本上,如果 .cake 为真,即如果 slotValues 包含 .cake 对象中的任何值,它应该运行 IF 语句吗?

获取槽值的代码和您的逻辑看起来不太正确。

要获取插槽值,请使用最新版本的 v2 SDK(看起来您正在使用)。

// Require the SDK at the top of your file

const Alexa = require('ask-sdk');

// Then in your handle function where you want the slot value

handle (handlerInput) {
  const { requestEnvelope } = handlerInput;
  const cakeSlotValue = Alexa.getSlotValue(requestEnvelope, 'cake'); // 'cake' slot name
  let say = '';

  if (cakeSlotValue) {
    say = `The price of ${cakeSlotValue} is 800 Indian Rupees.`;
  } else {
    say = 'Sorry, I didnt catch that. Could you please repeat that again? ';
  }

  // Do the rest of your stuff here...
}

您应该能够根据自己的需要使用和调整它。

顺便说一句,您还可以获得这样的插槽值:

const cake = handlerInput.requestEnvelope.request.intent.slots['cake'].value

请求信封实用程序 - ASK SDK v2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM