简体   繁体   中英

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. I've written/edited the AWS Lambda code and trying to test it. 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. Instead, the else statement runs.

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 (slotValues.cake)

Basically, if .cake is true, ie if the slotValues contains any value in the .cake object, it should run the IF statement?

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).

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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