简体   繁体   English

aws lex 的正确 aws lambda python 输出格式是什么? 获取无效的 Lambda 响应

[英]What is correct aws lambda python output format to aws lex. Getting Invalid Lambda Response

i hit a huge roadblock with aws lambda function not sending expected json format to aws lex.我遇到了一个巨大的障碍,aws lambda 函数没有将预期的 json 格式发送到 aws lex。 Building a simple weather chatbot that communicates with lambda python function.构建一个与 lambda python 函数通信的简单天气聊天机器人。 It has just one slot {city}.它只有一个插槽 {city}。 In Python i tried many different variations of message variable but all tries are getting lex error Invalid Lambda Response: Received error response from Lambda: Unhandled在 Python 中,我尝试了许多不同的message变量变体,但所有尝试都得到 lex 错误无效的 Lambda 响应:收到来自 Lambda 的错误响应:未处理

Here is lambda function:这是 lambda 函数:

import requests

def lambda_handler(event, context):
    city = event['currentIntent'] ['slots'] ['City']
    api = "http://api.openweathermap.org/data/2.5/weather?q="+ city +"TOKEN"
    json_data = requests.get(api).json()
    temp = int(json_data['main']['temp'] - 273.15)
    answer = f"Weather in {city} is {temp}C"
    message = {
        "dialogAction": {
            "type": "ConfirmIntent", #also tried Close
            "fulfillmentState": "Fulfilled",
            "message": {
                "contentType": "PlainText",
                "content": answer
                }
            }
        }

    return message

#Also tried variant 2 With contentType PlainText or SSML:
    message = {
        "sessionAttributes": {},
        "dialogAction": {
            "type": "Close",
            "fulfillmentState": "Fulfilled",
            "message": {
                "contentType": "PlainText",
                "content": answer
            }
        }
    }

#Variant 3:
message = {
    "sessionState": {
        "dialogAction": {
            "type": "Close"
        },
        "intent": {
          "name": "FindingWeather"
        },
        "state": "Fulfilled"
    },
    "messages": [
        {
            "contentType": "PlainText",
            "content": answer
        }
    ]
}

Neither works, one error for all tries... HELP )两者都不起作用,所有尝试都有一个错误......帮助)

I've pulled this Lambda response from one of my Cloudwatch logs.我从我的 Cloudwatch 日志之一中提取了此 Lambda 响应。

{
   "dialogAction":{
      "type":"Close",
      "fulfillmentState":"Fulfilled",
      "message":{
         "contentType":"PlainText",
         "content":"MY BOTS RESPONSE"
      }
   }
}

I would suggest that you hardcode a sample response (using mine perhaps) and make that the only response from the Lambda function.我建议您对示例响应进行硬编码(可能使用我的),并使其成为 Lambda 函数的唯一响应。 The idea will be to test that Lex works correctly with a proven response message.我们的想法是使用经过验证的响应消息来测试 Lex 是否正常工作。

Once you have that working, add in sessionAttributes and then finally substitute the value of content with the value from the answer variable.完成该工作后,添加sessionAttributes ,然后最后用answer变量中的值替换 content 的值。 Hopefully this will help locate the line that's causing the error.希望这将有助于找到导致错误的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM