如何为关联Amazon Lex V2的Amazon Connect聊天机器人配置交互式消息
Amazon Connect集成Lex V2后Lambda响应不展示的修复方案
当Lex V2关联Amazon Connect时,Connect对Lex的响应格式有特定要求——必须将响应内容放在sessionState.dialogAction.message字段中,你当前的代码把消息放在了顶层的content和contentType,这部分内容会被Connect直接忽略,导致响应无法展示。
核心修改点
把消息内容从顶层移到sessionState.dialogAction.message结构下,同时确保dialogAction.type设置符合会话需求(结束会话用Close,需继续收集信息用ElicitSlot/Delegate)。
修改后的示例代码
def message_format_retry(message, intent_type, name_intent, event, intent, sessionAttributes): return { "bot": event["bot"], "inputMode": event["inputMode"], "inputTranscript": event["inputTranscript"], "sessionState": { "dialogAction": { "type": "Close", # 关键:将消息内容移至该字段下 "message": { "contentType": "PlainText", "content": "Incorrect username or password. Please try again or Sign up" } }, "intent": { "name": name_intent, "slots": intent["intent"]["slots"], "state": "Fulfilled" }, "sessionAttributes": sessionAttributes, "originatingRequestId": event.get("sessionState").get("originatingRequestId") }, "interpretations": event.get("interpretations"), "requestAttributes": event["requestAttributes"], "sessionId": event.get("sessionId") }
图片/交互式卡片响应适配
如果需要返回图片或卡片,需使用Lex的ImageResponseCard格式并嵌套在dialogAction.message中,示例如下:
"message": { "contentType": "ImageResponseCard", "content": "操作选项", "imageResponseCard": { "title": "用户名或密码错误", "subtitle": "请选择后续操作", "imageUrl": "https://your-domain.com/error-icon.png", "buttons": [ { "text": "重试", "value": "重试" }, { "text": "注册", "value": "注册" } ] } }
注意:Connect仅支持PlainText、SSML、ImageResponseCard三种contentType,其他类型会被忽略。
内容的提问来源于stack exchange,提问作者Camila Rayen Nahuel
相关产品推荐
相关产品推荐

