Alexa Node.js技能-要求第二意图总是失败

时间:2019-02-19 14:56:00

标签: javascript node.js alexa alexa-skills-kit

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

我遇到的问题是,我的LaunchRequestHandler正常工作,并且我问的第一个意图也是如此,但是当我问第二个意图时(紧接在第一个意图之后),这就是我的技能所在中断和输出(在Alexa开发人员控制台内部):<Audio only response>

Alexa Developer Console Screenshot

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

// 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(),因为它可能一直在等待响应,但是,它似乎无法正常工作。

1 个答案:

答案 0 :(得分:0)

问题是您的 CurrentIncidentIntentHandler 在将响应传递给用户后立即关闭了会话。

如果您要保持会话打开并允许用户进行对话,请使用 CurrentIncidentIntentHandler 中的.withShouldEndSession(false).repeat(speechText) 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();
  }
}