aws-solutions / qnabot-on-aws

AWS QnABot is a multi-channel, multi-language conversational interface (chatbot) that responds to your customer's questions, answers, and feedback. The solution allows you to deploy a fully functional chatbot across multiple channels including chat, voice, SMS and Amazon Alexa.
https://aws.amazon.com/solutions/implementations/aws-qnabot
Apache License 2.0
400 stars 252 forks source link

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

Closed tkashi closed 1 year ago

tkashi commented 2 years ago

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:

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".

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.

bobpskier commented 2 years ago

@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
            };
        }
    }
}
tkashi commented 2 years ago

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
bobpskier commented 2 years ago

@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.

tabdunabi commented 2 years ago

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

ihmaws commented 1 year ago

Released as of v5.2.1, thanks all!