Overcoming namespace clashes when upgrading to Bot Framework 4.3

V4.3 comes with some nice additional support that I was eager to use. However, there is a problem. v4.3 (Microsoft.Bot.Builder.Azure) uses the latest variant of the Azure Storage library whereas Microsoft.AspNetCore.All, via Microsoft.AspNetCore.DataProtection.AzureStorage (2.2.0), uses the older variant. This can cause problems if your own code wishes to use one of the clashing types. E.g. if you add

CloudStorageAccount blah = new CloudStorageAccount(null, false);

Then you’ll get an error like The type X exists in both Y and Z, e.g.

error CS0433: The type 'CloudStorageAccount' exists in both 'Microsoft.Azure.Storage.Common, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'Microsoft.WindowsAzure.Storage, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

The only solution I’ve found is to utilize the obscure extern and some trickery I grabbed from SO – using extern alias.

Step 1 – Create (or ensure there is) an xml file in the root of your project called Directory.Build.targets (don’t put .xml as the extension)

Step 2 – populate with;

  <Target Name="AddPackageAliases" BeforeTargets="ResolveReferences" Outputs="%(PackageReference.Identity)">

      <ReferencePath Condition="'%(FileName)'=='$(AliasPackageReference)'">

Step 3 – edit the project. Unload your bot project, edit it and find the reference you wish to alias. Add Alias= E.g. to add the alias AzureCommon

    <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.9.1" />
    <PackageReference Include="Microsoft.ApplicationInsights.TraceListener" Version="2.9.1" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.All" />
    <PackageReference Include="Microsoft.Azure.KeyVault.Core" Version="3.0.3" />
    <PackageReference Include="Microsoft.Azure.Storage.Common" Version="9.4.2" Alias="AzureCommon"/>

Save and reload the project

Step 4 (optional) -That should be enough to provide the separation for the compiler. But if you want to use the aliased version then add the extern to where you wish to use the clashing type, e.g.;

extern alias AzureCommon;
using AzureStore = AzureCommon;


AzureCommon.Microsoft.WindowsAzure.Storage.CloudStorageAccount blah =
new AzureCommon.Microsoft.WindowsAzure.Storage.CloudStorageAccount(null, false);

Step 5 – celebrate you’ve avoided this hiccup 🙂

Using the streamlined On handlers in Bot Framework v4.3

Bot Framework v4.3 has introduced a series of ‘On’ activity handlers to make you code more modular and easier to understand. Once you updated your project reference to use 4.3 you need to change you main bot to use the new activity handler class;

public class MayBotBot : IBot
 public class MayBotBot : ActivityHandler

You’ll then be able to use override to discover the options, e.g.

protected virtual Task OnMessageActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken)
return Task.CompletedTask;

The other nice new feature that might slide under the radar is when the framework looks for a dialog Id it will now search up the stack to look for one. I.e. you could, if you wanted to, AddDialog all your possible dialogs in the root dialog and remove all AddDialog from everywhere else. Don’t do that, but in theory you could. The advantage here is that you can declare your common dialogs once and not have to keep adding them everywhere else.

Enjoy the chocolatey goodness of 4.3.



Upgrading your bot to .net core 2.2, warnings

When you upgrade your bot from .net core 2.0/2.1 to 2.2 you may see an warning that states; ‘A PackageReference to ‘Microsoft.AspNetCore.All’ specified a Version of 2.2.1. Specifying the version of this package is not recommended’.

To avoid this warning;

  1. Right click the bot project in the solution explorer and ‘unload’ it.
  2. Right click the unloaded project and edit it
  3. Change
    <PackageReference Include=”Microsoft.AspNetCore.All” Version=”2.2.1″/>
    <PackageReference Include=”Microsoft.AspNetCore.All” />
  4. Reload the project.


Getting started with LUIS Containers

LUIS Containers is a really interesting (preview) feature. One of the issues with LUIS, and machine learning in general, is that each endpoint learns differently. E.g. if you create two LUIS endpoints, JANET and JOHN, and import the exact same JSON model then Janet and John will likely give you different scores for exactly the same utterance. Whilst that is just something you need to appreciate it, it does make a lot of workflow scenarios very difficult, E.g. testing. One potential solution to this is to export the LUIS model as a Container. In my initial tests this seems to clone the server, i.e. you get JANET2 rather than daughter of JANET – you get the same scores for the same utterance. Here is my add-on help to the documentation.

The main documentation is currently located at Install and run LUIS docker containers. If you don’t want to follow my guide then my advice is to read that but before you do anything read the next document along as that has a better explanation of the settings  Configure Language Understanding Docker containers

