【问题标题】:Unable to process alexa intent无法处理 alexa 意图
【发布时间】:2019-09-23 15:57:49
【问题描述】:

我的代码没有运行,谁能帮忙。 无法说出文本,我可以返回处理程序输入响应吗?测试函数是一个http调用,可能需要时间。

function test(url, number)
{
    return 5;
}

function speak(handlerInput) {
    return handlerInput.responseBuilder
        .getResponse();
}

const NumberFactIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'NumberFactIntent';
    },

    handle(handlerInput) {

    const theNumber = handlerInput.requestEnvelope.request.intent.slots.number.value;
    const repromptOutput = " Would you like another fact?";
    const URL = "http://numbersapi.com";

    test(URL, theNumber).then(function (data) {
             console.log("data is " + data);
             handlerInput.responseBuilder
            .speak("Test Data")
            .reprompt(repromptOutput) 

             return speak(handlerInput);
        }).catch(function (data) {

             console.log("error is " + data);
             handlerInput.responseBuilder
            .speak(`I wasn't able to find a fact for ${theNumber}` )
            .reprompt(repromptOutput)
             return speak(handlerInput);
        }); 
    }
};

【问题讨论】:

  • "测试函数是一个可能需要时间的 http 调用" - http 调用需要多长时间。 Alexa 将在 8 秒后超时,因此您需要在此之前返回响应。
  • 如果您将代码部署到 AWS Lambda,我还建议您检查 CloudWatch 日志是否有任何错误。

标签: node.js promise alexa alexa-app alexa-sdk-nodejs


【解决方案1】:

首先,您的test 函数不会返回承诺。我不知道这是否是故意的,你只是削减了 api 调用代码以使其更简单,但如果你想在其上使用then,它应该返回一个承诺。

如果它在您的完整示例中返回一个承诺,那么您缺少的是在 test 之前添加一个返回。你也应该从你的承诺中返回handlerInput。代码应该是这样的(我会删除一些不相关的代码):

const NumberFactIntentHandler = {
    canHandle(handlerInput) {},

    handle(handlerInput) {

    const repromptOutput = " Would you like another fact?";
    const URL = "http://numbersapi.com";

    return test(URL, theNumber).then(function (data) {
             return handlerInput.responseBuilder
                .speak("Test Data")
                .reprompt(repromptOutput) 
        }).catch(function (data) {
             return handlerInput.responseBuilder
                 .speak(`I wasn't able to find a fact for ${theNumber}` )
                 .reprompt(repromptOutput)
        }); 
    }
};

现在您可能想知道为什么需要那些return。这是因为 JS 函数隐式返回 undefined,所以在这种情况下你必须明确告诉 handle 函数应该返回什么。同样适用于 promise 的内部。

【讨论】:

  • “现在,如果你仍然想使用 Promise,你可以。在你的情况下,你缺少的是在测试之前添加一个 return。” - 我认为你应该更清楚地表明这可能是这里的答案。我认为在关于 Promises 的解释中有点迷失了。此外,他们在代码示例中的 test 函数不会返回 Promise,因此 .then 不会工作 - 也许还可以明确指出该函数需要返回 Promise(添加 async 当然会工作)。
  • 感谢您的反馈。昨天写了一个答案,我得意忘形了。用新鲜的眼光看它,我发现答案的含义可能会丢失。我根据你的评论更新了我的答案。
【解决方案2】:

此代码可能会对您有所帮助!!

 //use request for http call
 function fun(url) {
      return new Promise((resolve, reject) => {
       request.get(url,(err, res, body) => {
       console.log("url-fetched");
       return resolve(body);
     });
   });
  }

   const NumberFactIntentHandler = {
      canHandle(handlerInput) {..
      },

 async handle(handlerInput) {

   const theNumber =handlerInput.requestEnvelope.request.intent.slots.number.value;
   const repromptOutput = " Would you like another fact?";
   const URL = "http://numbersapi.com";
   let data = await fun(url);

   return handlerInput.responseBuilder
    .speak(data)
    .reprompt('is there any thing i can do for you?')
    .withSimpleCard('Hello', speechText)
    .getResponse();
  };

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多