如何从AWS lambda重建对Amazon lex的响应?

问题描述 投票:1回答:1

我在lex控制台中设置了一个机器人,用于收集用户的数据,例如他们感兴趣的产品/服务以及电子邮件ID,电话号码等。现在,每当访问者与机器人交互时,我都希望收到一封电子邮件聊天对话。

我还创建了一个由lex履行触发的lambda函数但是我在bot ui上发生了这个错误发生错误:Lambda响应无效:从Lambda收到无效响应:无法构造IntentResponse的实例:no String-argument constructor / factory method从String值反序列化

根据文档,我重建了一个响应并返回它。

import json

def lambda_handler(event, context):
    print(event)

    var1 = '''dialogAction": {
    "type": "close",
    "fulfillmentState": "fulfilled",
    "message": {
      "contentType": "PlainText or SSML or CustomPayload",
      "content": "Message to convey to the user. For example, What size pizza would you like?"
    },
    }
    }'''
    return(var1)

这是错误的样子: -

An error has occurred: Invalid Lambda Response: Received invalid response from Lambda: Can not construct instance of IntentResponse: no String-argument constructor/factory method to deserialize from String value ('"dialogAction": { "type": "close", "fulfillmentState": "fulfilled", "message": { "contentType": "PlainText or SSML or CustomPayload", "content": "Message to convey to the user. For example, What size pizza would you like?" }, } }') at [Source: "\"dialogAction\": {\n \"type\": \"close\",\n \"fulfillmentState\": \"fulfilled\",\n \"message\": {\n \"contentType\": \"PlainText or SSML or CustomPayload\",\n \"content\": \"Message to convey to the user. For example, What size pizza would you like?\"\n },\n }\n }"; line: 1, column: 1]
python-3.x aws-lambda chatbot amazon-lex
1个回答
1
投票

您错误地将响应作为字符串传递。并且通过使用三重引号''',它添加了您可以在错误源中看到的换行符\n,而您不需要这些。

所以试试这个,只需将响应作为对象返回。我相信回调函数会在交付给Lex之前自动转换为JSON。

 var1 = {
    dialogAction": {
         "type": "close",
         "fulfillmentState": "fulfilled",
         "message": {
               "contentType": "PlainText or SSML or CustomPayload",
               "content": "Message to convey to the user. For example, What size pizza would you like?"
         },
    }
}
return var1
© www.soinside.com 2019 - 2024. All rights reserved.