Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 )

like image 764
Mohammad Khubaib Nasir Avatar asked Jun 08 '20 07:06

Mohammad Khubaib Nasir


2 Answers

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
                      );
like image 81
Mohammad Khubaib Nasir Avatar answered Sep 21 '22 01:09

Mohammad Khubaib Nasir


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
                }
        }
    }
);
like image 25
Jin Thakur Avatar answered Sep 19 '22 01:09

Jin Thakur