Botframework-webchat: Access bot using Direct Line API

Created on 29 Oct 2018  路  1Comment  路  Source: microsoft/BotFramework-WebChat

I am curious about how I can unit test the bot without using an emulator. Either it can be hosting the bot in unit test project with the in-memory server hosting or communicate the bot with Direct Line API. In both ways, the bot should run locally.

I can't host the bot with Microsoft.AspNetCore.Hosting in my test framework(if hosted, don't know what endpoint the bot communicates).

If I use the bot Startup class with WebHostBuilder in console app the bot is up and running at localhost:5000.

In Console App: IWebHostBuilder builder = new WebHostBuilder().UseStartup()

Now how do I make call to bot?

Question

Most helpful comment

This is an advanced topic (for now). You can look at one of our internal tool, called "Emulator Core CLI", https://github.com/Microsoft/BotFramework-Emulator/tree/master/packages/emulator/cli.

Emulator Core is the server that emulate Direct Line service. You can use it without Emulator app (via the CLI). And it's exactly the same copy we bundle in Emulator app.

Today, we don't publish the CLI because it's not polished yet. You can clone Emulator repos and build it manually. We use this tool internally for hosting bot in offline mode, mainly for testing Web Chat today (or building bot while we are on a plane.)

Before using this tool, it will be helpful if you understand how Web Chat, Direct Line service, and Bot communicate with each other:

  • Web Chat talk to Direct Line service, via REST (polling) or Web Socket

    • Today, Emulator Core only talk REST, not Web Socket

  • Bot talk to Direct Line service in two ways

    • Direct Line service will act as HTTP client and send HTTP request to the bot

    • Bot will act as HTTP client and send HTTP request to Direct Line service

Note that since the bot is both HTTP server/client. If all 3 components (Web Chat, Emulator Core, Bot) are hosted locally, it should be kinda trivial to setup. But if you are using a local Emulator Core to talk to a production bot on Azure Bot Services (i.e. Azure Web App), you will need to use ngrok to make your Emulator Core outside of your firewall for the bot to call. We call this "customizing service URL", which is customizing the endpoint for the bot to call back (a.k.a. service URL). This feature is in Emulator app ("the ngrok checkbox") but you will need to do it manually if you go thru CLI.

After you setup all 3 components correctly and they can talk to each other. You can start replacing Web Chat by either BotFramework-DirectLineJS or Web Chat Core (BotFramework-WebChat-Core). Then you could write a test harness to use either one of the packages, to talk to your bot.

The difference of DirectLineJS or Web Chat Core is about the level of abstractions:

  • DirectLineJS is the Node.js SDK for Direct Line API, the HTTP protocol that drive Web Chat, it is stateless
  • Web Chat Core (depends on DirectLineJS) is the Redux store behind Web Chat, it is headless and stateful. It provide some abstractions to simplify writing a new bot client, e.g. suggested actions, patching up outgoing activities, etc.

DirectLineJS was not designed to run outside of a browser. But you can follow notes here to make DirectLineJS works under Node.js (and also Web Chat Core). We have a work item to make DirectLineJS work under Node.js without polyfills.

So after you setup DirectLineJS or Web Chat Core, you will be able to write a test harness to talk to your bot in offline mode.

To be honest, the whole setup is not trivial. Also to be fair, the code that do heavy-lifting is ready to use today. While we are focusing on big things (a.k.a. main story, e.g. Emulator v4, rewrite of Web Chat, new SDK), we are also paving the way to make bot development easier (a.k.a. side stories). When all the components are cooked in the main story, we will be able to complete the side story in very short timeframe. It's like killing two birds with one stone. One big bird, one small bird. :)

Until then, please let us know about your journey into bot testing. We would love to help.

>All comments

This is an advanced topic (for now). You can look at one of our internal tool, called "Emulator Core CLI", https://github.com/Microsoft/BotFramework-Emulator/tree/master/packages/emulator/cli.

Emulator Core is the server that emulate Direct Line service. You can use it without Emulator app (via the CLI). And it's exactly the same copy we bundle in Emulator app.

Today, we don't publish the CLI because it's not polished yet. You can clone Emulator repos and build it manually. We use this tool internally for hosting bot in offline mode, mainly for testing Web Chat today (or building bot while we are on a plane.)

Before using this tool, it will be helpful if you understand how Web Chat, Direct Line service, and Bot communicate with each other:

  • Web Chat talk to Direct Line service, via REST (polling) or Web Socket

    • Today, Emulator Core only talk REST, not Web Socket

  • Bot talk to Direct Line service in two ways

    • Direct Line service will act as HTTP client and send HTTP request to the bot

    • Bot will act as HTTP client and send HTTP request to Direct Line service

Note that since the bot is both HTTP server/client. If all 3 components (Web Chat, Emulator Core, Bot) are hosted locally, it should be kinda trivial to setup. But if you are using a local Emulator Core to talk to a production bot on Azure Bot Services (i.e. Azure Web App), you will need to use ngrok to make your Emulator Core outside of your firewall for the bot to call. We call this "customizing service URL", which is customizing the endpoint for the bot to call back (a.k.a. service URL). This feature is in Emulator app ("the ngrok checkbox") but you will need to do it manually if you go thru CLI.

After you setup all 3 components correctly and they can talk to each other. You can start replacing Web Chat by either BotFramework-DirectLineJS or Web Chat Core (BotFramework-WebChat-Core). Then you could write a test harness to use either one of the packages, to talk to your bot.

The difference of DirectLineJS or Web Chat Core is about the level of abstractions:

  • DirectLineJS is the Node.js SDK for Direct Line API, the HTTP protocol that drive Web Chat, it is stateless
  • Web Chat Core (depends on DirectLineJS) is the Redux store behind Web Chat, it is headless and stateful. It provide some abstractions to simplify writing a new bot client, e.g. suggested actions, patching up outgoing activities, etc.

DirectLineJS was not designed to run outside of a browser. But you can follow notes here to make DirectLineJS works under Node.js (and also Web Chat Core). We have a work item to make DirectLineJS work under Node.js without polyfills.

So after you setup DirectLineJS or Web Chat Core, you will be able to write a test harness to talk to your bot in offline mode.

To be honest, the whole setup is not trivial. Also to be fair, the code that do heavy-lifting is ready to use today. While we are focusing on big things (a.k.a. main story, e.g. Emulator v4, rewrite of Web Chat, new SDK), we are also paving the way to make bot development easier (a.k.a. side stories). When all the components are cooked in the main story, we will be able to complete the side story in very short timeframe. It's like killing two birds with one stone. One big bird, one small bird. :)

Until then, please let us know about your journey into bot testing. We would love to help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

naveen-vijay picture naveen-vijay  路  4Comments

electrobabe picture electrobabe  路  4Comments

compulim picture compulim  路  3Comments

prashanthsridhar picture prashanthsridhar  路  3Comments

marcasmar94 picture marcasmar94  路  3Comments