Getting Started With Thumbnail 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 Thumbnail card UI design in a Bot application. The Thumbnail card is a multipurpose card that contains small image, title text, subtext, multiple buttons, and a card tap action.

A Hero Card and a Thumbnail Card have the difference in the size of image and text alignment of the card.

Microsoft Bot Framework

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 and select File > Create New Project (Ctrl + Shift +N) > select Bot application.

Microsoft Bot Framework

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

Microsoft Bot Framework

Create New ThumbnailCardDialog Class

Step 1

You can create a new ThumbnailCardDialog class to show the ThumbnailCard dialog. Right-click on Project > Add New Item > create a class that is marked with the [Serializable] attribute (so 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 BotThumbnailCard.Dialogs  
  8. {  
  9.     [Serializable]  
  10.     public class ThumbnailCardDialog : IDialog<object>  
  11.     {  

Step 2

IDialog interface has only StartAsync() method. StartAsync() is called when the dialog becomes active. The method passes the IDialogContext object 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 to 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 Thumbnail Card Demo";  
  12.   
  13.          await context.PostAsync(welcomeMessage);  
  14.   
  15.          await this.DisplayThumbnailCard(context);  
  16.      }  
  17.      /// <summary>  
  18.      /// DisplayThumbnailCard  
  19.      /// </summary>  
  20.      /// <param name="context"></param>  
  21.      /// <returns></returns>  
  22.      public async Task DisplayThumbnailCard(IDialogContext context)  
  23.      {  
  24.   
  25.          var replyMessage = context.MakeMessage();  
  26.          Attachment attachment = GetProfileThumbnailCard(); ;  
  27.          replyMessage.Attachments = new List<Attachment> { attachment };  
  28.          await context.PostAsync(replyMessage);  
  29.      }  

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

Microsoft Bot Framework

Step 4 Design Thumbnail Card

The Thumbnail Card typically contains a small image with left alignment, one or more buttons, and text. Thumbnail 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 small image with left alignment
  5. Buttons - One or more buttons. The Skype allow only 5 buttons will 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 of the user profile message with image, text, subtext, and action button. 
    1. /// <summary>  
    2.     /// GetProfileThumbnailCard  
    3.     /// </summary>  
    4.     /// <returns></returns>  
    5.     private static Attachment GetProfileThumbnailCard()  
    6.     {  
    7.         var thumbnailCard = new ThumbnailCard  
    8.         {  
    9.             // title of the card  
    10.             Title = "Suthahar Jegatheesan",  
    11.             //subtitle of the card  
    12.             Subtitle = "Microsoft certified solution developer",  
    13.             // navigate to page , while tab on card  
    14.             Tap = new CardAction(ActionTypes.OpenUrl, "Learn More", value: "http://www.devenvexe.com"),  
    15.             //Detail Text  
    16.             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",  
    17.             // smallThumbnailCard  Image  
    18.             Images = new List<CardImage> { new CardImage("http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/AuthorImage/jssuthahar20170821011237.jpg") },  
    19.             // list of buttons   
    20.             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/") }  
    21.         };  
    22.   
    23.         return thumbnailCard.ToAttachment();  
    24.     }  

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

Microsoft Bot Framework

Run Bot Application

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

Microsoft 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. You can 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 "Connect".

    Microsoft Bot Framework

Summary

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

Up Next
    Ebook Download
    View all
    Learn
    View all