如何在 LUIS Bot C# 中附加 AdaptiveCardFromJson?

How do I Attach an AdaptiveCardFromJson in a LUIS Bot C#?

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

但是,在 Microsoft LUIS 机器人模型下不是一个选项,这里是我的代码,returns 来自 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 返回,我该如何实现?

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

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

代码看起来像这样

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

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

        [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.");
        }
    }