首页 > 解决方案 > Alexa Nodejs Skill - 第二个意图询问总是失败

问题描述

我已经开始使用 CodeStar 和 Nodejs 制作一个简单的 Alexa 技能,我的技能可以告知用户 Web 系统当前是否仍然可用,是否有任何当前事件或服务中断。我从http://statuspage.io以 JSON API 的形式获取所有数据。

我遇到的问题是,我的LaunchRequestHandler工作正常,我问的第一个 Intent 也是如此,但是当我问第二个 Intent(紧接在第一个 Intent 之后)时,这就是我的技能中断和输出的地方(在 Alexa Developer控制台)<Audio only response>:。

Alexa 开发者控制台截图

下面我粘贴了我的技能中的代码。

// model/en-GB.json

"intents": [{
    "name": "QuickStatusIntent",
    "slots": [],
    "samples": [
        "quick status",
        "current status",
        "tell me the current status",
        "what's the current status",
        "tell me the quick status",
        "what's the quick status"
    ]
},
{
    "name": "CurrentIncidentIntent",
    "slots": [],
    "samples": [
        "incidents",
        "current incident",
        "is there a current incident",
        "tell me the current incidents",
        "what are the current incidents",
        "are there any current incidents"
    ]
}]


// lambda/custom/index.js

const QuickStatusIntentHandler = {
    canHandle(handlerInput) {
      return handlerInput.requestEnvelope.request.type === 'IntentRequest'
        && handlerInput.requestEnvelope.request.intent.name === 'QuickStatusIntent';
    },
    async handle(handlerInput) {
      let speechText = await getNetlifyStatus();
      return handlerInput.responseBuilder
        .speak(speechText)
        .getResponse();
    }
};

const CurrentIncidentIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'CurrentIncidentIntent';
  },
  async handle(handlerInput) {
    const result = await getSummary();
    const speechText = `There are currently ${result.incidents.length} system incidents`;
    return handlerInput.responseBuilder
      .speak(speechText)
      .getResponse();
  }
}

我最初的想法是删除.getResponse()可能一直在等待响应的,但是,这似乎无法正常工作。

标签: javascriptnode.jsalexaalexa-skills-kit

解决方案


问题是您的CurrentIncidentIntentHandler在向用户提供响应后立即关闭会话。

如果您想保持会话打开并让用户进行对话,请使用.withShouldEndSession(false).repeat(speechText)在您的CurrentIncidentIntentHandler中让 Alexa 等待用户的下一个响应。

const CurrentIncidentIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'CurrentIncidentIntent';
  },
  async handle(handlerInput) {
    const result = await getSummary();
    const speechText = `There are currently ${result.incidents.length} system incidents`;
    return handlerInput.responseBuilder
      .speak(speechText)
      .withShouldEndSession(false) //you can also use .repeat(speachText) at this line
      .getResponse();
  }
}


推荐阅读