C# Botframework Loop inside of AdaptiveCard

馋奶兔 提交于 2021-01-28 10:43:57

问题


I have created one dialog to evaluate our company consultant in our chatbot (C# botframework), but I can't use for or foreach inside of AdaptiveCard. I need to create one TextBlock() and one ChoiceSet() block for each item from List Pergunta, but I can't do it with foreach. Any suggestion or better idea to create this loop ?

Here is my complete dialog code in 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:


I need to create one TextBlock() and one ChoiceSet() block for each item from List Pergunta, but I can't do it with foreach.

To achieve your requirement, please refer to the following code snippet.

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

Test result:



来源:https://stackoverflow.com/questions/52046755/c-sharp-botframework-loop-inside-of-adaptivecard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!