将自适应卡组合框的结果传递给另一个方法

时间:2017-08-02 12:33:08

标签: c# botframework adaptive-cards

我正在使用自适应卡组合框来显示某些类别的产品。一旦用户点击该类别应传递给另一种方法的类别,并在另一个自适应卡组合框中显示该类别的所有产品,并让用户选择产品。

以下是将所有类别添加到组合框的代码。

    public async Task GetCategoryAdaptiveCard(IDialogContext context)
    {
        var replyToConversation = context.MakeMessage();
        replyToConversation.Attachments = new List<Attachment>();

        HttpResponseMessage response = new HttpResponseMessage();
        string query = string.Format(APIChatBot + "/AllCategory");

        using (var client = ClientHelper.GetClient())
        {
            response = await client.GetAsync(query);
        }
        var categoryList = await response.Content.ReadAsAsync<IEnumerable<CategoryDTO>>();
        AdaptiveCard adaptiveCard = new AdaptiveCard();
        adaptiveCard.Speak = "Please Select A Category from the list";

        adaptiveCard.Body.Add(new TextBlock()
        {
            Text = "Please Select A Category from the list",
            Size = TextSize.Normal,
            Weight = TextWeight.Normal
        });

        adaptiveCard.Body.Add(new TextBlock()
        {
            Text = "Category List",
            Size = TextSize.Normal,
            Weight = TextWeight.Normal
        });

        List<AdaptiveCards.Choice> list = new List<AdaptiveCards.Choice>();

        foreach (var item in categoryList)
        {
            AdaptiveCards.Choice choice = new AdaptiveCards.Choice()
            {
                Title = item.CategoryName,
                Value = item.Id.ToString()
            };

            list.Add(choice);
        }
        adaptiveCard.Body.Add(new ChoiceSet()
        {
            Id = "Category",
            Style = ChoiceInputStyle.Compact,
            Choices = list
        });

        Attachment attachment = new Attachment()
        {
            ContentType = AdaptiveCard.ContentType,
            Content = adaptiveCard
        };

        replyToConversation.Attachments.Add(attachment);
        await context.PostAsync(replyToConversation);
    }

以下是我用于获取所选类别的产品的方法。

   public async Task GetProductForCategory(IDialogContext context, string category)
    {
        var replyToConversation = context.MakeMessage();
        replyToConversation.Attachments = new List<Attachment>();

        HttpResponseMessage response = new HttpResponseMessage();
        string query = string.Format(APIChatBot + "/ProductByCategory/" + category);

        using (var client = ClientHelper.GetClient())
        {
            response = await client.GetAsync(query);
        }

        var productList = await response.Content.ReadAsAsync<IEnumerable<ProductDTO>>();

        if(productList .Count() == 0)
        {
            string message = "Sorry There Are No products For this Category" + category;
           await context.PostAsync(message);
        }
        else
        {
            List<AdaptiveCards.Choice> list = new List<AdaptiveCards.Choice>();

            foreach (var item in productList )
            {
                AdaptiveCards.Choice choice = new AdaptiveCards.Choice()
                {
                    Title = item.ProductName,
                    Value = item.Id.ToString()
                };

                list.Add(choice);
            }

            AdaptiveCard adaptiveCard = new AdaptiveCard();
            adaptiveCard.Body.Add(new TextBlock()
            {
                Text = "List of Products for the Category " + category,
                Size = TextSize.Normal,
                Weight = TextWeight.Normal
            });

            adaptiveCard.Body.Add(new TextBlock()
            {
                Text = "Please Select A Product From The List",
                Size = TextSize.Normal,
                Weight = TextWeight.Normal
            });

            adaptiveCard.Body.Add(new ChoiceSet()
            {
                Id = "ProductForCategory",
                Style = ChoiceInputStyle.Compact,
                Choices = list
            });

            Attachment attachment = new Attachment()
            {
                ContentType = AdaptiveCard.ContentType,
                Content = adaptiveCard
            };

            replyToConversation.Attachments.Add(attachment);
            await context.PostAsync(replyToConversation);
        }
    }

如何将用户选择的类别传递给根据类别选择产品的方法?

1 个答案:

答案 0 :(得分:1)

如果您创建这样的自适应卡:

class CategorySelection
{
    public string Category { get; set; }
}

var activityValue = activity.Value as Newtonsoft.Json.Linq.JObject;
if (activityValue != null)
{
    var categorySelection = activityValue.ToObject<CategorySelection>();
    var category = categorySelection.Category;
    //query the database for products of this category type, 
    //create another adaptive card displaying the products and send to user

    await context.PostAsync(reply);
}

当用户选择其中一个选项并单击该按钮时,生成的活动的值将是您可以反序列化并用于创建特定于产品的自适应卡的作业:

{{1}}