Using Dialogs in Microsoft Bot Framework (Creating lead in CRM)

Let us continue with our previous post on Bot Framework

Here we will implement our own dialog.

Dialogs are basically a class which implements IDialog interface. Dialog sends a message to the user and is in suspended state till it waits for the response from the user. The state is saved in State Service provided by Bot Connector Service.

Here MyDialog is our dialog class which implements the IDialog Interface i.e. implement StartAsync method which would be the first method called. We also need to mark our class as Serializable.

Next we need to update our controller class to point it to our new MyDialog class.

Here we will ask the user about the product in which he is interested, then get the name and description and finally using this information we will create a Lead record inside CRM.

The lead record created in CRM.

Here we are making use of PromptDialog type for managing interactions with the user. Using PromptDialog we can easily prompt user for text, choice, attachment, confirmation etc.

The context here is the IDialogContext. In resume parameter, we can specify which dialog methods to be called next after the user has responded. The response from the user is passed to the subsequent dialog methods.

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Bot_Application1.Dialogs

public enum InterestOptions

// Decorate the class with Serializable attribute
// Implement IDialog Interface
public class MyDialog : IDialog
InterestOptions interestOptions;
string name;
string description;

// Start Sysnc is the first method which is called
public async Task StartAsync(IDialogContext context)
await context.PostAsync("Hi how may i help you?");
// wait for the user's response

public virtual async Task MessageRecieveAsync(IDialogContext context, IAwaitable argument)
// get the message
var message = await argument;

if (message.Text.Contains("interested"))
context: context,
resume: ResumeGetInterest,
options: (IEnumerable<InterestOptions>)Enum.GetValues(typeof(InterestOptions)),
prompt: "Which product are your interested in :",
retry: "I didn't understand. Please try again.");


public async Task ResumeGetInterest(IDialogContext context, IAwaitable result)
interestOptions = await result;

context: context,
resume: ResumeGetName,
prompt: "Please provide your name",
retry: "I didn't understand. Please try again.");

public async Task ResumeGetName(IDialogContext context, IAwaitable result)
name = await result;

context: context,
resume: ResumeGetDescription,
prompt: "Please provide a detailed description",
retry: "I didn't understand. Please try again.");

public async Task ResumeGetDescription(IDialogContext context, IAwaitable result)
description = await result;
context: context,
resume: ResumeAndConfirm,
prompt: $"You entered Product :- '{interestOptions}', Your Name - '{name}', and Description - '{description}'. Is that correct?",
retry: "I didn't understand. Please try again.");

public async Task ResumeAndConfirm(IDialogContext context, IAwaitable result)
bool confirm = await result;

if (confirm)
await context.PostAsync("Thanks for showing your interest we will contact you shortly.");

// Create a lead record in CRM


private void CreateLeadinCRM()
Microsoft.Xrm.Sdk.Entity lead = new Microsoft.Xrm.Sdk.Entity("lead");
lead.Attributes["subject"] = "Interested in product " + interestOptions;
lead.Attributes["lastname"] = name;
lead.Attributes["description"] = description;

public static OrganizationServiceProxy GetOrganizationService()
IServiceManagement<IOrganizationService> orgServiceManagement =
ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(""));

AuthenticationCredentials authCredentials = new AuthenticationCredentials();
authCredentials.ClientCredentials.UserName.UserName = "";
authCredentials.ClientCredentials.UserName.Password = "*****";
AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);
return new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);


The next posts in this series

Hope it helps..


Getting started with Microsoft Bot Framework

To get started with Bot Framework, download the visual studio template for Bot Application from the following location

Extract it and put the content at the following location. (Visual Studio 2017 in my case)

C:\Users\<<ComputerName>>\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#

Open Visual Studio and select Bot Application template to create a new project

This is how the basic structure of the bot application looks like

The most important part here being the MessageController class.

The class has the Post method which handles the message sent by the user/client termed as Activity. The Post method handles the Activity of type Message.

Below is the list of different type of Activities

DeleteUserData is when the client\user requests for Deletion of all the user data, ConversationUpdate is when a new member is added or removed from the conversation, Typing is when user is typing the message etc.

Now to test this sample application, we need to first download the Bot Emulator.

Now run the bot application.

And next start the emulator

Enter the URL and click on Connect. We will leave App ID and Password as blank as we are testing it locally.

Just type in the message and we’d get the response back.

Details section shows the JSON message which could be useful for debugging and testing purpose.

Apart from sending message we can simulate other Activity Type from within the emulator

The next post in this series

Hope it helps..