如何让这个alexa技能识别两个意图
Posted
技术标签:
【中文标题】如何让这个alexa技能识别两个意图【英文标题】:How to make this alexa skill recognize both Intents 【发布时间】:2020-08-27 23:02:04 【问题描述】:这是我的第一个 Alexa 技能,我只是想了解这里的基本工作流程。
在下面的代码中,如果你使用FoodPointsIntent
它可以工作,但TestIntent
只是返回(并说)“触发的TestIntent”。我很困惑,因为它们是同一个东西,只是叫法不同。
Index.js
const Alexa = require('ask-sdk-core');
const LaunchRequestHandler =
canHandle(handlerInput)
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
,
handle(handlerInput)
const speakOutput = 'Welcome to food points! What food would you like to know about?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
;
const FoodPointsIntentHandler =
canHandle(handlerInput)
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'FoodPointsIntent';
,
handle(handlerInput)
console.log("THIS.EVENT = " + JSON.stringify(this.event));
var speakOutput = 'Sorry, there was an error';
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
;
const TestIntentHandler =
canHandle(handlerInput)
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'TestIntent';
,
handle(handlerInput)
console.log("THIS.EVENT = " + JSON.stringify(this.event));
var speakOutput = 'Sorry, there was an error';
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
;
/****************************REMEMBER TO UPDATE THIS*************************/
const HelpIntentHandler =
canHandle(handlerInput)
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
,
handle(handlerInput)
const speakOutput = 'You can say hello to me! How can I help?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
;
const CancelAndStopIntentHandler =
canHandle(handlerInput)
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
,
handle(handlerInput)
const speakOutput = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
;
const SessionEndedRequestHandler =
canHandle(handlerInput)
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
,
handle(handlerInput)
// Any cleanup logic goes here.
return handlerInput.responseBuilder.getResponse();
;
const IntentReflectorHandler =
canHandle(handlerInput)
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
,
handle(handlerInput)
const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
const speakOutput = `You just triggered $intentName`;
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
;
const ErrorHandler =
canHandle()
return true;
,
handle(handlerInput, error)
console.log(`~~~~ Error handled: $error.stack`);
const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
;
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
FoodPointsIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
)
.addErrorHandlers(
ErrorHandler,
)
.lambda();
IntenSchema.json
"interactionModel":
"languageModel":
"invocationName": "food points",
"intents": [
"name": "FoodPointsIntent",
"slots": [
"name": "FoodQuery",
"type": "AMAZON.Food"
],
"samples": ["tell me about FoodQuery"]
,
"name": "TestIntent",
"slots": [
"name": "TestQuery",
"type": "AMAZON.Food"
],
"samples": ["lets try TestQuery"]
,
"name": "AMAZON.YesIntent",
"slots": [],
"samples": []
,
"name": "AMAZON.NoIntent",
"slots": [],
"samples": []
,
"name": "AMAZON.HelpIntent",
"slots": [],
"samples": []
,
"name": "AMAZON.StopIntent",
"slots": [],
"samples": []
,
"name": "AMAZON.CancelIntent",
"slots": [],
"samples": []
,
"name": "AMAZON.NavigateHomeIntent",
"samples": []
],
"types": []
【问题讨论】:
【参考方案1】:您应该在导出中添加TestIntentHandler
以便可以访问它。导出代码如下。
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
TestIntentHandler,
FoodPointsIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
)
.addErrorHandlers(
ErrorHandler,
)
.lambda();
希望这行得通!
【讨论】:
只有请求处理程序的导出中包含的意图才会在代码中到达。检查列表是“它是否在请求处理程序中列出”,然后是“canHandle 是否为真”。还需要注意的是,意图处理程序是按照导出中列出的顺序进行尝试的。这就是为什么与您的 ErrorHandler 类似,您可以有一个 Fallback 或在底部有一个更开放的 canHandle 的处理程序作为包罗万象的原因。以上是关于如何让这个alexa技能识别两个意图的主要内容,如果未能解决你的问题,请参考以下文章