AdaptiveCard内部的C#Botframework循环

时间:2018-08-27 20:47:40

标签: c# for-loop foreach botframework adaptive-cards

我已经创建了一个对话框来评估我们的聊天机器人(C#botframework)中的公司顾问,但是我不能在AdaptiveCard中使用foreach或foreach。我需要为List Pergunta中的每个项目创建一个TextBlock()和一个ChoiceSet()块,但是我无法使用foreach来实现。有什么建议或更好的主意来创建此循环吗?

这是我在C#中完整的对话框代码。

using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Collections.Generic;
using AdaptiveCards;
using SimpleEchoBot.Formulário;

namespace SimpleEchoBot.Dialogs
{
    [Serializable]
    public class RatingDialog : IDialog<EntidadeCliente>
    {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(this.MessageReceivedAsync);
            return Task.CompletedTask;
        }

        public async virtual Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
        {
            var replyMessage = context.MakeMessage();

            Attachment attachment = null;

            attachment = CriarAdapativecard();
            replyMessage.Attachments = new List<Attachment> { attachment };
            await context.PostAsync(replyMessage);
        }       

        public Attachment CriarAdapativecard()
        {

            AdaptiveCard card = new AdaptiveCard()
            {
                Body = new List<CardElement>()
                {
                    new Container()
                    {                        
                        Items = new List<CardElement>()
                        {
                            new ColumnSet()
                            {
                                Columns = new List<Column>()
                                {
                                    new Column()
                                    {
                                        Size = ColumnSize.Auto,
                                        Items = new List<CardElement>()
                                        {
                                            new Image()
                                            {
                                                Url = "D:/EvaWeb.png",
                                                Size = ImageSize.Medium,
                                                Style = ImageStyle.Person
                                            }
                                        }
                                    },
                                    new Column()
                                    {
                                        Size = ColumnSize.Auto,
                                        Items = new List<CardElement>()
                                        {
                                            new TextBlock()
                                            {
                                                Text =  "Olá, temos uma novidade!",
                                                Weight = TextWeight.Bolder,
                                                IsSubtle = true
                                            },
                                            new TextBlock()
                                            {
                                                Text = "Gostaria de iniciar a avaliação do consultor?",
                                                Wrap = true
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                // Buttons
                Actions = new List<ActionBase>()
                {

                    new ShowCardAction()
                    {
                        Title = "Sim",                        
                        Speak = "<s>Sim</s>",                        
                        Card = AvaliarConsultor("")
                    },
                    new ShowCardAction()
                    {
                        Title = "Não",
                        Speak = "<s>Não</s>",                        
                        Card = PularAvaliacao()
                    },
                }
            };

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

            return attachment;
        }

        private static AdaptiveCard PularAvaliacao()
        {
            //does nothing on No option from user
            return new AdaptiveCard()
            { };
        }

        private static AdaptiveCard AvaliarConsultor(List<string> Pergunta)
        {            

            var adaptiveCard = new AdaptiveCard()
            {
                Body = new List<CardElement>()
                {
                    new TextBlock()
                    {
                        Text = "Avaliação de questionário 1!",
                        Weight = TextWeight.Bolder,
                        Size = TextSize.Large
                    },

                        new TextBlock() { Text = Pergunta[0]},
                        new ChoiceSet()
                        {

                            Id = "nota",
                            Style = ChoiceInputStyle.Compact,
                            IsMultiSelect = false,
                            Choices = new List<Choice>()
                            {
                                new Choice()
                                {
                                    Title = "⭐",
                                    Value = "1"
                                },

                                new Choice()
                                {
                                    Title = "⭐⭐",
                                    Value = "2"
                                },
                                new Choice()
                                {
                                    Title = "⭐⭐⭐",
                                    Value = "3"
                                },
                                new Choice()
                                {
                                    Title = "⭐⭐⭐⭐",
                                    Value = "4"
                                },
                                new Choice()
                                {
                                    Title = "⭐⭐⭐⭐⭐",
                                    Value = "5"
                                }
                            }
                        }

                },

                Actions = new List<ActionBase>()
                {
                    new SubmitAction()
                    {
                        Title = "Registrar",
                        Speak = "<s>Registrar avaliação</s>",
                        DataJson = "{ \"Type\": \"Registrar\" }"
                    }
                }
            };         
            return adaptiveCard;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

我需要为List Pergunta中的每个项目创建一个TextBlock()和一个ChoiceSet()块,但是我无法使用foreach来实现。

要满足您的要求,请参考以下代码段。

private static AdaptiveCard AvaliarConsultor(List<string> Pergunta)
{

    var adaptiveCard = new AdaptiveCard()
    {
        Body = new List<CardElement>()
        {
            new TextBlock()
            {
                Text = "Avaliação de questionário 1!",
                Weight = TextWeight.Bolder,
                Size = TextSize.Large
            },


        },

        Actions = new List<ActionBase>()
        {
            new SubmitAction()
            {
                Title = "Registrar",
                Speak = "<s>Registrar avaliação</s>",
                DataJson = "{ \"Type\": \"Registrar\" }"
            }
        }
    };

    //dynamically generate TextBlock and ChoiceSet
    //and then add to AdaptiveCard Body

    foreach (var item in Pergunta)
    {
        var textblock = new TextBlock() { Text = item };
        var choiceset = new ChoiceSet()
        {

            Id = "nota",
            Style = ChoiceInputStyle.Compact,
            IsMultiSelect = false,
            Choices = new List<Choice>()
                    {
                        new Choice()
                        {
                            Title = "⭐",
                            Value = "1"
                        },

                        new Choice()
                        {
                            Title = "⭐⭐",
                            Value = "2"
                        },
                        new Choice()
                        {
                            Title = "⭐⭐⭐",
                            Value = "3"
                        },
                        new Choice()
                        {
                            Title = "⭐⭐⭐⭐",
                            Value = "4"
                        },
                        new Choice()
                        {
                            Title = "⭐⭐⭐⭐⭐",
                            Value = "5"
                        }
                    }
        };

        adaptiveCard.Body.Add(textblock);
        adaptiveCard.Body.Add(choiceset);
    }

    return adaptiveCard;
}

测试结果:

https://codepen.io/anon/pen/WgvgVB?editors=0010#0