dshero
dshero

Reputation: 53

Adding the Adaptive Card JSON to the bot script - C#

I am new to Bot Framework and just started using the Echo Bot example using C# . However, in the end, I want to integrate my bot to Teams channel and one of the dialogue flow will need to consolidate information from multiple API and send it to the user.

I figured we can use 'ColumnSet' to display data in table format from how-to-display-data-in-table-format-in-microsoft-bot-framework

Now that I have the adaptive card JSON object (the data in a table), I am not sure where and how do I exactly integrate this adaptive card component to the echo bot script. Few examples showed storing the Adaptive card JSON and reading from a path and sending it as an attachment, but I am still not clear.

Below is the C# bot code (Echobot.cs) in the echo bot project folder. Should a create a different function for implementing the adaptive card ?

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;

namespace Microsoft.BotBuilderSamples.Bots
{
    public class EchoBot : ActivityHandler
    {
        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            var replyText = $"Echo: {turnContext.Activity.Text}";
            await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
        }

        protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            var welcomeText = "Hello and welcome!";
            foreach (var member in membersAdded)
            {
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    await turnContext.SendActivityAsync(MessageFactory.Text(welcomeText, welcomeText), cancellationToken);
                }
            }
        }
    }
}

Upvotes: 2

Views: 2206

Answers (1)

Tim Cadenbach
Tim Cadenbach

Reputation: 1565

here is one example on how to send a card to MS Teams:

                var newActivity = new Activity
            {
                Text = string.Empty,
                Summary = GetActivityText(data,eventData,step),
                Type = ActivityTypes.Message,
                Attachments = new List<Attachment>
                {
                    new Attachment
                    {
                        ContentType = "application/vnd.microsoft.card.adaptive",
                        Content = card
                    },
                },
                Conversation = new ConversationAccount
                {
                    Id = conversationid,
                }
            };
           await turnContext.SendActivityAsync(activity, cancellationToken);

However there are various ways to do that. It depends a litle bit on the event. MessageExtensions for example require a slightly different aproach.

In general you create a new activity, add the card as attachment to the activity and just send it.

As MickyD pointed out in the comment, all that is explained in the docs aswell. Here's a few more infos about this: https://blog.botframework.com/2019/07/02/using-adaptive-cards-with-the-microsoft-bot-framework/

Just search for AdaptiveCards and Botframework and you'll find a lot more from various sources. They're all pretty good.

Upvotes: 2

Related Questions