如何在 C# 中使用 AWS Lambda 发送响应卡

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

您好,我正在 amazon lex 上开发一个聊天机器人,我想使用 lambda 函数发送一个响应卡,但是在关闭响应格式中使用响应卡函数时,它给出了 null 异常错误。任何人都可以告诉它的解决方案吗? PS 我正在使用 Nikki 创建的 FlowerOrder 蓝图。

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
                                  }
                                  }
                              }
                              );
            }

异常:-

2020-06-09 17:31:20:对象引用未设置到对象的实例。:D:\AWS [=19 中 EVS_Test_Abbar_Lambda_Function.OrderWatchIntentProcessorTest.Process(LexEvent lexEvent,ILambdaContext 上下文)处的 NullReferenceException =] Projects\EVS_Test_Abbar_Lambda_Function\EVS_Test_Abbar_Lambda_Function\OrderWatchIntentProcessorTest.cs:第 52 行位于 EVS_Test_Abbar_Lambda_Function.Function.FunctionHandler(LexEvent lexEvent, ILambdaContext 上下文) 在 D:\AWS Project\Abbrar Projects\EVS_Test_Abbar_Lambda_Function\EVS_Test_Abbar_Lambda_Function\Function.cs:第 43 行 在 lambda_method(闭包,流,流,LambdaContextInternal)

这是它的解决方案,因为如果您查看 JSON 的结构,它包含许多模型和列表,每个模型和列表都必须单独处理。

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
                      );

可能是你键名的大写。例如,你有 ContentType 但它应该是 contentType 以小写字母开头的驼峰式。

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
                }
        }
    }
);

只尝试一张 Lex 响应卡。

    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
                }
        }
    }
);