alexa 无法识别意图

alexa not recognizing intent

我正在尝试将自己的响应添加到自定义意向。 LaunchRequest 文本有效,但除了 AMAZON.HelpIntent 和其他默认意图外,我自己的意图无法识别。

意图:

{
    "interactionModel": {
        "languageModel": {
            "invocationName": "my personal heartbeat",
            "intents": [
                {
                    "name": "AMAZON.FallbackIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": []
                },
                {
                    "name": "start",
                    "slots": [],
                    "samples": [
                        "Talk to my personal heartbeat"
                    ]
                },

                {
                    "name": "currentbpm",
                    "slots": [],
                    "samples": [
                        "what's my current BPM",
                        "how fast is my heart beating right now",
                        "How many beats per minute is my heart making at the moment"
                    ]
                }

            ],
            "types": []
        }
    }
}

index.js(改编自此处找到的 nodejs 教程的示例:https://github.com/alexa/skill-sample-nodejs-fact/blob/en-US/lambda/custom/index.js 我添加了 CurrentBPM 函数并将其添加到底部的 addRequestHandlers 中。它看起来匹配的意图名称是上面列表中的当前 bpm 意图。

/* eslint-disable  func-names */
/* eslint-disable  no-console */

const Alexa = require('ask-sdk');

const GetNewFactHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'LaunchRequest'
      || (request.type === 'IntentRequest'
        && request.intent.name === 'GetNewFactIntent');
  },
  handle(handlerInput) {
    const speechOutput = "Welcome to your personal heart health monitor. What would you like to know?";

    return handlerInput.responseBuilder
      .speak(speechOutput)
      .withSimpleCard(speechOutput)
      .getResponse();
  },
};

const CurrentBPMHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && request.intent.name === 'currentbpm';
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak('seventy five bpm')
      .reprompt('seventy five bpm')
      .getResponse();
  },
};

const HelpHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && request.intent.name === 'AMAZON.HelpIntent';
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak(HELP_MESSAGE)
      .reprompt(HELP_REPROMPT)
      .getResponse();
  },
};

const ExitHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && (request.intent.name === 'AMAZON.CancelIntent'
        || request.intent.name === 'AMAZON.StopIntent');
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak(STOP_MESSAGE)
      .getResponse();
  },
};

const SessionEndedRequestHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'SessionEndedRequest';
  },
  handle(handlerInput) {
    console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);

    return handlerInput.responseBuilder.getResponse();
  },
};

const ErrorHandler = {
  canHandle() {
    return true;
  },
  handle(handlerInput, error) {
    console.log(`Error handled: ${error.message}`);

    return handlerInput.responseBuilder
      .speak('Sorry, an error occurred.')
      .reprompt('Sorry, an error occurred.')
      .getResponse();
  },
};

const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';


const skillBuilder = Alexa.SkillBuilders.standard();

exports.handler = skillBuilder
  .addRequestHandlers(
    GetNewFactHandler,
    CurrentBPMHandler,
    HelpHandler,
    ExitHandler,
    SessionEndedRequestHandler
  )
  .addErrorHandlers(ErrorHandler)
  .lambda();

当我调用技能时: 'Alexa start my personal heartbeat.' 它确实会说出脚本中的欢迎句。但是当我问 'how fast is my heart beating right now' 时,它只响应 'Sorry, not sure' 而不是说出硬编码的响应。

请进行以下更改使其生效。 1. 在交互模型中,对于 intent start 只给出与“to start”相同的语句而不是 current。 例如:Alexa,让我的个人心跳开始。

  1. 在您的 lambda 代码中,在 getnewfact 方法的 lambda 代码中,您忘记将意图名称从 getnewfactintent 更改为 start。

  2. 调用currentbpm意图,使用“Alexa,询问我的个人心跳,我现在的心跳有多快。

希望对您有所帮助。

解决方法是在 LaunchRequest 的响应中添加一行:

.withShouldEndSession(false)

如果不添加,默认设置为true,所以技能会在给出第一个响应(欢迎意图)后立即结束。 见文档:https://ask-sdk-for-nodejs.readthedocs.io/en/latest/Response-Building.html

