0

I have written this function which returns fine when it is just returning as a String. I have followed the syntax for the response card very closely and it passes my test case in lambda. However when it's called through Lex it throws an error which i'll post below. It says fulfillmentState cannot be null but in the error it throws it shows that it is not null.

I have tried switching the order of the dialogue state and response card, i have tried switching the order of "type" and "fulfillmentState". Function:

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

                ]
            }]
        }
    }

screenshot of test case passing in lambda: https://ibb.co/sgjC2WK screenshot of error throw in Lex: https://ibb.co/yqwN42m

Text for the error in Lex:

"An error has occurred: Invalid Lambda Response: Received invalid response from Lambda: Can not construct instance of CloseDialogAction, problem: fulfillmentState must not be null for Close dialog action at [Source: {"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": "How to back up Iphone to 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]"

shizhen
  • 12,251
  • 9
  • 52
  • 88
Synikk
  • 31
  • 1
  • 2
  • Would appear that your issue was due to incorrect spelling in your response message. `fulfillmentState` has two L's whereas your key only has one. Also, *Plain Text* should be written without the space in between. – Reegz Aug 18 '20 at 09:35

1 Answers1

0

I fixed the issue by sending everything i was trying to return through a function, and then storing all the info in the correct syntax into an object, which i then returned from the function. Relevant code below:

     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"
                             },
                         ]
                     }
                 ]
             }
             )
Synikk
  • 31
  • 1
  • 2