Search code examples
botframeworkazure-language-understandingadaptive-cards

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


I asked a similar question recently but wasn't specific enough. I see that there is some code with the AdaptiveCards NuGet Package to attach an AdaptiveCardFromJson and AdaptiveCardFromSDK, which under a the normal Microsoft Bot Model is available.

However, under the Microsoft LUIS Bot Model isn't an option, here's the code I have which returns an employee lookup result from a SQL DB Search:

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

I would like that information to be returned as an AdaptiveCard, how do I achieve this?


Solution

  • This was the code I ended up having to use to make this successful:

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