感谢 Suneet Patil 我相应地更新了脚本(见下文) 起初只有这个有效:

  • 用户:'Alexa, ask my personal heartbeat how fast my heart is beating right now.'
  • Alexa:'75 bpm'

但是我无法达到目的:

  • 用户:'Alexa talk to my personal heartbeat'
  • Alexa:'Welcome to your personal heart health monitor. What would you like to know?'(默认退出技能
  • 用户:'How fast is my heart beating right now?'
  • Alexa:“抱歉,我不确定。”

使用以下新脚本:

/* eslint-disable  func-names */
/* eslint-disable  no-console */

const Alexa = require('ask-sdk');

const GetNewFactHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'LaunchRequest'
      || (request.type === 'IntentRequest'
        && request.intent.name === 'start');
  },
  handle(handlerInput) {
    const speechOutput = "Welcome to your personal heart health monitor. What would you like to know?";

    return handlerInput.responseBuilder
      .speak(speechOutput)
      .withSimpleCard(speechOutput)
      .withShouldEndSession(false)
      .getResponse();

  },
};

const CurrentBPMHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && request.intent.name === 'currentbpm';
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak('seventy five bpm')
      .reprompt('seventy five bpm')
      .getResponse();
  },
};


const HelpHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && request.intent.name === 'AMAZON.HelpIntent';
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak(HELP_MESSAGE)
      .reprompt(HELP_REPROMPT)
      .getResponse();
  },
};

const ExitHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && (request.intent.name === 'AMAZON.CancelIntent'
        || request.intent.name === 'AMAZON.StopIntent');
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak(STOP_MESSAGE)
      .getResponse();
  },
};

const SessionEndedRequestHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'SessionEndedRequest';
  },
  handle(handlerInput) {
    console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);

    return handlerInput.responseBuilder.getResponse();
  },
};

const ErrorHandler = {
  canHandle() {
    return true;
  },
  handle(handlerInput, error) {
    console.log(`Error handled: ${error.message}`);

    return handlerInput.responseBuilder
      .speak('Sorry, an error occurred.')
      .reprompt('Sorry, an error occurred.')
      .getResponse();
  },
};

const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';


const skillBuilder = Alexa.SkillBuilders.standard();

exports.handler = skillBuilder
  .addRequestHandlers(
    GetNewFactHandler,
    CurrentBPMHandler,
    HelpHandler,
    ExitHandler,
    SessionEndedRequestHandler
  )
  .addErrorHandlers(ErrorHandler)
  .lambda();

现在有效:

  • 用户:'Alexa talk to my personal heartbeat'
  • Alexa:'欢迎来到您的 个人心脏健康监测仪。你想知道什么?'
  • 用户:'How fast is my heart beating right now?'
  • Alexa:'seventy-five bpm.'(技能保持开放状态以提出另一个问题)

对于任何请求,如果未提供,shouldEndSession 默认为 true。在您的情况下,对 LaunchRequest 的响应将没有此 shouldEndSession 参数并且会话关闭。

尽管您总是可以使用 ask-nodejs-sdk 的 shouldEndSession(false) 来保持会话的活动,但您不必每次都专门将其设置为 false .相反,更好的方法是在 LaunchRequest 中使用 reprompt。如果您包含 reprompt(),则 SDK 会自动在您的响应中添加 "shouldEndSession": false

使用您现在的代码,您的 LaunchRequest 将等待 8 秒,如果没有用户响应,会话将关闭。但是,对于 CurrentBPMHandlerHelpHandler,您包含了一个重新提示,它将在 reprompt 之后再等待 8 秒。当您期望用户响应时,包含重新提示总是一个好主意。

您的交互模型定义了 AMAZON.FallbackIntent 意图,但您尚未在代码中处理它。 AMAZON.FallbackIntent 可帮助您处理意外的话语,或者当用户说出与您技能中的任何意图不符的内容时。如果没有处理程序,那么它将在您的错误处理程序中被捕获。将其作为错误处理没有问题,但更好的方法是专门为此添加一个处理程序并给出类似 "Sorry, I didn't understand, can you please rephrase your question" 或类似内容的响应。