【问题标题】:How do I Attach an AdaptiveCardFromJson in a LUIS Bot C#?如何在 LUIS Bot C# 中附加 AdaptiveCardFromJson?
【发布时间】:2019-03-17 18:13:42
【问题描述】:

我最近问了一个类似的问题,但不够具体。我看到有一些带有 AdaptiveCards NuGet 包的代码可以附加一个 AdaptiveCardFromJson 和 AdaptiveCardFromSDK,这在正常的 Microsoft Bot 模型下是可用的。

但是,在 Microsoft LUIS 机器人模型下不是一个选项,这是我拥有的代码,它从 SQL 数据库搜索返回员工查找结果:

    [LuisIntent("Who_is_Employee")]
    public async Task Who_is_EmployeeIntent(IDialogContext context, LuisResult result)
    {
        EntityRecommendation recommendation;
        if (result.TryFindEntity("Communication.ContactName", out recommendation))
        {
            List<Employee> results = EmployeeService.FindEmployees(recommendation.Entity);
            if (results.Count > 0)
            {
                string response = "";
                foreach (Employee e in results)
                {
                    string name = e.FullName;
                    string title = e.JobTitle;
                    response += " " + name + " " + title + "\n";
                }
                await context.PostAsync(response);
            }
        }
        else
        {
            await context.PostAsync(" Sorry, I couldn't find who you were looking for.");
        }
    }

我希望以 AdaptiveCard 的形式返回该信息,我该如何实现?

【问题讨论】:

    标签: botframework azure-language-understanding adaptive-cards


    【解决方案1】:

    马克, 您需要将自适应卡制作为 json 或使用 SDK 创建 AdaptiveCard 的实例。 Here 是了解更多信息的好地方。

    制作卡片并拥有 AdaptiveCard 类的实例后,您需要创建一条新消息并将卡片附加到该消息中。新消息是您将发回给用户的内容。

    代码看起来像这样

    var card = AdaptiveCard.FromJson(<your json here>);
    
    Attachment attachment = new Attachment()
    {
        ContentType = AdaptiveCard.ContentType,
        Content = card
    };
    
    var myRespsonse = context.MakeMessage();
    myRespsonse.Attachments.Add(attachment);
    
    await context.PostAsync(myRespsonse, CancellationToken.None);
    

    【讨论】:

      【解决方案2】:

      这是我最终必须使用的代码才能成功:

              [LuisIntent("Who_is_Employee")]
          public async Task Who_is_EmployeeIntent(IDialogContext context, LuisResult result)
          {
              EntityRecommendation recommendation;
              if (result.TryFindEntity("Communication.ContactName", out recommendation))
              {
                  List<Employee> results = EmployeeService.FindEmployees(recommendation.Entity);
                  if (results.Count > 0)
                  {
                      /* Single line per result */
                      /*
                      string response = "";
                      foreach (Employee e in results)
                      {
                          string name = e.FullName;
                          string title = e.JobTitle;
                          response += " " + name + " " + title + "\n";
                      }
                      await context.PostAsync(response);
                      */
      
                      /* Adaptive card per result */
                      // Load json template
                      string physicalPath = System.Web.HttpContext.Current.Server.MapPath("../AdaptiveCards/EmployeeLookup.json");
                      string jsonTemplate = "";
                      using (StreamReader r = new StreamReader(physicalPath))
                      {
                          jsonTemplate = r.ReadToEnd();
                      }
      
                      var respsonse = context.MakeMessage();
      
                      foreach (Employee e in results)
                      {
                          string employeeJson = jsonTemplate;
      
                          employeeJson = employeeJson.Replace("{{FullName}}", e.FullName);
                          employeeJson = employeeJson.Replace("{{JobTitle}}", e.JobTitle);
                          employeeJson = employeeJson.Replace("{{Reference}}", e.Reference);
                          employeeJson = employeeJson.Replace("{{Phone}}", e.Phone);
                          employeeJson = employeeJson.Replace("{{Email}}", e.Email);
                          employeeJson = employeeJson.Replace("{{Mobile}}", e.Mobile);
      
                          AdaptiveCard card = AdaptiveCard.FromJson(employeeJson).Card;
      
                          Attachment attachment = new Attachment()
                          {
                              ContentType = AdaptiveCard.ContentType,
                              Content = card
                          };
                          respsonse.Attachments.Add(attachment);
                      }
      
                      await context.PostAsync(respsonse);
                  }
              }
              else
              {
                  await context.PostAsync(" Sorry, I couldn't find who you were looking for.");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        相关资源
        最近更新 更多