
本文介绍如何利用 alexa 内置的 amazon.yesintent 和 amazon.nointent 实现自然的多轮对话流程,无需复杂对话管理,仅通过 session attributes 维护上下文即可完成“欢迎→询问→确认→响应”的完整交互链。
本文介绍如何利用 alexa 内置的 amazon.yesintent 和 amazon.nointent 实现自然的多轮对话流程,无需复杂对话管理,仅通过 session attributes 维护上下文即可完成“欢迎→询问→确认→响应”的完整交互链。
在 Alexa 技能开发中,实现“用户回答‘是’后自动执行对应操作”并不需要依赖 ConfirmIntentDirective 或手动配置复杂对话模型——这些方式不仅易出错,还要求开启并正确配置对话委托(dialog delegation),而实际需求往往更轻量。推荐做法是:利用 Alexa 预定义的内置意图(Built-in Intents)配合 session 状态管理,简洁、可靠且符合最佳实践。
✅ 正确实现步骤
-
在 LaunchRequest 中设置上下文标记
当技能启动并询问营业时间时,将当前上下文(如 offeredOpeningHours: true)存入 sessionAttributes,作为后续判断依据:
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
handle(handlerInput) {
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
sessionAttributes.offeredOpeningHours = true; // 标记已发起询问
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
const speakOutput = 'Welcome to Coffee Shop! Would you like to know our hours of operation?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('Would you like to know our hours?') // 引导明确回应
.getResponse();
}
};
-
为 AMAZON.YesIntent 添加专属处理器
在技能模型中启用 AMAZON.YesIntent(无需额外槽位),并在其 handler 中检查上下文标志:
const YesIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.YesIntent';
},
handle(handlerInput) {
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
if (sessionAttributes.offeredOpeningHours === true) {
// 执行对应业务逻辑
const speakOutput = 'We are open from nine a.m. to five p.m.';
delete sessionAttributes.offeredOpeningHours; // 清除状态,避免误触发
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('What else can I help with?')
.getResponse();
} else {
// 无上下文时的兜底响应
return handlerInput.responseBuilder
.speak("I'm not sure what you're saying yes to. Can you rephrase?")
.reprompt("You can say 'yes' or 'no' to my questions.")
.getResponse();
}
}
};
-
同步处理 AMAZON.NoIntent(可选但推荐)
提供礼貌退出路径,并清理上下文:
const NoIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.NoIntent';
},
handle(handlerInput) {
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
delete sessionAttributes.offeredOpeningHours;
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return handlerInput.responseBuilder
.speak("Ok, have a good day!")
.withShouldEndSession(true) // 主动结束会话
.getResponse();
}
};
⚠️ 关键注意事项
- 不要启用 ConfirmIntentDirective:它仅适用于 slot 确认场景,无法驱动跨意图跳转,且需严格匹配 dialog model 配置,极易失败。
- 务必清理 session attribute:每次使用后删除标记,防止后续无关“yes”意外触发逻辑(例如用户在其他环节说 yes)。
- Always end with reprompt or close session:避免静默终止,提升用户体验与 Alexa 流程合规性。
- 模型配置只需启用内置意图:在 Interaction Model 的 JSON 或开发者控制台中确保 AMAZON.YesIntent 和 AMAZON.NoIntent 已启用(默认通常已开启)。
✅ 总结
该方案摒弃了对对话委托和自定义指令的依赖,回归 Alexa 意图路由本质:用 intent 做动作,用 session 做记忆。结构清晰、调试简单、扩展性强——若后续需支持更多 Yes/No 场景(如“是否订阅每日提醒?”),只需增加不同命名的 session flag(如 offeredNewsletter: true)并复用相同的 Yes/No 处理器逻辑即可。这才是构建健壮语音交互的可持续路径。










