September 15th, 2017

Using Microsoft Bot Framework as a Proxy to Publish your Bot Stack

Image: Silver Jubilee Bridge by k_r_craft, used under CC0 Creative Commons

Background

Exceed.ai provides a platform for authoring and developing bots, focusing on brands’ digital strategy by introducing AI into the sales funnel. They develop their own stack for managing bots’ dialogs, state, and logic. The bots are hosted mainly on their customers’ Facebook pages, and their customers can author their bots’ dialogs using an administration dashboard. Exeed.ai wanted the ability to publish their bot dialogs over multiple channels, such as those that Microsoft Bot Framework supports (Web, Skype, Slack, etc.), without having to rewrite their backend logic or dialogs from scratch, using the Bot Framework SDK.

We collaborated with Exceed.ai to create a solution which uses their current content management system while providing the capability to expose dialogs across different channels using Bot Framework. The layout for our solution is based on the use of the bot-graph-dialog extension developed as part of our collaborations with Pager and SMARK.io.

The Solution

In our engagement with SMARK.io, we converted the dialogs that they maintain on their backend to dialogs that were managed and run using the bot-graph-dialog extension.

In order to provide support for Exceed.ai’s existing content management system, we added a “proxy mode” to the bot-graph-dialog, so that every message sent to/from the user is tunneled through the bot-graph-dialog extension to Exceed.ai’s backend service where it can be processed and continue the flow.

To implement this proxy feature, we created and extended a Navigator interface, responsible for navigating the conversation graph (steps in the conversation) to the GraphDialog class that manages the conversation. The navigator implementation is responsible for calling their backend to get the next step and providing the previous step’s results.

Essentially, the translation between the external backend (e.g., Exceed.ai) models and Microsoft Bot Framework models can be done in the backend or in the Navigator, depending on your business scenario and implementation.

The Code

The critical components of the solution code are outlined below. Our full implementation of the Exceed.ai scenario can be found on GitHub.

You can also follow the instructions in the bot-trees repository and use the proxyMode.js file to run a local implementation that does not depend on a remote API.

Implementing the Navigator Interface

// an implementation of the Navigator interface
// which will act as the proxy for the backend API
class ProxyNavigator {

  constructor() {
    // backend root URL
    this.apiUrl = "https://<backend api url>";
  }

  // returns the current node of the dialog
  async getCurrentNode(session) {
    var node = // TODO: Implement logic to resolve current step to process
    return node;
  };

  // resolves the next node in the dialog
  async getNextNode(session) {
    console.log(`getNextNode, message: ${session.message}`);
    var node = // TODO: Implement logic to resolve next step to process
    return node;
  }

}

The node that is returned by these methods is an object with a similar schema to the nodes that the GraphDialog uses in its JSON files (see example 1 and example 2).

By extending the existing GraphDialog library with the “proxyMode” feature, we can leverage its translation logic, transforming its JSON-based nodes into the actual responses of the bot framework.

When creating the GraphDialog instance, inject the ProxyNavigator implementation into its constructor:

Using The Proxy Navigator

var navigator = new ProxyNavigator();
var graphDialog = await GraphDialog.create({ bot, navigator, proxyMode: true });
bot.dialog('/', graphDialog.getDialog());
console.log(`proxy graph dialog loaded successfully`);

Our bot is now up and ready for users’ interactions.

Summary

With our solution, users can publish bot dialogs from any backend directly to Bot Framework channels, with custom Navigator logic if needed. This approach can be used in any scenario where you want to develop your dialogs (both logic and state) using a different SDK than Microsoft Bot Framework, or even your own custom stack, but still want to use the functionality of Microsoft Bot Framework to publish those dialogs to different channels.

Author

A Principal Software Engineer and Evangelist at Microsoft. Ami's team engages with startups and enterprise innovation labs, to work through their toughest technical challenges. Ami brings entrepreneurship skills, creativity, innovation, end-2-end development and customer-focused approach. Ami joined Microsoft 10 years ago as a software engineer. Since then, Ami played different roles in product groups and incubations which shipped several innovative products in the mobile and web worlds.

0 comments

Discussion are closed.