1

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

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

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

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

4

1 回答 1

1

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

var reply = context.MakeMessage();
var card = new AdaptiveCard();

var choices = new List<Choice>();
choices.Add(new Choice()
{
    Title = "Category 1",
    Value = "c1"
});
choices.Add(new Choice()
{
    Title = "Category 2",
    Value = "c2"                       
});
var choiceSet = new ChoiceSet()
{
    IsMultiSelect = false,
    Choices = choices,
    Style = ChoiceInputStyle.Compact,
    Id = "Category"
};
card.Body.Add(choiceSet);
card.Actions.Add(new SubmitAction() { Title = "Select Category", Data = Newtonsoft.Json.Linq.JObject.FromObject(new { button = "select" }) });

reply.Attachments.Add(new Attachment()
{
    Content = card,
    ContentType = AdaptiveCard.ContentType, 
    Name = $"Card"
});

await context.PostAsync(reply);

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

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);
}
于 2017-08-03T00:53:52.307 回答