DaR*_*eal 4 alexa node.js alexa-skill alexa-skills-kit alexa-sdk-nodejs
我正在尝试将自己的响应添加到自定义意图中。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": []
}
}
}
Run Code Online (Sandbox Code Playgroud)
index.js(来自nodejs教程的自适应示例,位于此处:https : //github.com/alexa/skill-sample-nodejs-fact/blob/en-US/lambda/custom/index.js 我添加了CurrentBPM函数并将其添加到底部的addRequestHandlers中,它看起来要匹配的意图名称是上面列表中的currentbpm意图。
/* 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();
Run Code Online (Sandbox Code Playgroud)
当我调用该技能时:“ Alexa开始我的个人心跳。” 它确实会说出脚本中的欢迎词。但是,当我问“现在我的心跳多快”时,它只会回答“对不起,不确定”,而不是说出硬编码的回答。
解决方案是在LaunchRequest中的响应中添加一行:
.withShouldEndSession(false)
Run Code Online (Sandbox Code Playgroud)
如果不添加,则默认设置为true,因此该技能将在给出第一个响应(欢迎意图)后立即结束。请参阅文档:https : //ask-sdk-for-nodejs.readthedocs.io/en/latest/Response-Building.html
多亏了Suneet Patil,我才相应地更新了脚本(见下文)。
但是我无法达到目的:
使用以下新脚本:
/* 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();
Run Code Online (Sandbox Code Playgroud)
现在可以使用:
| 归档时间: |
|
| 查看次数: |
1577 次 |
| 最近记录: |