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

时间:2019-04-27 07:03:45

标签: python-3.x aws-lambda chatbot amazon-lex

我在lex控制台中设置了一个机器人,该机器人从用户那里收集数据,例如他们感兴趣的产品/服务以及电子邮件ID,电话号码等。现在,每次访客与该机器人进行交互时,我都希望接收带有聊天对话的电子邮件。

我还创建了一个由lex履行触发的lambda函数,但我在bot ui上收到此错误发生了一个错误:无效的Lambda响应:从Lambda接收到无效响应:无法构造IntentResponse的实例:无字符串-argument构造函数/工厂方法从字符串值反序列化

根据文档,我重构了一个响应并将其返回。

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]

1 个答案:

答案 0 :(得分: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