How to send a response card using AWS Lambda in C#
Asked Answered
B

3

11

Hi I am developing a chatbot on amazon lex and I want to send a response card using the lambda function but on using response card function inside the close response format it gives the error of null exception. Can anyone tell the solution to it? PS I am using FlowerOrder blueprint created by Nikki.

if (slots[greet] != null) 
        {

            var validateGreet = ValidateUserGreeting(slots[greet]);
            if (validateGreet.IsValid) 
            {
                return Close(sessionAttributes,
                              "Fulfilled",
                              new LexResponse.LexMessage
                              {
                                  ContentType = "PlainText",
                                  Content = String.Format("Hello Kindly choose one option")
                              },
                              new LexResponse.LexResponseCard
                              {
                                  Version = 1,
                                  ContentType = "application/vnd.amazonaws.card.generic",
                                  GenericAttachments =
                                  {
                                  new LexResponse.LexGenericAttachments
                                  {
                                      Buttons =
                                      {
                                         new LexResponse.LexButton
                                         {
                                             Text = "Shop Now",
                                             Value = "Shop Now"
                                         }
                                      },
                                      AttachmentLinkUrl = null,
                                      Title = "Shopping",
                                      SubTitle = "Sub Shopping",
                                      ImageUrl = null
                                  }
                                  }
                              }
                              );
            }

Exception:-

2020-06-09 17:31:20: Object reference not set to an instance of an object.: NullReferenceException at EVS_Test_Abbar_Lambda_Function.OrderWatchIntentProcessorTest.Process(LexEvent lexEvent, ILambdaContext context) in D:\AWS Project\Abbrar Projects\EVS_Test_Abbar_Lambda_Function\EVS_Test_Abbar_Lambda_Function\OrderWatchIntentProcessorTest.cs:line 52 at EVS_Test_Abbar_Lambda_Function.Function.FunctionHandler(LexEvent lexEvent, ILambdaContext context) in D:\AWS Project\Abbrar Projects\EVS_Test_Abbar_Lambda_Function\EVS_Test_Abbar_Lambda_Function\Function.cs:line 43 at lambda_method(Closure , Stream , Stream , LambdaContextInternal )

Bridges answered 8/6, 2020 at 7:8 Comment(1)
Please post your stack trace so we can see what it doesn't likeAyacucho
B
2

Here is the solution to it since if you look at the structure of JSON it contains many models and lists and each has to be handled separately.

LexResponse.LexResponseCard lexResponseCard = new LexResponse.LexResponseCard();

    List<LexResponse.LexGenericAttachments> ListlexGenericAttachments = new List<LexResponse.LexGenericAttachments>();
    LexResponse.LexGenericAttachments lexGenericAttachments = new LexResponse.LexGenericAttachments();



    List<LexResponse.LexButton> ListlexButton = new List<LexResponse.LexButton>();
    LexResponse.LexButton lexButton = new LexResponse.LexButton();



    lexButton.Text = "Yes Now";
    lexButton.Value = "Yes";
    ListlexButton.Add(lexButton);
    lexGenericAttachments.AttachmentLinkUrl = "Link";
    //lexGenericAttachments.AttachmentLinkUrl = null;
    lexGenericAttachments.Title = "Shopping";
    lexGenericAttachments.SubTitle = "Sub Shopping";
    lexGenericAttachments.ImageUrl = "Link";
    //lexGenericAttachments.ImageUrl = null;
    lexGenericAttachments.Buttons = ListlexButton;



    ListlexGenericAttachments.Add(lexGenericAttachments);



    lexResponseCard.Version = 0;
    lexResponseCard.ContentType = "application/vnd.amazonaws.card.generic";
    lexResponseCard.GenericAttachments = ListlexGenericAttachments;



    return Close(sessionAttributes,
                      "Fulfilled", 
                      new LexResponse.LexMessage
                      {
                          ContentType = "PlainText",
                          Content = String.Format("Hello Kindly choose one option")
                      },
                      lexResponseCard
                      );
Bridges answered 10/6, 2020 at 9:7 Comment(0)
R
1

It may be your capitalization of key names. For example you have ContentType but it should be contentType as camelcase beginning with lowercase letters.

return Close(sessionAttributes,
    "Fulfilled",
    new LexResponse.LexMessage
    {
        contentType = "PlainText",
        content = String.Format("Hello Kindly choose one option")
    },
    new LexResponse.LexResponseCard
    {
        version = 1,
        contentType = "application/vnd.amazonaws.card.generic",
        GenericAttachments =
        {
                new LexResponse.LexGenericAttachments
                {
                    Buttons =
                    {
                        new LexResponse.LexButton
                        {
                            text = "Shop Now",
                            value = "Shop Now"
                        }
                    },
                    attachmentLinkUrl = null,
                    title = "Shopping",
                    subTitle = "Sub Shopping",
                    imageUrl = null
                }
        }
    }
);
Resist answered 15/6, 2020 at 15:8 Comment(0)
R
1

try just one Lex Response Card .

    return Close(sessionAttributes,
    "Fulfilled"
    new LexResponse.LexResponseCard
    {
        version = 1,
        contentType = "application/vnd.amazonaws.card.generic",
        GenericAttachments =
        {
                new LexResponse.LexGenericAttachments
                {
                    Buttons =
                    {
                        new LexResponse.LexButton
                        {
                            text = "Shop Now",
                            value = "Shop Now"
                        }
                    },
                    attachmentLinkUrl = null,
                    title = "Shopping",
                    subTitle = "Sub Shopping",
                    imageUrl = null
                }
        }
    }
);
Ruyter answered 15/6, 2020 at 23:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.