简体   繁体   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?

I'm trying to create an Alexa Skill, and I've hit a roadblock.我正在尝试创建 Alexa 技能,但遇到了障碍。 I've written/edited the AWS Lambda code and trying to test it.我已经编写/编辑了 AWS Lambda 代码并尝试对其进行测试。 Now, I've added "Oreo cake" as one of my slot values.现在,我添加了“奥利奥蛋糕”作为我的槽值之一。 When I utter/type oreo cake, for some reason, the if statement, which is supposed to run, doesn't.当我说出/输入奥利奥蛋糕时,出于某种原因,应该运行的 if 语句却没有。 Instead, the else statement runs.相反,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? ';
      }

I might be mistaken, but can't you do your if statement like so:我可能弄错了,但你不能像这样做你的 if 语句:

if (slotValues.cake)

Basically, if .cake is true, ie if the slotValues contains any value in the .cake object, it should run the IF statement?基本上,如果 .cake 为真,即如果 slotValues 包含 .cake 对象中的任何值,它应该运行 IF 语句吗?

The code to get the slot value and your logic doesn't look quite right.获取槽值的代码和您的逻辑看起来不太正确。

To get a slot value, with the latest version of the v2 SDK (which it looks like you are using).要获取插槽值,请使用最新版本的 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...
}

You should be able to use and tweak this for your needs.您应该能够根据自己的需要使用和调整它。

BTW, you could also get slot values like this:顺便说一句,您还可以获得这样的插槽值:

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

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

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

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