【问题标题】:How to send a response card using AWS Lambda in C#如何在 C# 中使用 AWS Lambda 发送响应卡
【发布时间】:2020-09-27 02:31:29
【问题描述】:

您好,我正在亚马逊 lex 上开发一个聊天机器人,我想使用 lambda 函数发送响应卡,但是在关闭响应格式中使用响应卡函数时,它会给出空异常的错误。谁能告诉它的解决方案? 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 Project\Abbrar Projects\ 中 EVS_Test_Abbar_Lambda_Function.OrderWatchIntentProcessorTest.Process(LexEvent lexEvent, ILambdaContext context) 处的 NullReferenceException EVS_Test_Abbar_Lambda_Function\EVS_Test_Abbar_Lambda_Function\OrderWatchIntentProcessorTest.cs:第 52 行 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\F 在 lambda_method(Closure , Stream , Stream , LambdaContextInternal )

【问题讨论】:

  • 请发布您的堆栈跟踪,以便我们查看它不喜欢什么

标签: c# aws-lambda chatbot aws-lex aws-sdk-net


【解决方案1】:

这是它的解决方案,因为如果您查看 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
                      );

【讨论】:

    【解决方案2】:

    只尝试一张 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
                    }
            }
        }
    );
    

    【讨论】:

      【解决方案3】:

      这可能是您对键名的大写。例如你有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
                      }
              }
          }
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-21
        • 2020-02-11
        • 2021-10-23
        • 2020-04-03
        • 1970-01-01
        • 2017-05-20
        • 1970-01-01
        相关资源
        最近更新 更多