aws lambda返回响应卡在Amazon Lex中引发空的fulfillmentState错误?

时间:2019-07-16 03:15:31

标签: amazon-web-services aws-lambda aws-lex

我已经编写了此函数,当它只是作为字符串返回时,该函数返回正常。我非常仔细地遵循了响应卡的语法,它以lambda的形式通过了我的测试案例。但是,当通过Lex调用它时,会引发错误,我将在下面发布。它说完成状态不能为空,但抛出错误则表明它不为空。

我尝试切换对话状态和响应卡的顺序,我尝试切换“类型”和“ 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的实例,问题:对于[来源:{” dialogAction“:{” fulfilmentState“ :“已完成”,“类型”:“关闭”,“ contentType”:“纯文本”,“内容”:“在这里”},“ responseCard”:{“ contentType”:“ application / vnd.amazonaws.card .generic”,“版本”:1,“ genericAttachments”:[{“标题”:“请选择以下选项之一”,“ subTitle”:“如何将Iphone备份到窗口”,“按钮”:[{ text“:” https://www.easeus.com/iphone-data-transfer/how-to-backup-your-iphone-with-windows-10.html“,” value“:” test“}]}]}};行:1,列:121]”

1 个答案:

答案 0 :(得分:0)

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

     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"
                             },
                         ]
                     }
                 ]
             }
             )