1

I have been trying to make a lambda function for a Lex chatbot I'm making, but whenever my intent calls upon the function, it keeps giving me the same error and I am tired of it. I am using node.js. The error message it gives me is:

An error has occurred: Invalid Lambda Response: 
Received invalid response from Lambda: Can not construct instance of
IntentResponse: no String-argument constructor/factory method to
deserialize from String value ('this works') at
[Source: "this works"; line: 1, column: 1

This happens no matter what kind of lambda function I input. Any answers?

3
  • Can you show your node code? Commented Aug 9, 2017 at 13:32
  • exports.handler = (event, context, callback) => { // TODO implement callback(null, "this works"); }; Commented Aug 9, 2017 at 19:18
  • sorry it looks weird, i can't get markdown to work right Commented Aug 9, 2017 at 19:22

1 Answer 1

1

This is happening because all you're sending back is a String, whereas Lex expects replies in specific formats e.g.

"dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled or Failed",
    "message": {
      "contentType": "PlainText or SSML",
      "content": "Message to convey to the user. For example, Thanks, your pizza has been ordered."
    },
   "responseCard": {
      "version": integer-value,
      "contentType": "application/vnd.amazonaws.card.generic",
      "genericAttachments": [
          {
             "title":"card-title",
             "subTitle":"card-sub-title",
             "imageUrl":"URL of the image to be shown",
             "attachmentLinkUrl":"URL of the attachment to be associated with the card",
             "buttons":[ 
                 {
                    "text":"button-text",
                    "value":"Value sent to server on button click"
                 }
              ]
           } 
       ] 
     }
  }

This code will work:

function close(sessionAttributes, fulfillmentState, message, responseCard) {
    return {
        sessionAttributes,
        dialogAction: {
            type: 'Close',
            fulfillmentState,
            message,
            responseCard,
        },
    };
}

function dispatch(intentRequest, callback) {
    const outputSessionAttributes = intentRequest.sessionAttributes || {};
    callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText',
        content: 'Thank you and goodbye' }));
}

function loggingCallback(response, originalCallback) {
    originalCallback(null, response);
}

exports.handler = (event, context, callback) => {
    try {
        console.log("event: " + JSON.stringify(event));
        dispatch(event, (response) => loggingCallback(response, callback));
    } catch (err) {
        callback(err);
    }
};

It simply sends back "Thank you and goodbye" in the required format, in this case with a "dialogAction" type of "Close" - which informs Lex not to expect a response from the user.

There are other types - this and more are all explained in the Lex documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

I am very new to AWS Lambda, and so I don't really understand how I get that JSON code out of my javascript.
I'd recommend you go through the tutorials they provide, they are fairly detailed - and show you how to construct the required JSON responses using sample functions like the "close" one I included in my answer. For that one all you need to provide is the "content" - a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.