
本文介绍如何利用 alexa 内置的 amazon.yesintent 和 amazon.nointent 实现自然的多轮对话跳转,无需复杂对话管理或自定义 slot 槽位,只需合理使用会话属性(session attributes)即可完成“欢迎→询问→确认→响应”的流程。
本文介绍如何利用 alexa 内置的 amazon.yesintent 和 amazon.nointent 实现自然的多轮对话跳转,无需复杂对话管理或自定义 slot 槽位,只需合理使用会话属性(session attributes)即可完成“欢迎→询问→确认→响应”的流程。
在 Alexa 技能开发中,实现“用户说‘Yes’后自动进入某意图”的关键,并不在于强制跳转或模拟 intent 触发,而在于利用内置意图 + 会话上下文状态管理。Alexa 并不支持运行时动态调用其他 intent,但可通过 AMAZON.YesIntent 和 AMAZON.NoIntent 捕获肯定/否定回应,并结合 session attributes 判断当前语境,从而决定下一步响应逻辑。
✅ 正确实现步骤
-
在 LaunchRequest 中设置上下文标记
当技能启动并询问营业时间时,将 offeredOpeningHours: true 存入 session attributes,作为后续判断依据:
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
在 Interaction Model 中确保已启用 AMAZON.YesIntent(默认开启,无需额外配置)。其处理器检查上下文标志,执行对应逻辑后清除状态:
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 9 a.m. to 5 p.m. What else can I help with?';
// 清除上下文,避免后续误触发
delete sessionAttributes.offeredOpeningHours;
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('What else can I help with?')
.getResponse();
} else {
// 无上下文时的兜底响应(如用户中途说 yes)
return handlerInput.responseBuilder
.speak("I'm not sure what you're saying yes to. Can you rephrase that?")
.reprompt("Can you tell me what you'd like to do?")
.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();
if (sessionAttributes.offeredOpeningHours === true) {
delete sessionAttributes.offeredOpeningHours;
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return handlerInput.responseBuilder
.speak("Ok, have a great day!")
.withShouldEndSession(true) // 主动结束会话
.getResponse();
} else {
return handlerInput.responseBuilder
.speak("Got it. Let me know if there's anything else I can assist with.")
.reprompt("Is there something else you'd like to know?")
.getResponse();
}
}
};
-
移除无效的 ConfirmIntentDirective
原代码中使用的 addConfirmIntentDirective 适用于确认用户刚表达的意图本身(如用户说“订一杯拿铁”,系统问“您要的是拿铁吗?”),而非跨意图引导。本场景中无需该指令,反而会导致模型与逻辑不匹配。
⚠️ 注意事项与最佳实践
- 不要依赖 dialog delegation 或 slot elicitation:本场景本质是“问题→答案”式对话,非多槽位填空流程,强行使用 dialog 指令会增加复杂度且易出错。
- 始终清理 session attributes:避免残留状态干扰后续交互(例如用户再次说“Yes”时误触发营业时间播报)。
- 为每个 Yes/No 响应提供明确的 reprompt:提升用户体验,防止静默中断。
- Interaction Model 不需额外 intent 或 slot:仅需确保 AMAZON.YesIntent 和 AMAZON.NoIntent 已启用(开发者控制台默认启用)。
- 若技能中存在多个 Yes/No 场景,建议使用更结构化的上下文键名(如 pendingAction: 'hours'),而非布尔标志,便于扩展维护。
通过以上方式,你就能以简洁、可靠、符合 Alexa 设计规范的方式,实现自然流畅的“Yes → 获取信息”对话流。










