要为Alexa技能添加意图,您可以创建自己的意图或使用Alexa技能默认意图。但是,如果您想要覆盖Alexa技能默认意图,例如取消意图或停止意图,该怎么办?
以下是如何使用Node.js SDK 2.0覆盖默认意图的示例代码:
const { SkillBuilders } = require('ask-sdk-core');
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speakOutput = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speakOutput)
.withSimpleCard('Hello World', speakOutput)
.getResponse();
},
};
const skillBuilder = SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
CancelAndStopIntentHandler,
)
.lambda();
在上面的示例中,我们定义了一个名为“CancelAndStopIntentHandler”的对象,该对象处理Alexa默认意图“CancelIntent”和“StopIntent”。还定义了一个speakOutput消息,并使用responseBuilder将其返回到用户。最后,我们在skillBuilder上添加了该意图处理程序,并导出了lambda函数。
如上所示,您可以创建一个对象来处理您想要覆盖的默认意图,并将其添加到Alexa SDK的请求处理程序列表中。这将覆盖Alexa技能默认提供的意图处理程序。