Skip to main content
 首页 » 编程设计

python之如何在 AWS Lex 中添加多个响应

2024年12月31日10mfryf

我尝试使用 AWS Lambda 函数为 AWS Lex 添加多重响应,但我遇到了这个错误。

我一直在努力

但是我卡在了消息上

An error has occurred: Invalid Lambda Response: Received invalid response from Lambda: Can not construct instance of Message, problem: contentType must not be null at [Source: {"dialogAction": {"type": "ConfirmIntent", "message": {"messages": [{"contentType": "PlainText", "group": 1, "content": "Hello"}, {"contentType": "PlainText", "group": 2, "content": "My"}, {"contentType": "PlainText", "group": 3, "content": "Friend"}]}, "intentName": "CardsI", "slots": {"CardsB": null}}}; line: 1, column: 252]

在 Lambda 函数中,我们使用以下代码打印多个响应

return { 
    "dialogAction": { 
        "type": "ConfirmIntent", 
        "message": { 
            "messages": [{ 
                    "contentType": "PlainText", 
                    "group": 1, 
                    "content": "Hello" 
                }, 
                { 
                    "contentType": "PlainText", 
                    "group": 2, 
                    "content": "My" 
                }, 
                { 
                    "contentType": "PlainText", 
                    "group": 3, 
                    "content": "Friend" 
                } 
            ] 
        }, 
        "intentName": "CardsI", 
        "slots": { 
            "CardsB": "" 
        } 
    } 
} 

我们甚至仔细阅读了文档,例如

  1. https://docs.aws.amazon.com/lex/latest/dg/lambda-input-response-format.html#using-lambda-response-format

  2. https://docs.aws.amazon.com/lex/latest/dg/howitworks-manage-prompts.html#message-groups

  3. https://docs.aws.amazon.com/lex/latest/dg/context-mgmt.html#special-response

但我们仍然面临问题。有什么帮助吗?

请您参考如下方法:

我有同样的问题,文档没有提出任何建议。但是当检查来自 lex 的网络响应时,我们可以看到,在多条消息的情况下,消息数组作为字符串而不是对象传入。

lambda 的响应应采用以下格式。

return { 
"dialogAction": { 
    "type": "ConfirmIntent", 
    "message": { 
        "contentType": "CustomPayload", 
        "content": "{\"messages\":[{\"type\":\"PlainText\",\"group\":1,\"value\":\"Hello\"},{\"type\":\"PlainText\",\"group\":2,\"value\":\"Hey\"}]}" 
    }, 
    "intentName": "CardsI", 
    "slots": { 
        "CardsB": null 
    } 
}