CRM Chat Bot: Part 2 – Microsoft Bot Framework Development Concept

In the Part 1 of this series we have explored on how to configure the development environment and run the Bot Framework template to get the “Hello World” kind of reply from the Bot.

Now in this part, I would like to discuss about the development concept of Bot Framework. Behind the scene Bot Framework is an API that is encapsulated within Bot Connector library. The focus of the API itself is to provide a conversational context. Each conversation that come from the user is captured as “Activity”. “Activity” class is part of Bot Builder library. So, be careful not to mixed up with the other “Activity” class on Microsoft .Net framework or even from Dynamics CRM SDK. (Ref: https://docs.botframework.com/en-us/csharp/builder/sdkreference/activities.html).

So far there are 6 activity types that the Bot Framework has:

ActivityType Interface Description
message IMessageActivity a simple communication between a user <-> bot
conversationUpdate IConversationUpdateActivity your bot was added to a conversation or other conversation metadata changed
contactRelationUpdate IContactRelationUpdateActivity The bot was added to or removed from a user’s contact list
typing ITypingActivity The user or bot on the other end of the conversation is typing
ping n/a an activity sent to test the security of a bot.
deleteUserData n/a A user has requested for the bot to delete any profile / user data

 

From the above activity types, “message” probably will be the most commonly used activity type.

Now, let’s drill down to the Activity class. The fundamental basic of Bot is to reply the message that is typed by the end user.

Now, let’s take a look at the code that is generated by the template to understand the basic of message reply from the Bot.

Pt2.BotFramework.png

The highlighted part of the code above is the basic on how the Bot Framework is constructing the reply to the activity.

Ok, we might have the reply from the bot, but it is just the same reply all the time, it is not useful. Bot should be able to engage the user with something meaningful. So, to start a meaningful conversation there are some concepts that we can use: Dialog, FormFlow and Luis.

Note: In this post, I will cover just the high-level concept first and will deep dive with the code examples on the next post.

1.     Dialog

Dialog is the basic model of conversational process. Each Dialog in Bot Framework is encapsulated in a C# class that implements IDialog. The overview of Dialog provided by its documentation (https://docs.botframework.com/en-us/csharp/builder/sdkreference/dialogs.html):

Dialogs model a conversational process, where the exchange of messages between bot and user is the primary channel for interaction with the outside world. Each dialog is an abstraction that encapsulates its own state in a C# class that implements IDialog. Dialogs can be composed with other dialogs to maximize reuse, and a dialog context maintains a stack of dialogs active in the conversation. A conversation composed of dialogs is portable across machines to make it possible to scale a bot implementation. This conversation state (the stack of active dialogs and each dialog’s state) is stored in the state service provided by the Bot Connector service, making the bot implementation stateless between requests. (Much like a web application that does not store session state in the web server’s memory.)

In short, Dialog is the simplest conversation building block for Bot to communicate/reply. Dialog can be meaningful with its Chain method. To make it simple analogy, Chain can be illustrated as a series of command prompt dialog that will handle the reply from the user.

2.        Form Flow

Dialog is good for a starter piece of conversation. However, it can be really tedious job to build the Dialog that might have some aspects of ambiguity. Form Flow, as the name suggests, is based on the idea of a form, where there is a collection of fields to be captured throughout the conversation. Form Flow share the same principle as Dialog, where a “Form” is built through a C# class. Where it is using basic C# data types, such as: Integral, Floating Point, String, DateTime, Enum and List of Enum.

3.        LUIS

Now, with Dialog and Form Flow, the bot can handle basic conversations, however the bot itself will only understand the command based on the pattern of the message. With LUIS (Language Understanding Intelligent Service) from Microsoft, the Bot can be smarter to predict what is the intent of the user. To use LUIS, in code, it will be similar to the normal Dialog class, but we will inherit LuisDialog instead of the IDialog class.

A comparison of normal Dialog vs LUIS Dialog is applicable when we need to handle a conversation that initiates a car interest. With the normal Dialog, we need to put some hard-coded If-Else conditions and regex patterns to predict what is the user wants. But with LUIS, it will provide an output of the possible intents based on the conversation reply analysis.

Part 2 – Conclusion

So, I hope this post helps you to get the basic understanding of Microsoft Bot Framework and get the understanding of the toolset that the SDK provides. Stay tuned for the Part 3 – Development Deep Dive + Integration Through CRM API.

CRM Chat Bot: Part 1 – Getting Started with Microsoft Bot Framework

Microsoft quite recently announced the Microsoft Bot Framework on Build Conference 2016. Below is the overview of the vision of Bot Framework from Microsoft:

The Microsoft Bot Framework provides everything you need to build and connect your bots to your users wherever they converse – from SMS to Office 365 mail to Slack and more… It’s your bot, wherever your users are talking. Learn how to use the Bot Framework to build a bot with your own existing code, use the Bot Builder to generate conversational dialogs from scratch, and more to give your bot skills with natural language processing and deep learning technology. Walk away with tools to build a great bot that can connect with users, wherever they are.

I’ve spent a few months reading and researching on the new Microsoft Bot Framework and I hope with this blog post series I could share and introduce how Microsoft Bot Framework to work together with Dynamics CRM.

Getting Started

Microsoft Bot Framework supports Node.js, .NET and REST Bot Builder SDK. In my posts, I will use .NET based bot, as this is the most common platform that CRM developers use to reduce the learning curve with the Bot Framework.

To start building the bot, we need to set the development environment. Below is my development environment setup:

  • Visual Studio 2015 Update 3 (latest update)
  • Bot Builder SDK (download links are here)
  • Visual Studio C# template (download here)
    • To install, extract the downloaded template and copy it to Visual Studio Project Template. Typically it is located on: “Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#”
  • Bot Framework Emulator (download here)
  • Microsoft Account
  • Microsoft Azure subscription (trial license should do)

Once the environment has been configured, you should be able to see the new project template to be available in Visual Studio:

VS-BotTemplate.png

Let the Journey Begins…

Now let’s create a new bot application. This will give you the following project structure:

ProjectStructure.png

Then we need to install these NuGet packages:

If your environment setup is successfully installed, you would be able to see a page similar to below on build:

ChatBotFirstBuild.png

Bot Emulator

We will use the Bot Emulator to simulate the conversation. Take note of the URL of the page before and enter it in Bot URL.

ChatBotEmulatorFirstBuild.png

You now will see that the bot is replying. Congratulations, you have your first reply from your bot🙂 

Lesson Learned/Troubleshooting

  1. During the research, I encountered a few differences on the sample code. Some of them used “Message” and some have “Activity”. Apparently, it is part of the update on Bot Framework V3 (https://docs.botframework.com/en-us/support/upgrade-to-v3/) that Microsoft changed the usage of Message to Activity.
  2. Another difference in Bot Framework V3 is that you no longer need Microsoft.Bot.Connector package, Microsoft.Bot.Builder is now include the Microsoft.Bot.Connector assemblies.
  3. Use .Net Framework 4.6 and above to develop Bot. I used 4.5.2 (as this is the current supported framework for Dynamics CRM), this will resulting the installation of Microsoft.Bot.Builder package to fail:

Install-Package : Could not install package ‘Microsoft.Bot.Builder 3.2.0’. You are trying to install this package into

a project that targets ‘.NETFramework,Version=v4.5.2’, but the package does not contain any assembly references or

content files that are compatible with that framework. For more information, contact the package author.

At line:1 char:1

+ Install-Package Microsoft.Bot.Builder

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Install-Package], Exception

    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

 

Stay tuned for the Part 2: Microsoft Bot Framework Development Concept!

CRM Ideas, Let your ideas listened

Microsoft just announced CRM Ideas portal: https://crmideas.dynamics.com/ideas/.

Previously the way we submit our feedback/ideas is through Microsoft Connect: https://connect.microsoft.com/dynamicssuggestions. However it seems this site might not be known well and the user experience on that site is not as good as the new portal.

The new portal seems to be based on AdxStudio, from the profile management perspective:idea_portal_profile.png

The user interface of the idea portal is also looks nicer compared to the previous portal:

crm_idea.png

So, what are you waiting for, submit your idea now!

Note: Before you post, please read this posting guideline: https://crmideas.dynamics.com/posting-guidelines/

Ribbon Workbench 2016 beta on XrmToolBox

I just recently re-opened my XrmToolBox to do some Sitemap modification and found out some new plugins are available on the XrmToolBox plugin store. One of it is Ribbon Workbench 2016 beta by Scott Durow.

My first test run on my solution showing that the 2016 version is much quicker compared to the Silverlight-based version that is running on CRM web app.

XrmToolBox-RibbonWorkbench.png

Looking forward for the final version. It will be awesome!

 

Gotcha Working with Dynamics CRM Web API: Grammar..

Microsoft has suggested developers to start using the new Web API to develop new custom development. As they are planning to deprecate the 2011 endpoints sometime after CRM version 9 (We are on version 8 now!). Ref: https://msdn.microsoft.com/en-us/library/dn281891.aspx

So, I’ve embraced the Web API in my new projects for javascript codes. Typically I would use CRM Rest Builder by Jason Lattimer to build Web API request in Javascript.

I found out it is quite interesting on one of my custom entity, let’s call it “cust_selectionmatrix”. I tried to build the query using the tool, but I won’t generate the query as expected. Then I tried to look at Microsoft examples around the Web API query. Most of them are appending “s” against the entity name. E.g: for account: /api/data/v8.0/accountand for contact: /api/data/v8.0/contacts.

So my first trial is just adding s at the end of my custom entity: /api/data/v8.0/cust_selectionmatrixs. However, I’ve got a weird error:

"Resource not found for the segment 'cust_selectionmatrixs'."

Then after I tried to find the list of entity for Web API using  <CRM Server URL>/api/data/v8.0/I found out that my entity is named: “cust_selectionmatrixes” in the Web API. I reckon this is how CRM is implementing “English”. So, if your entity ends with: ‑s, ‑sh, ‑ch, ‑x, or ‑z. Just append ‘es‘ to your entity name to work with the Web API.

https://www.grammarly.com/handbook/mechanics/spelling/5/spelling-plurals-with-s-es-or-other-plurals/

HTH…

 

Then.. There was an awkward moment when I have an entity named “cust_settings” and it is translated as “cust_settingses“… 15jkjv.jpg

Dynamics CRM Online 2016 Update 1 is Up!

I just recently spun up a new trial instance for practice purpose and noticed a slight difference on the notification bar. Out of curiosity I checked the CRM version, and…

CRM_2016_Update_1.png

My new trial instance is running version 8.1!

Looking forward to test what has been announced previously: https://community.dynamics.com/b/msftdynamicsblog/archive/2016/03/02/announcing-dynamics-crm-spring-2016-wave

Exciting times ahead!

CRM Online diagnostic with SaRA (Support and Recovery Assistant)

The CRM Support team recently released a new tool to diagnose Dynamics CRM Online connectivity issue. The initial CRM Online scenario is for CRM Outlook Client configuration issues.  This diagnostic can detect issues related to CRM Outlook Client install, configuration, and can also detect and fix a disabled CRM add-in with the click of a button.  It can also retrieve log files and event log information.

This will be really helpful in troubleshooting the Outlook Client connectivity. Kudos to the Dynamics CRM product team!

Looking forward for the future enhancement to this tool!

Full article from the CRM Support team:

https://community.dynamics.com/crm/b/dynamicscrmsupportblog/archive/2016/05/02/crm-diagnostic-now-available-in-office-365-support-and-recovery-assistant-sara