
本文介绍如何利用 alexa 内置的 amazon.yesintent 和 amazon.nointent 实现自然的多轮对话跳转,无需复杂对话管理或自定义槽位确认,只需合理使用会话属性(session attributes)即可完成“欢迎→询问→根据‘是/否’响应不同内容”的流程。
本文介绍如何利用 alexa 内置的 amazon.yesintent 和 amazon.nointent 实现自然的多轮对话跳转,无需复杂对话管理或自定义槽位确认,只需合理使用会话属性(session attributes)即可完成“欢迎→询问→根据‘是/否’响应不同内容”的流程。
在 Alexa 技能开发中,实现“用户说‘Yes’后自动进入特定意图”这一需求,并不需要依赖 addConfirmIntentDirective 或手动配置复杂对话模型——这些方式不仅易出错,还要求启用并正确配置对话委托(dialog delegation),而实际场景中往往只需轻量级上下文管理。
核心思路是:用会话属性(session attributes)记录当前对话状态,再由内置的 AMAZON.YesIntent 和 AMAZON.NoIntent 意图进行条件分发。这种方式简洁、可靠,且完全兼容标准技能模型(无需自定义槽位或确认逻辑)。
✅ 正确实现步骤
-
在 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 of operation?')
.getResponse();
}
};
-
为 AMAZON.YesIntent 添加专用处理器
检查 offeredOpeningHours 是否为 true,若是则返回营业时间;无论结果如何,务必清除该标志,避免后续误触发:
const YesIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.YesIntent';
},
handle(handlerInput) {
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
let speakOutput = '';
if (sessionAttributes.offeredOpeningHours === true) {
speakOutput = 'We are open from nine AM to five PM. What else can I help with?';
// 清除状态,防止重复响应
delete sessionAttributes.offeredOpeningHours;
} else {
speakOutput = 'I\'m not sure what you\'re saying yes to. Could you clarify?';
}
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('What else can I help with?')
.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('Okay, have a great day!')
.withShouldEndSession(true)
.getResponse();
}
};
-
确保技能模型已启用内置意图
在 Alexa Developer Console 的 Interaction Model 中,确认已勾选:
- AMAZON.YesIntent
- AMAZON.NoIntent
(无需添加示例语句,系统已预训练支持常见“yes/no”表达)
⚠️ 注意事项:
- 不要使用 addConfirmIntentDirective 配合 OperationalHoursIntent —— 它仅适用于槽位确认,而非跨意图跳转;
- 避免在多个场景复用同一会话属性名(如 offeredOpeningHours),否则易引发状态污染;可按功能命名,如 pendingOrderConfirmation;
- 所有响应结尾建议包含明确引导(如提问或结束提示),提升用户体验与 ASR 置信度;
- 若需支持更多分支(如“Maybe”、“Not now”),可扩展为自定义意图 + 更细粒度状态管理。
通过上述方式,你就能以最小侵入性、最高兼容性的方式,实现流畅、符合 Alexa 设计规范的 Yes/No 对话流。










