Search code examples
botframework

Formatting text in Hero Card


I need to show formatted text in HeroCard

card = new HeroCard
            {
                Title = taskEntity.SourceEntityName + " : " + taskEntity.SourceEntityInstanceName,
                Subtitle = taskEntity.Description + " : " + $"{taskEntity.CreatedOn:D}",
                Text = await GetDetails(taskEntity),
                Buttons = new List<CardAction>
                {
                    new CardAction {Value = "Approve " + taskEntity.Id, Title = "Approve", Type = ActionTypes.PostBack},
                    new CardAction {Value = "Reject " + taskEntity.Id, Title = "Reject", Type = ActionTypes.PostBack}
                }
            };

GetDetails returns a formatted string with '\r\n' by using 'Environment.NewLine' in it so that each detail is on a new line but it doesn't work. Is there a way without using Adaptive Cards?

Edit: 1. The channel I am testing on is Skype


Solution

  • Rather than '\r\n' please try '\n\n'. In a Skype bot, you can also use '<br/>'

    Here is an example of both:

    if (length > 0 && activity.Text == "hero")
    {
        var reply = ((Activity)context.Activity).CreateReply(string.Empty);
        reply.Attachments = new List<Attachment>();
        var heroCard = new HeroCard()
            {
                Text = "1)Hero Card Text\n\n2)next line!\n\n3)and another new line!"
            };
        reply.Attachments.Add(heroCard.ToAttachment());
        await context.PostAsync(reply);
    }
    else if (length > 0 && activity.Text == "hero html")
    {
        var reply = ((Activity)context.Activity).CreateReply(string.Empty);
        reply.Attachments = new List<Attachment>();
        var heroCard = new HeroCard()
            {
                Text = "Hero Card Text <br/>next line!",
            };
        reply.Attachments.Add(heroCard.ToAttachment());
        await context.PostAsync(reply);
    }
    

    and the result in Skype:

    enter image description here