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..

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

14 thoughts on “Using Dialogs in Microsoft Bot Framework (Creating lead in CRM)”

  1. Hi Nishant,
    Excellent blog. Could you add the types as well for the IAwaitable in the method’s signatures. e.g.
    public virtual async Task MessageRecieveAsync(IDialogContext context, IAwaitable argument)
    public async Task ResumeGetInterest(IDialogContext context, IAwaitable result)

    After adding the applicable types, the bot worked perfectly fine and I was able to exchange the CRM data.
    Thanks again for the wonderful blog. Moving on to your next blog.

    Liked by 2 people

Please share your thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform


Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions


Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM


Mail to for queries and suggestions

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM


Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts


developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: