Messages exchanged between a user and a bot can contain media such as images, video, etc. The Bot Framework SDK supports the task of sending rich messages to the user.
In this tutorial, you will learn the following.
- Design Hero Card and Response with Images.
- Design the Video Card and Response with Video.
- Build and Publish the Solution.
- Test your bot with Web Chat Client.
Below are the prerequisites.
- The bot created in the previous article to help you understand the simple QnA Maker Bot
- Response card can be designed with different media response, i.e., Image & Video
Let's understand the architecture flow with respect to this article.
Primary bot communicates with the user through message activities.
To send a simple text message, use turn context object SendActivityAsync method to send a single message response. You can use SendActivitiesAsync method to send multiple messages.
- await turnContext.SendActivityAsync($"Welcome!");
To receive a simple text message, use text property of activity object.
- var responseMessage = turnContext.Activity.Text;
Some message activities consist of simple text and others may consist of richer context such as Cards, i.e., Hero Card, Video Card etc. Let’s start designing the cards.
Step 1 - Design Hero Card and Response with Images
Hero card allows you to combine images and buttons in one object, and send them to the user.
To process with rich card i.e. Hero Card, the below properties are required.
Type
|
Description
|
Tile
|
Title of the hero card
|
Sub Title
|
Sub Title of the hero card
|
Text
|
Text or Description need to display the below image
|
Images
|
Image URL which needs to be rendered
|
Buttons
|
Button with the click event, clicking allows the user to navigate
|
- public static HeroCard GetHeroCard()
- {
- var heroCard = new HeroCard
- {
- Title = "<<Title>>",
- Subtitle = "<<SubTitle>>",
- Text = "<<Secription>>",
- Images = new List<CardImage> { new CardImage("<<Image Path>>") },
- Buttons = new List<CardAction> { new CardAction(ActionTypes.OpenUrl, "Get Started", value: "<<Re-direct Url>>") },
- };
- return heroCard;
- }
QnAMaker Hero Card Response Design
Set Response design with Hero Card values.
Step 2 - Design Video Card and Response with Video
Video Card allow you to play video. Typically it's used to open a URL and stream an available video.
To process with rich card i.e. Video Card, the below properties are required.
Type
|
Description
|
Tile
|
Title of the hero card
|
Sub Title
|
Sub Title of the hero card
|
Text
|
Text or Description need to display the below image
|
Images
|
Image URL which needs to be rendered
|
Media
|
URL, i.e., video URL to stream any available video
|
Buttons
|
Button with the click event, Onclick allows the user to navigate
|
- public static VideoCard GetVideoCard(string[] qnaAnswerData)
- {
- var videoCard = new VideoCard
- {
- Title = <<”Title”>>,
- Subtitle = <<”Title”>>,
- Text = <<”Title”>>,
- Image = new ThumbnailUrl
- {
- Url = <<”Title”>>,
- },
- Media = new List<MediaUrl>
- {
- new MediaUrl()
- {
- Url = <<”Title”>>,
- },
- },
- Buttons = new List<CardAction>
- {
- new CardAction()
- {
- Title = "Learn More",
- Type = ActionTypes.OpenUrl,
- Value = <<”Title”>>,
- },
- },
- };
- return videoCard;
- }
QnaMaker Video Card Response Design.
Set Response design with Video Card values.
Step 3 - Build and Publish the Solution
Navigate to EchoBot.cs file and add a condition for Hero Card and Video Card response and design.
Replace the AccessQnAMaker code with the below section. It checks the response parameter and design card. If the response has 5 parameters, it calls the Hero Card method. If the response has 6 parameters, it calls Video Card method else replies with simple text.
- private async Task AccessQnAMaker(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
- {
- var results = await EchoBotQnA.GetAnswersAsync(turnContext);
- string[] qnaAnswerData = results[0].Answer.Split(';');
- int dataSize = qnaAnswerData.Length;
- if (results.Any())
- {
- var attachments = new List<Attachment>();
- // Reply to the activity we received with an activity.
- var reply = MessageFactory.Attachment(attachments);
- //image card have 5 paramters
- if (dataSize == 5)
- {
- reply.Attachments.Add(Cards.GetHeroCard(qnaAnswerData).ToAttachment());
- await turnContext.SendActivityAsync(reply, cancellationToken);
- }
- //video card have 6 parameter
- else if (dataSize == 6)
- {
- reply.Attachments.Add(Cards.GetVideoCard(qnaAnswerData).ToAttachment());
- await turnContext.SendActivityAsync(reply, cancellationToken);
- }
- else
- {
- await turnContext.SendActivityAsync(MessageFactory.Text(results.First().Answer), cancellationToken);
- }
- }
- else
- {
- await turnContext.SendActivityAsync(MessageFactory.Text("Sorry, could not find an answer in the Q and A system."), cancellationToken);
- }
- }
Right-click the solution. Select "Build and publish the solution"with the help of published profile.
Step 4 - Test your bot with Web Chat Client
Navigate to Azure WebApp Bot and select Test in WebChat client.
The previous article can be found from these links.
I hope you have enjoyed and learned something new in this article. Thanks for reading and stay tuned for the next article.