如何在responseBuilder中调用其他intent而不使用addDelegateDirective或addElicitSlotDirective
实现方案
针对你的需求,有两种可行的实现方式,可根据你的SDK版本和使用场景选择:
方案1:使用Dialog.StartIntentRequest指令(推荐)
该方案无需用户额外交互,播放完speakOutput后会直接触发目标intent,完全不需要绑定槽位询问逻辑,也不会使用平台默认的对话提示语,可完全自定义SSML输出。
示例代码如下:
return input.responseBuilder .speak(speakOutput) // 你的自定义SSML内容会正常播报 .addDirective({ type: 'Dialog.StartIntentRequest', intent: { name: '你需要跳转的目标Intent名称', confirmationStatus: 'NONE', slots: { // 若目标intent需要传递槽位可在此处填写,无槽位则留空 } } }) .getResponse();
如果你使用的是ASK SDK v2及以上版本,可以直接调用封装好的接口:
return input.responseBuilder .speak(speakOutput) .addStartIntentRequestDirective({ name: '你需要跳转的目标Intent名称', slots: {} }) .getResponse();
注意:该指令仅支持跳转当前技能内已定义的intent,无法跨技能触发。
方案2:Session状态标记路由(兼容旧版本SDK)
如果你的运行环境不支持Dialog.StartIntentRequest指令,可以通过会话属性标记待跳转的intent,下一轮请求进入时直接路由到对应处理逻辑:
- 第一次返回响应时存入跳转标记:
const sessionAttrs = input.attributesManager.getSessionAttributes(); // 存入待跳转的intent标记 sessionAttrs.pendingJumpIntent = '目标Intent名称'; input.attributesManager.setSessionAttributes(sessionAttrs); return input.responseBuilder .speak(speakOutput) .reprompt('请说出你的需求') // 可自定义等待回复的提示语 .getResponse();
- 在全局请求拦截器中添加路由判断:
const JumpIntentInterceptor = { process(handlerInput) { const sessionAttrs = handlerInput.attributesManager.getSessionAttributes(); // 存在待跳转标记时直接修改当前请求的intent if (sessionAttrs.pendingJumpIntent) { const targetIntent = sessionAttrs.pendingJumpIntent; delete sessionAttrs.pendingJumpIntent; handlerInput.attributesManager.setSessionAttributes(sessionAttrs); handlerInput.requestEnvelope.request.intent.name = targetIntent; } } };
该方案需要用户做出一次回复后才会触发目标intent跳转。
内容的提问来源于stack exchange,提问作者nie.kohl
相关产品推荐
相关产品推荐

