如何在 Amazon Lex 中创建机器人来获取天气更新?

How can I create a bot in Amazon Lex for getting weather update?

我正在尝试获取天气更新。 Python 代码运行良好,但我无法将其嵌入 Amazon Lex。它显示收到错误响应。

from botocore.vendored import requests
    # using openweathermap api 
    api_address = 'http://api.openweathermap.org/data/2.5/weather?appid=__api_key_here__&q='
    city = input("Enter city >> ")

    url = api_address + city

    json_data = requests.get(url).json()
    formatted_data = json_data['weather'][0]['main']
    desc_data = json_data['weather'][0]['description']
    print(formatted_data)
    print(desc_data)
    # print(json_data)
  • 确保 api 是 运行 完美的 python 代码。
  • 取决于您需要保持类型为 ElicitSlot 或 ElicitInten 的下一个状态
  • 如果您使用 lambda 作为 lex 的后端,我们需要以下面的格式发送响应。
  • Lambda 响应格式可以参考link Lambda response formats

        {
      "dialogAction": {
        "type": "Close",
        "fulfillmentState": "Fulfilled",
        "message": {
          "contentType": "PlainText",
          "content": "Thanks, your pizza has been ordered."
        },
        "responseCard": {
          "version": integer-value,
          "contentType": "application/vnd.amazonaws.card.generic",
          "genericAttachments": [
              {
                 "title":"card-title",
                 "subTitle":"card-sub-title",
                 "imageUrl":"URL of the image to be shown",
                 "attachmentLinkUrl":"URL of the attachment to be associated with the card",
                 "buttons":[ 
                     {
                        "text":"button-text",
                        "value":"Value sent to server on button click"
                     }
                 ]
              } 
          ] 
        }
      }
    }