My guide to get up an running with LUIS Containers (for Windows 10)

I’m assuming you’ll be using the Command prompt and not a Bash terminal (as used in the official guide).

  1. Install Docker – if you’re new to Docker then I’d recommend at least following the first page of Get Started. Install Docker for Windows 10.
  2. Create a folder on your local disk that will container the LUIS model, e.g. C:\LUIS\Input and a folder for the output, e.g. C:\LUIS\Output
  3. Open the Docker settings panel (from the taskbar tools) and ensure you are sharing the local disk where you created the folders in (2)
  4. Go to LUIS and export the version of the model you want to use. Remember to select export as a Container.
  5. Get the Docker image for LUIS,
    docker pull mcr.microsoft.com/azure-cognitive-services/luis:latest
  6. Now create the container, remember this is all one line in CMD, so consider writing a batch file for it.
    docker run --rm -it -p 5000:5000 --memory 4g --cpus 2 --mount type=bind,src=c:\luis\input,target=/input --mount type=bind,src=c:\luis\output,target=/output mcr.microsoft.com/azure-cognitive-services/luis Eula=Accept Billing=https://YOUR_REGION.api.cognitive.microsoft.com/luis/v2.0 ApiKey=YOUR_API_KEY

    Where the Billing endpoint can be take from the first part of the Endpoint address in LUIS ‘Keys and Endpoint settings’ and the ApiKey is only the set of digits from the key of the same page. E.g. 3q919c439w2445f217b3w262622331c1

  7. You can then use your REST client of choice. You may use http://localhost:5000/swagger/index.html but be warned you’ll need to convert the AppId to a GUID (online coverter)…I know, right?? You should get an error saying something like; No model found with the given Application ID. If you do, then look at the output from your Docker console window. It should say something like could not find file xyz.gz.
  8. Copy the file you downloaded in (4) and put it into the input folder you created in (2). Carefully examine the name of the file. It needs to perfectly match the name of the file in the error message in (7)
  9. Ctrl+c the Docker container. Re-run the command from (6). Retry your REST call. You should now be working 🙂

NB, when you finish with this and try it again at a later date you may get errors when restarting your container, step (6). It may say something like, Error starting userland proxy. This appears to be a problem with Docker on Windows 10. You need to reopen the Docker desktop from the Taskbar and select Restart. This can take a little time but keep hovering over the icon and it will show you when it’s running again. Then you can re-issue step (6) and everything should be fine again.

One last note is that you need to stay online when using the Container. You can temporarily go offline but any prolonged absence and the LUIS Container will take itself offline with failed to reach metering endpoint,  resource temporarily unavailable. Shame, that scuppers offline use. Oh well, can’t have everything you want 🙂


Fetching all the LUIS intents in the Bot Framework

I decided that today was the day that I could no longer write a useful LUIS + Bot by only consuming the top scoring intent. So I checked the little Include all predicted intent scores switch in LUIS and ensured the ‘REST-API’ results had returned all the predictions. Yay. Changed my code to consume them to discover I was still only getting the top intent. Turns out you to do a little more work with the Bot SDKs to see the other intents;

v3 SDK

In your code that implements the LUISDialog base class;

protected override LuisRequest ModifyLuisRequest(LuisRequest request)
request.Verbose = true;
return base.ModifyLuisRequest(request);

v4 SDK

In the code where you create your LUIS Application and Recognizer, add the IncludeAllIntents options;

var app = new LuisApplication(luis.AppId, luis.AuthoringKey, luis.GetEndpoint());
var recognizer = new LuisRecognizer(


new LuisPredictionOptions { IncludeAllIntents = true });

botframeworkoptions.state is obsolete

An recent update to the Bot Framework means that you may see some obsolete messages when trying to add conversation/user state, etc. to the options object in the setup. Don’t worry, it’s easy enough to alter.

// Create and add conversation state.
var conversationState = new ConversationState(dataStore);
// REMOVE THIS -> options.State.Add(conversationState);

Introduction to writing Bot Application video tutorial

Introduction to the tutorial series is about writing bot applications using the Microsoft Bot Framework v4. The goal of this tutorial is to help someone who is interested in writing a Bot application that may not have much experience in software development. It will be a, “warts and all” journey to show how I would develop a bot. So I will be making mistakes and showing you how I go about resolving them. It will get you to a point where you can write your own conversational bot and provide you with some of the additional tools to help you resolve issues that you may find.


Ep1 Setup

Ep2 Bot Basics

Ep3 Language Understanding

