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


Let us continue with our previous post on Bot Framework

https://nishantrana.me/2017/04/18/getting-started-with-microsoft-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
{
Product1,
Product2,
Product3
}

// Decorate the class with Serializable attribute
[Serializable]
// 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
context.Wait(MessageRecieveAsync);
}

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

if (message.Text.Contains("interested"))
{
PromptDialog.Choice(
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;

PromptDialog.Text(
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;

PromptDialog.Text(
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;
PromptDialog.Confirm(
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
CreateLeadinCRM();

}

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;
GetOrganizationService().Create(lead);
}

public static OrganizationServiceProxy GetOrganizationService()
{
IServiceManagement<IOrganizationService> orgServiceManagement =
ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://nishantcrm365.crm.dynamics.com/XRMServices/2011/Organization.svc"));

AuthenticationCredentials authCredentials = new AuthenticationCredentials();
authCredentials.ClientCredentials.UserName.UserName = "nishant@nishantcrm365.onmicrosoft.com";
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

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

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

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

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

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

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

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