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

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

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

  1. import requests
  2. def lambda_handler(event, context):
  3. city = event['currentIntent'] ['slots'] ['City']
  4. api = "http://api.openweathermap.org/data/2.5/weather?q="+ city +"TOKEN"
  5. json_data = requests.get(api).json()
  6. temp = int(json_data['main']['temp'] - 273.15)
  7. answer = f"Weather in {city} is {temp}C"
  8. message = {
  9. "dialogAction": {
  10. "type": "ConfirmIntent", #also tried Close
  11. "fulfillmentState": "Fulfilled",
  12. "message": {
  13. "contentType": "PlainText",
  14. "content": answer
  15. }
  16. }
  17. }
  18. return message
  19. # Also tried variant 2 With contentType PlainText or SSML:
  20. message = {
  21. "sessionAttributes": {},
  22. "dialogAction": {
  23. "type": "Close",
  24. "fulfillmentState": "Fulfilled",
  25. "message": {
  26. "contentType": "PlainText",
  27. "content": answer
  28. }
  29. }
  30. }
  31. # Variant 3:
  32. message = {
  33. "sessionState": {
  34. "dialogAction": {
  35. "type": "Close"
  36. },
  37. "intent": {
  38. "name": "FindingWeather"
  39. },
  40. "state": "Fulfilled"
  41. },
  42. "messages": [
  43. {
  44. "contentType": "PlainText",
  45. "content": answer
  46. }
  47. ]
  48. }

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

暂无答案!

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

相关问题