Getting Started With Hero Card Design Using Microsoft Bot Framework

Introduction

The Bot Framework supports different types of rich cards and provides a richer interaction experience to the users. In this article, I will show how to integrate Hero Card UI design in a Bot application. The Hero card is a multipurpose card and contains an image, title text, subtext, multiple buttons, and a card tap action.

Prerequisite

I have explained about Bot framework installation, deployment, and implementation in the below articles.

  1. Getting Started with Chatbot Using Azure Bot Service
  2. Getting Started with Bots Using Visual Studio 2017
  3. Deploying A Bot to Azure Using Visual Studio 2017
  4. How to Create ChatBot In Xamarin
  5. Getting Started with Dialog Using Microsoft Bot Framework
  6. Getting Started with Prompt Dialog Using Microsoft Bot Framework
  7. Getting Started With Conversational Forms And FormFlow Using Microsoft Bot Framework
  8. Getting Started With Customizing A FormFlow Using Microsoft Bot Framework
  9. Sending Bot Reply Message With Attachment Using Bot Framework

Create New Bot Application

Let's create a new bot application using Visual Studio 2017. Open Visual Studio. Go to File > New Project (Ctrl + Shift +N) > Bot application.

Bot Framework

The Bot application template gets created with all the components and all required NuGet references installed in the solution.

Bot Framework

Create a new HeroCardDialog class

Step 1

You can create a new HeroCardDialog class to show the HeroCard dialog. Right-click on Project > select Add New Item > create a class that is marked with the [Serializable] attribute (so that the dialog can be serialized to state) and implement the IDialog interface.

  1. using Microsoft.Bot.Builder.Dialogs;  
  2. using Microsoft.Bot.Connector;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace BotHeroCard.Dialogs  
  8. {  
  9.     [Serializable]  
  10.     public class HeroCardDialog : IDialog<object>  
  11.     {   

Step 2

IDialog interface has only StartAsync() method. StartAsync() is called when the dialog becomes active. The method has passed the IDialogContext object that is used to manage the conversation.

  1. public async Task StartAsync(IDialogContext context)  
  2.         {  
  3.             context.Wait(this.MessageReceivedAsync);  
  4.         }  

Step 3

Create a MessageReceivedAsync method and write the following code for the welcome message and show the list of demo options dialog.

  1. /// <summary>  
  2.       /// MessageReceivedAsync  
  3.       /// </summary>  
  4.       /// <param name="context"></param>  
  5.       /// <param name="result"></param>  
  6.       /// <returns></returns>  
  7.       public async virtual Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)  
  8.       {  
  9.           var message = await result;  
  10.           var welcomeMessage = context.MakeMessage();  
  11.           welcomeMessage.Text = "Welcome to bot Hero Card Demo";  
  12.   
  13.           await context.PostAsync(welcomeMessage);  
  14.      
  15.          await this.DisplayHeroCard(context);  
  16.       }  
  17.       /// <summary>  
  18.       ///   
  19.       /// </summary>  
  20.       /// <param name="context"></param>  
  21.       /// <returns></returns>  
  22.       public async Task DisplayHeroCard(IDialogContext context)  
  23.       {  
  24.   
  25.           var replyMessage = context.MakeMessage();  
  26.           Attachment attachment = GetProfileHeroCard(); ;  
  27.           replyMessage.Attachments = new List<Attachment> { attachment };  
  28.           await context.PostAsync(replyMessage);  
  29.       }  

After the user enters the first message, the bot will reply with a welcome message, like below.

Bot Framework

Step 4 Design Hero Card

The Hero Card typically contains a large image, one or more buttons, and text. Hero Card class has the following properties.

  1. Title - Title text of the card
  2. Subtitle -subtitle text for the title
  3. Text – Summary text to display on the card
  4. Images – Display large image
  5. Buttons - One or more buttons. The Skype allows only 5 buttons to display on a card. If you have more buttons, you can create two cards.
  6. Tap - An action that is triggered when a user taps on the card.

The following code is used for showing the design to the user profile message with image, text, subtext, and action button.

  1. private static Attachment GetProfileHeroCard()  
  2.   {  
  3.       var heroCard = new HeroCard  
  4.       {  
  5.           // title of the card  
  6.           Title = "Suthahar Jegatheesan",  
  7.           //subtitle of the card  
  8.           Subtitle = "Microsoft certified solution developer",  
  9.           // navigate to page , while tab on card  
  10.           Tap = new CardAction(ActionTypes.OpenUrl, "Learn More", value: "http://www.devenvexe.com"),  
  11.           //Detail Text  
  12.           Text = "Suthahar J is a Technical Lead and C# Corner MVP. He has extensive 10+ years of experience working on different technologies, mostly in Microsoft space. His focus areas are  Xamarin Cross Mobile Development ,UWP, SharePoint, Azure,Windows Mobile , Web , AI and Architecture. He writes about technology at his popular blog http://devenvexe.com",  
  13.           // list of  Large Image  
  14.           Images = new List<CardImage> { new CardImage("http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/AuthorImage/jssuthahar20170821011237.jpg") },  
  15.           // list of buttons   
  16.           Buttons = new List<CardAction> { new CardAction(ActionTypes.OpenUrl, "Learn More", value: "http://www.devenvexe.com") , new CardAction(ActionTypes.OpenUrl, "C# Corner", value: "http://www.c-sharpcorner.com/members/suthahar-j"), new CardAction(ActionTypes.OpenUrl, "MSDN", value: "https://social.msdn.microsoft.com/profile/j%20suthahar/") }  
  17.       };  
  18.   
  19.       return heroCard.ToAttachment();  
  20.   }  

The above code will generate a Hero Card and reply to the user.

Bot Framework

Run Bot Application

The emulator is a desktop application that lets us test and debug our bot on localhost. Now, you can click on "Run the application" in Visual Studio and execute in the browser.

Bot Framework
Test application on Bot Emulator

You can follow the below steps to test your bot application.

  1. Open Bot Emulator.
  2. Copy the above localhost URL and paste it in the emulator, e.g., - http://localHost:3979
  3. Append the /api/messages in the above URL; e.g. - http://localHost:3979/api/messages.
  4. You won't need to specify Microsoft App ID and Microsoft App Password for localhost testing. So, click on "Connect".

    Bot Framework

Summary

In this article, you learned how to create a Bot application using Visual Studio 2017 and create Hero Card design using it. If you have any questions/feedback/issues, please write in the comment box.


Similar Articles