簡體   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