Amazon Lex v2技术问题:自动触发意图与Lambda延迟对话控制
问题解决指南
问题1:自动触发Welcome Intent启动对话
Amazon Lex v2本身不会主动发起对话,需通过集成的Kommunicate客户端触发。在网站加载完成后,调用Kommunicate API主动发送触发请求:
// 页面加载完成后执行 window.addEventListener('load', function() { setTimeout(() => { kmChat.sendMessage({ message: 'welcome', // 填写你设置的Welcome Intent触发词 to: '你的Kommunicate机器人ID' }); }, 1000); // 延迟1秒确保Kommunicate初始化完成 });
问题2:先返回Elicit Slot,5秒后自动触发Confirm Intent
Lambda为同步执行环境,无法在响应后延迟触发操作,需通过前端Kommunicate处理延迟逻辑:
步骤1:Lambda返回带标记的Elicit Slot响应
修改Lambda代码,添加自定义元数据标记,告知前端需延迟触发Confirm Intent:
if carFound is not None: dialog_type = "ElicitSlot" slot_to_elicit = "Args" custom_metadata = {} if resp['total'] < 30: custom_metadata["need_auto_confirm"] = "true" response = { "sessionState": { "dialogAction": { "type": dialog_type, "slotToElicit": slot_to_elicit }, "intent": { 'name': intent, 'slots': slots, "state": "InProgress" // 必须设为InProgress,避免会话提前结束 } }, "messages": carFound, "responseMetadata": custom_metadata }
步骤2:前端监听消息并延迟触发
通过Kommunicate的消息监听事件,识别标记后延迟5秒发送触发Confirm Intent的消息:
// 监听Kommunicate消息接收事件 Kommunicate.on('MESSAGE_RECEIVED', function(message) { if (message.senderType === 'bot' && message.metadata?.need_auto_confirm === "true") { setTimeout(() => { kmChat.sendMessage({ message: "confirm_trigger", // 填写你设置的Confirm Intent触发词 to: message.from }); }, 5000); // 延迟5秒执行 } });
关键注意事项
- Lambda中
intent.state必须设为InProgress,否则会话会直接结束,无法触发后续意图。 - 需确保Lex的Confirm Intent已配置对应触发词,或通过Kommunicate API直接指定意图名称(依版本支持情况而定)。
内容的提问来源于stack exchange,提问作者Marco Amendola
相关产品推荐
相关产品推荐

