首页 > 解决方案 > aws lambda 返回响应卡在 Amazon Lex 中引发 null 履行状态错误?

问题描述

我编写了这个函数,当它只是作为字符串返回时,它返回正常。我非常严格地遵循响应卡的语法,它通过了我在 lambda 中的测试用例。然而,当它通过 Lex 调用时,它会抛出一个错误,我将在下面发布。它说fulfillmentState 不能为null,但在它抛出的错误中表明它不为null。

我试过切换对话状态和响应卡的顺序,我试过切换“type”和“fulfillmentState”的顺序。功能:

def backup_phone(intent_request):
    back_up_location = get_slots(intent_request)["BackupLocation"]
    phone_os = get_slots(intent_request)["PhoneType"]

    try:
        from googlesearch import search
    except ImportError:
        print("No module named 'google' found")

    # to search
    query = "How to back up {} to {}".format(phone_os, back_up_location)

    result_list = []
    for j in search(query, tld="com", num=5, stop=5, pause=2):
        result_list.append(j)

    return {
        "dialogAction": {
            "fulfilmentState": "Fulfilled",
            "type": "Close",
            "contentType": "Plain Text",
            'content': "Here you go",
        },
        'responseCard': {
            'contentType': 'application/vnd.amazonaws.card.generic',
            'version': 1,
            'genericAttachments': [{
                'title': "Please select one of the options",
                'subTitle': "{}".format(query),
                'buttons': [
                    {
                        "text": "{}".format(result_list[0]),
                        "value": "test"
                    },

                ]
            }]
        }
    }

在 lambda 中传递的测试用例的屏幕截图:https ://ibb.co/sgjC2WK Lex 中的错误抛出屏幕截图:https ://ibb.co/yqwN42m

Lex 中的错误文本:

“发生错误:无效的 Lambda 响应:收到来自 Lambda 的无效响应:无法构造 CloseDialogAction 的实例,问题:在 [Source: {"dialogAction": {"fulfilmentState": "Fulfilled ", "type": "Close", "contentType": "Plain Text", "content": "Here you go"}, "responseCard": {"contentType": "application/vnd.amazonaws.card.generic" , "version": 1, "genericAttachments": [{"title": "请选择其中一个选项", "subTitle": "如何将 Iphone 备份到 windows", "buttons": [{"text": ” https://www.easeus。com/iphone-data-transfer/how-to-backup-your-iphone-with-windows-10.html ", "value": "test"}]}]}}; line: 1, column: 121]"

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

解决方案


我通过发送我试图通过函数返回的所有内容来解决这个问题,然后将所有信息以正确的语法存储到一个对象中,然后我从函数中返回该对象。相关代码如下:

     def close(session_attributes, fulfillment_state, message, response_card):
         response = {
            'sessionAttributes': session_attributes,
    'dialogAction': {
        'type': 'Close',
        'fulfillmentState': fulfillment_state,
        'message': message,
      "responseCard": response_card,
    }
}
return response


    def backup_phone(intent_request):
back_up_location = get_slots(intent_request)["BackupLocation"]
phone_os = get_slots(intent_request)["PhoneType"]

try:
    from googlesearch import search
except ImportError:
    print("No module named 'google' found")

# to search
query = "How to back up {} to {}".format(phone_os, back_up_location)

result_list = []
for j in search(query, tld="com", num=1, stop=1, pause=1):
    result_list.append(j)

return close(intent_request['sessionAttributes'],
             'Fulfilled',
             {'contentType': 'PlainText',
              'content': 'test'},
             {'version': 1,
                 'contentType': 'application/vnd.amazonaws.card.generic',
                 'genericAttachments': [
                     {
                         'title': "{}".format(query.lower()),
                         'subTitle': "Please select one of the options",
                         "imageUrl": "",
                         "attachmentLinkUrl": "{}".format(result_list[0]),
                         'buttons': [
                             {
                                 "text": "{}".format(result_list[0]),
                                 "value": "Thanks"
                             },
                         ]
                     }
                 ]
             }
             )

推荐阅读