Ep4 Bot & LUIS

Ep5a Dialogs

Ep5b Dialogs

Ep6 Wrap up

Get Sentiment Score from LUIS RecognizerResult

Another quick extension method to help with getting a sentiment score from a RecognizerResult;

public static double? GetSentimentScore(this RecognizerResult luisResult)
    double? result = null;

    if (luisResult != null)
        var data = luisResult.Properties["sentiment"];
        var sentimentValues = data as IDictionary;
        var score = sentimentValues["score"] as JValue;
        result = (double)score.Value;

    return result;

Automate Bot tests with saved transcript files

I’ve recently worked on a testing framework with Ben Williams (@negativeeddy) that allows you to write your own chat scripts. However, I thought that saving transcripts of your chat from the Bot Emulator would be a really helpful way to produce automated testing. Unfortunately I could not find any out-of-the-box support for running transcript files. Thus the following was born;

Testing with Transcript files

In my example I have the following chat conversation with my Adventure Bot;

Examine table

The table is old but in good condition.
On the table there is a key 'Room Key A'.
Take Room Key A

Take key

You Take key.

When you save the transcript file from the Emulator it contains the serialized version of those activities. Here’s a snippet of the above;

“type”: “message”,
“text”: “examine table”,
“from”: {
“id”: “5c8620e2-ccd8-4624-9c91-9842610220a4”,
“name”: “User”
“type”: “message”,
“serviceUrl”: “http://localhost:50090&#8221;,
“channelId”: “emulator”,
“from”: {
“id”: “1”,
“name”: “Bot”,
“role”: “bot”
“conversation”: {
“id”: “b1bbf220-011b-11e9-aa8d-d94902c621f5|livechat”
“recipient”: {
“id”: “5c8620e2-ccd8-4624-9c91-9842610220a4”,
“role”: “user”
“text”: “The table is old but in good condition.\nOn the table there is a key ‘Room Key A’.\n”,
“inputHint”: “acceptingInput”,
“suggestedActions”: {
“actions”: [
“type”: “imBack”,
“title”: “take Room Key A”,
“value”: “take Room Key A”

Write a test

Now we have the transcript file we want to use it from a test. First create a Test project and get the contents of the file. Note, I’m using MSTest and Moq. I’m taking a hacky short cut and reading the whole file but it should be using streams;

public async Task TakeKeyFromTranscriptTest()
  var transcriptFile = await File.ReadAllTextAsync("Transcripts\\examineTable.transcript");
  var activityTranscript = JsonConvert.DeserializeObject(transcriptFile);
  await TestBotTranscriptAsync(activityTranscript);

Set-up the test

Now we configure the test and the service-under-test, or in our case – a dialog.

private async Task TestBotTranscriptAsync(List activityTranscript, object request = null)
  // create mock version
  var mockDependency = new Mock();
  RecognizerResult recognizerResult = FakeRecognizerResult(
   "examine table",

  mockDependency.Setup(x => x.RecognizeAsync(
   It.Is(i => i.Activity.Text.Equals("Examine table", StringComparison.InvariantCultureIgnoreCase)),

  recognizerResult = FakeRecognizerResult(
   "take Room Key A",

  // set up mock method
  mockDependency.Setup(x => x.RecognizeAsync(
   It.Is(i => i.Activity.Text.Equals("take Room Key A", StringComparison.InvariantCultureIgnoreCase)),

  this.Recognizer = mockDependency.Object;

  await CreateDialogTest(
   (id, svc, log) => new AdventureDialog(id, svc, log),
   options: request)

private static RecognizerResult FakeRecognizerResult(string query, string intent, string noun)
    var resultJson = "{\"text\":\"" + query +
        "\",\"alteredText\":null,\"intents\":{\"" + intent +
        "\":{\"score\":0.9696778}},\"entities\":{\"$instance\":{\"noun\":[{\"startIndex\":8,\"endIndex\":13,\"text\":\"" + noun +
        "\",\"type\":\"noun\",\"score\":0.997288644}]},\"noun\":[\"" + noun + "\"]}}";
    var recognizerResult = JsonConvert.DeserializeObject(resultJson);
    return recognizerResult;

The Botness of the test is inside the CreateDialogTest method;

public TestFlow CreateDialogTest(string dialogId,
Func createDialog,
object options = null)
where TDialog : Dialog
  // setup the bot state for the test
  var storage = new MemoryStorage();
  var conversationState = new ConversationState(storage);
  var userState = new UserState(storage);

  var accessors = new AdventureCBCBotAccessors(conversationState, userState)
   AdventureState = conversationState.CreateProperty(AdventureCBCBotAccessors.AdventureStateName),
   DialogState = conversationState.CreateProperty(AdventureCBCBotAccessors.DialogStateName),

  // create the test adapter with any required middleware
  var adapter = new TestAdapter(sendTraceActivity: true)
   .Use(new GameServicesMiddleware(accessors.AdventureState))
   .Use(new AutoSaveStateMiddleware(conversationState));

  // Create or Moq any services
  ILoggerFactory factory = new LoggerFactory()

  var services = new BotServices(
   new BotConfiguration(),

  if (this.Recognizer != null)
   services.LuisServices.Add("LuisAdventureBot", this.Recognizer);

  // Create the dialog under test
  var dialogState = conversationState.CreateProperty("dialogState");
  var dialogs = new DialogSet(dialogState);
  TDialog dialog = createDialog.Invoke(dialogId, services, factory);

  // Start the test flow-loop to execute the chat
  return new TestFlow(adapter, async (turnContext, cancellationToken) =>
   var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);

   var result = await dc.ContinueDialogAsync(cancellationToken);

   if (result.Status == DialogTurnStatus.Complete)
    if (result.Result != null)
     await turnContext.SendActivityAsync(result.Result.ToString());

   if (!turnContext.Responded)
    await dc.BeginDialogAsync(dialog.Id, options, cancellationToken);

public EndpointService TestEndpoint { get; set; } = new EndpointService() { Endpoint = "https://example.com/bot" };

Execute a test

You may have noticed the lines;


These are were the execution and testing happen. StartTestAsync start the test adapter but the AssertBotTranscript activity extension is where the magic happens. Before we look at that we need to create a couple of helper classes. These classes are responsible for finding all the message activies and to identify which ones are from the user or the bot.

public enum StepActor

public class BotScript

  public BotScript(List transcript)
   this.Steps = this.ParseScript(transcript);

  public string UserName { get; set; } = "User";

  public IEnumerable Steps { get; }

  private IEnumerable ParseScript(List transcript)
   foreach (var activity in transcript
    .SkipWhile(t => t.Type != ActivityTypes.Message || t.From.Name != this.UserName)
    .Where(t => t.Type == ActivityTypes.Message))
    if (activity.From.Name == this.UserName)
     BotStep step = new BotStep
       Actor = StepActor.User,
       Text = activity.Text,

     yield return step;
     BotStep step = new BotStep
       Actor = StepActor.Bot,
       Text = activity.Text,
       Actions = activity.SuggestedActions?.Actions.Select(a => a.Title).ToList(),

     yield return step;

public class BotStep
  public StepActor Actor { get; internal set; }
  public string Text { get; internal set; }
  public List Actions { get; internal set; }

Now we have our User and Bot steps we can execute the test. This is achieved by enumerating all the steps from the transcripts. If it is a User step then we send it into the Flow. If it’s a Bot step then we assert that the Bot has responded with the expected value in the step.

public static TestFlow AssertBotTranscript(this TestFlow testFlow, List transcript)
  var botScript = new BotScript(transcript);
  return testFlow.AssertBotScript(botScript);

public static TestFlow AssertBotScript(this TestFlow testFlow, BotScript script)
  foreach (var step in script.Steps)
   if (step.Actor == StepActor.Bot)
     testFlow = testFlow.AssertBotStep(step);
     testFlow = testFlow.Send(step.Text);
 return testFlow;

public static TestFlow AssertBotStep(this TestFlow testFlow, BotStep expectedResponse)
 return testFlow.AssertReply(activity =>
  var messageActivity = activity.AsMessageActivity();

  // check main text of message
  if (!string.IsNullOrEmpty(expectedResponse.Text))
   string fixedExpected = SanitizeText(expectedResponse.Text);
   string actualResponse = SanitizeText(messageActivity.Text);

   Assert.AreEqual(fixedExpected, actualResponse);

private static string SanitizeText(string text)
  string fixedReply = text;

  if (!string.IsNullOrEmpty(fixedReply))
   fixedReply = fixedReply.Replace("\r", string.Empty).Replace("\n", string.Empty);

  return fixedReply;

All that’s left to is to run the test;



It is just a start

I’ve left out of lot of additional code you need, e.g. handling Adaptive cards, Actions, Hero Cards, etc. but it’s exactly the same idea. You need to flesh out the BotStep class and AssertReply to handle the different forms of message activities. Hopefully, this will just be included in the Framework of the Bot Community framework and you will never need to write this yourself. But for now, this should help you out.