Getting Started With Receipt Card Design Using Microsoft Bot Framework

Introduction

The Bot Framework supports different types of rich cards and provides a richer interaction experience to the user. In this article, I will show how to integrate Receipt Card UI design in a Bot application. If you are developing a Shopping Cart Bot chat application, Receipt Card is a very important one.

Receipt card class has the properties like list of items, total, tax, vat, title, and image. We can reuse all the Receipt Card properties and add a tab action event for navigating to the original invoice website.

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
  10. Getting Started With Hero Card Design Using Microsoft Bot Framework
  11. Getting Started With Thumbnail Card Design Using Microsoft Bot Framework
  12. Getting Started With Adaptive Card Design Using Microsoft Bot Framework

Create New Bot Application

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

Microsoft Bot Framework

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

Microsoft Bot Framework

Create New ReceiptCardCardDialog Class

Step 1

You can create a new ReceiptCardDialog class to show the receiptCard dialog. Right click on project > select 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 BotReceiptCard.Dialogs  
  8. {  
  9.     [Serializable]  
  10.     public class ReceiptCardDialog : 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 which 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 = "Your Invoice Details below , if you requred detail click on 'Request Email' Options ";  
  12.   
  13.         await context.PostAsync(welcomeMessage);  
  14.   
  15.         await this.DisplayThumbnailCard(context);  
  16.     }  
  17.     /// <summary>  
  18.     /// DisplayReceiptCard  
  19.     /// </summary>  
  20.     /// <param name="context"></param>  
  21.     /// <returns></returns>  
  22.     public async Task DisplayReceiptCard(IDialogContext context)  
  23.     {  
  24.   
  25.         var replyMessage = context.MakeMessage();  
  26.         Attachment attachment = getBotReceiptCard(); ;  
  27.         replyMessage.Attachments = new List<Attachment> { attachment };  
  28.         await context.PostAsync(replyMessage);  
  29.     }  
After a user enters the first message, the bot will reply with a welcome message like below.

Microsoft Bot Framework

Step 4 Design Receipt Card

The Receipt Card enables a bot to provide a receipt to the user. It typically contains the list of items to include the receipt, tax and total, VAT and title text.

  1. Title - Title text of the card
  2. Facts – Fact is a set of key-value pairs rendered with default style so developers have no need to specify the style
  3. Receipt Items –Receipt Items has the list of item title, image, prize, quantity, subtitle, text and tab navigation for every single item.
  4. Tab – tab enables users to navigate to different screens or actions
  5. Tax – assign tax amount into tax property.
  6. Vat – assign vat amount into vat property.
  7. Total – calculate and assign total amount into total property
  8. Card Actions – adds a list of actions in the Receipt Card. Action card has image, title, type, and value property.

The following code is showing the design code of the online shopping Receipt Card, with image, text, Facts, tab, vat, total and action button. 

  1. /// <summary>  
  2.   /// getBotReceiptCard  
  3.   /// </summary>  
  4.   /// <returns></returns>  
  5.   private static Attachment getBotReceiptCard()  
  6.   {  
  7.       var receiptCard = new ReceiptCard  
  8.       {  
  9.           Title = "Online Shopping",  
  10.           Facts = new List<Fact> { new Fact("Name:","Nikhil"),  
  11.               new Fact("Address:","Bangalore"),  
  12.               new Fact("------------------------",""),  
  13.               new Fact("Phone :97420XXXX2",""),  
  14.               new Fact("Email:[email protected]",""),  
  15.               new Fact("Order Number:97421",""),  
  16.               new Fact("Payment Method : Master 5555-****"""),  
  17.               new Fact("------------------------","")  
  18.           },  
  19.           Items = new List<ReceiptItem>  
  20.   {  
  21.         
  22.       new ReceiptItem("Hit Refresh",subtitle:"by Satya Nadella (Author)",text:"Kindle Edition", price: "278.31", quantity: "1", image: new CardImage(url: "https://images-eu.ssl-images-amazon.com/images/I/41eAfVuUzeL.jpg"),tap: new CardAction("Read More")),  
  23.       new ReceiptItem("XamarinQA",subtitle:"by Suthahar J (Author)",text:"Kindle Edition", price: "116.82", quantity: "1", image: new CardImage(url: "https://images-eu.ssl-images-amazon.com/images/I/51z6GXy3FSL.jpg"),tap: new CardAction("Read More")),  
  24.       new ReceiptItem("Surface Pro 4",subtitle:"Core i5 - 6th Gen/4GB/128GB/Windows 10 Pro/Integrated Graphics/31.242 Centimeter Full HD Display", price: "66,500", quantity: "1", image: new CardImage(url: "https://images-na.ssl-images-amazon.com/images/I/41egJVu%2Bc0L.jpg"),tap: new CardAction("Read More")),  
  25.       new ReceiptItem("Windows 10 pro",subtitle:"by Microsoft", price: "13,846", quantity: "1", image: new CardImage(url: "https://images-na.ssl-images-amazon.com/images/I/7176wliQYsL._SL1500_.jpg"),tap: new CardAction("Read More"))  
  26.   },  
  27.           Tax = "2000",  
  28.           Total = "82,741.13",  
  29.           Tap =new CardAction(ActionTypes.OpenUrl,value: "https://www.microsoft.com/en-in/store/b/home?SilentAuth=1&wa=wsignin1.0"),  
  30.           Buttons = new List<CardAction>  
  31.   {  
  32.       new CardAction(  
  33.           ActionTypes.OpenUrl,  
  34.           "Request Email",  
  35.           "https://assets.onestore.ms/cdnfiles/external/uhf/long/9a49a7e9d8e881327e81b9eb43dabc01de70a9bb/images/microsoft-gray.png",  
  36.           "mailto:[email protected]?subject=Order%20Number:97421&body=Hi%team,%20I%need%20Invoice")  
  37.   }  
  38.       };  
  39.   
  40.       return receiptCard.ToAttachment();  
  41.   }  
The above code will generate the Receipt Card and reply to the user like below.

Microsoft Bot Framework

Run Bot Application

The emulator is a desktop application that lets us test & 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 localhost URL and paste it in 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. Just click on "Connect".

    Microsoft Bot Framework

Summary

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


Similar Articles