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

xmq68pz9  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(225)

我遇到了aws lambda函数无法向aws lex发送预期json格式的巨大障碍。构建一个与lambda python函数通信的简单天气聊天机器人。它只有一个插槽{city}。在python中,我尝试了许多不同的 message 变量,但所有尝试都获得lex error无效lambda响应:从lambda接收到错误响应:未处理
以下是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
        }
    ]
}

两者都不起作用,所有尝试都会出现一个错误。。。(帮助)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题