qnabot-on-aws icon indicating copy to clipboard operation
qnabot-on-aws copied to clipboard

Invoke error when buttons are defined but image url is not in response card

Open tkashi opened this issue 1 year ago • 4 comments

Describe the bug In the designer, we can define an image url and Lex buttons in a question's response card. When we create a question with only set buttons fields without image url one, we will get no response when we ask the question in the client.

The implementation in the below code at v5.2.0 assumes that an image urls exists in a response card, which produces a type error ("Cannot read property 'length' of undefined"). However, in the older versions, this was not true. https://github.com/aws-solutions/qnabot-on-aws/blob/771b4b465266c19fb863a38c2aa0cd80bf665e96/lambda/fulfillment/lib/middleware/lex.js#L471

To Reproduce Steps to reproduce the behavior.

  1. Clone the repository from GitHub and deploy by following the step in the Readme.md.
  2. Add a question with specifying Lex buttons but keeping Card Image Url blank.
  3. Ask the question in the default client.
  4. No answer will pop up.

We can also see an error log in the CW logs of the FulfillmentLambda function.

Expected behavior A clear and concise description of what you expected to happen.

We can define a question only with buttons, but without a card image url.

Please complete the following information about the solution:

  • [x] Version: [v5.2.0]

To get the version of the solution, you can look at the description of the created CloudFormation stack. For example, "(SO0189) QnABot [...] v0.0.1".

  • [x] Region: [All regions]
  • [ ] Was the solution modified from the version published on this repository?
  • [ ] If the answer to the previous question was yes, are the changes available on GitHub?
  • [x] Have you checked your service quotas for the sevices this solution uses?
  • [x] Were there any errors in the CloudWatch Logs?

Screenshots If applicable, add screenshots to help explain your problem (please DO NOT include sensitive information). Screen Shot 2022-08-31 at 20 48 55 Screen Shot 2022-08-31 at 21 07 03 Screen Shot 2022-08-31 at 21 08 14

Additional context Add any other context about the problem here.

tkashi avatar Aug 31 '22 12:08 tkashi

@tkashi Looks like a change in 5.2.0 for the file lambda/fulfillment/lib/middleware/lex.js is causing the problem. Since you've cloned the repo you might try the following change for lines 468 to 482 and then update your stack.

if (!isConnectClient(request)){
    let imageResponseCardV2 = buildImageResponseCardV2(response);
    if(imageResponseCardV2) {
        let imgUrlLength = imageResponseCardV2.imageUrl ? imageResponseCardV2.imageUrl.length : 0;
        if(imgUrlLength > 250){
            qnabot.log("ResponseCard Image URL length is greater than the max limit (250 chars). Client is LexWebUI. Sending ResponseCard as session attribute rather than as Lex ImageresponseCard to avoid hitting the Lex URL length limit.")
        } else {
            out.messages[out.messages.length] = {
                "contentType": "ImageResponseCard",
                "imageResponseCard": imageResponseCardV2
            };
        }
    }
}

bobpskier avatar Aug 31 '22 13:08 bobpskier

Hi @bobpskier, thanks for your quick response! Yes, actually the below code defining imgUrlLength works for me, which looks almost same as your suggestion (though I'm not sure this is the perfect correction).

        let imgUrlLength = _.get(imageResponseCardV2, "imageUrl", []).length

tkashi avatar Aug 31 '22 13:08 tkashi

@tkashi The important part is to set imgUrlLength to 0 if it does not exist. The lodash mechanism above will work as well, however, the default should be an empty string "" vs an empty array []. Both will work as they produce a length of 0. However, the expected value of imageUrl is a string.

bobpskier avatar Aug 31 '22 17:08 bobpskier

Thank you @tkashi , @bobpskier , and @greengangsta!. We are currently working on a patch release and will include the fix in the release.

tabdunabi avatar Aug 31 '22 21:08 tabdunabi

Released as of v5.2.1, thanks all!

ihmaws avatar Jan 06 '23 14:01 ihmaws