首页 > 解决方案 > 如果通过变量,AWS Lex 不会识别响应

问题描述

下面的 lambda 代码的响应不被 lex 接受,但是在return语句中,如果我将 json插槽值更改为声明的插槽变量,然后工作,即 lex 接受它的响应。它完全令人困惑,因为变量slot和变量d具有相同的值,请在附件中找到我的云观察日志截图。

def lambda_handler(event,context):
    slot=event['currentIntent']['slots']
    d="{'Intro': None, 'Start': None, 'ReturnBooking': None, 'name': None, 'pickup': None, 'conformation': None, 'location': None, 'Count': None, 'comfort': None}"
    print("using dict:",slot,"using variable:",d)
    return {  
       "dialogAction": {
   "type": "Delegate",
   "slots": d
  }
          }

在此处输入图像描述

如果有人知道,请帮助我。

标签: amazon-web-servicesaws-lambdaamazon-lex

解决方案


如果一个插槽没有保存一个值,那么它应该是nullnot None。看起来 Cloudwatch 正在nullNone您记录日志。这应该是您的变量之间的区别:slotd.

所以这d应该是:

d="{'Intro': null, 'Start': null, 'ReturnBooking': null, 'name': null, 'pickup': null, 'conformation': null, 'location': null, 'Count': null, 'comfort': null}"

但是实际上没有理由为您的意图插槽重新创建一个字符串。您应该简单地将slots=event['currentIntent']['slots']变量传递回 Lex。如果您想更改 Lambda 中的插槽,请将其视为数组并将其中一个插槽设置为新值:

slots['slotName'] = "new value";

或者您可以通过将其设置为 null 来删除插槽的值:

slots['slotName'] = null;

然后返回slots给 Lex。


推荐阅读