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

时间:2018-10-12 13:40:02

标签: botframework luis adaptive-cards

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

但是,在Microsoft LUIS Bot模型下不是一个选择,这是我拥有的代码,该代码从SQL DB搜索返回员工查找结果:

    [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返回,如何实现?

2 个答案:

答案 0 :(得分:0)

马克, 您需要将自适应卡制作为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);

答案 1 :(得分:0)

这是我最终不得不使用以使其成功的代码:

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