3
votes

I am getting Error while displaying image as a response. I am getting this error :

An error has occurred: Invalid Lambda Response: Received invalid response from Lambda: Unrecognized field "responseCard" (class IntentResponse), not marked as ignorable at [Source: {"sessionAttributes":{},"dialogAction":{"type":"Close","fulfillmentState":"Fulfilled","message":{"contentType":"PlainText","content":"

function close(sessionAttributes, fulfillmentState, message) {
    return {
        sessionAttributes,
        dialogAction: {
            type: 'Close',
            fulfillmentState,
            message,
        },
        responseCard: {
            version: '2',
            contentType: "application/vnd.amazonaws.card.generic",
            genericAttachments: [
                  {
                     imageUrl:"URL of the image to be shown",
                  }
               ] 
        }
    };
}

exports.handler = (event, context, callback) => {
    console.log( "EVENT= "+JSON.stringify(event) );

    const intentName = event.currentIntent.name;
    var sessionAttributes = event.sessionAttributes;

    var responseMsg = "";

    if (intentName == "greetings") {
        var message = {
            'contentType': 'PlainText', 
            'content': 'Hi! How can I help you?'
        }

        responseMsg = close( sessionAttributes, 'Fulfilled', message );
    }
    else {
        console.log( "ERROR unhandled intent named= "+intentName );
        responseMsg = close( sessionAttributes, 'Fulfilled', {"contentType":"PlainText", "content":"Sorry, I can't help with that yet."});
    }

    console.log( "RESPONSE= "+JSON.stringify(responseMsg) );
    callback(null,responseMsg);
}

How should i display image on the chat box? What mistake i am doing here?

1

1 Answers

2
votes

The responseCard needs to be inside dialogAction.

Try:

function close(sessionAttributes, fulfillmentState, message) {
    return {
        sessionAttributes,
        "dialogAction": {
            "type": "Close",
            fulfillmentState,
            message,
            "responseCard": {
                "version": "2",
                "contentType": "application/vnd.amazonaws.card.generic",
                "genericAttachments": [
                      {
                         "imageUrl":"http://...",
                      }
                ] 
            }
        }
    };
}

I also added quotation marks to the keys that are not variables just to be safe.


More Information on Response Cards:

ResponseCard format: contentType, genericAttachments, version
GenericAttachments format: attachmentLinkUrl, buttons, imageUrl, subtitle, title
Buttons format: text, value
None are required, but here is an example of all responseCard properties:

"responseCard": {
  "version": integer-value,                                     //change to integer
  "contentType": "application/vnd.amazonaws.card.generic",      //don't change
  "genericAttachments": [
      {
         "title":"card-title",                                  //change to any string         
         "subTitle":"card-sub-title",                           //change to any string 
         "imageUrl":"http://...",                               //change to full url 
         "attachmentLinkUrl":"http://...",                      //change to full url 
         "buttons":[ 
             {
                "text":"button-text",                           //change to any string 
                "value":"Value sent to server on button click"  //change to any string
             }
          ]
       } 
   ] 
}