Use IBM Watson in Bot Framework Chatbots

by Michael Szul on

No ads, no tracking, and no data collection. Enjoy this article? Buy us a ☕.

I've had a lot of ideas in my notes on what I want to work on for the Bot Builder Community. One of the things I've wanted to do is diversify the back-end tooling and services, while maintaining the coding patterns of the "default" services. I've had a couple of different services in mind, but after being interviewed by Rishi on a podcast episode in which he brought up IBM Watson, I spent some time in the Watson SDK, and decided to see what I could integrate.

I actually started this package about 6 months ago, but finalizing the chatbot book became a top priority and put things on hold. Once the book finally launched, I took the time to finish this package, and then started working on increasing the quality of the current Bot Builder Community packages.

We already had a Text Analytics module that did text analysis for keywords, sentiment analysis, etc. based on Azure's Cognitive Services, so I abstracted much of that code, created a base engine abstract class to be a sort of container for future methods on text analysis, and then started finalizing the IBM Watson NLU code as a swappable engine.

A few weeks ago, we made the official release. You can now use IBM Watson as the text analysis engine in your Bot Framework chatbots.

Start by installing the package:

npm install @botbuildercommunity/middleware-watson-nlu --save
      

Once you have that installed, you can include the IBM Watson NLU in your Bot Framework code just like you do with any other middleware:

import { EmotionDetection } from '@botbuildercommunity/middleware-watson-nlu';
      
      adapter.use(new EmotionDetection(process.env.WATSON_API_KEY, process.env.WATSON_ENDPOINT));
      

In the above code, we're importing the EmotionDetection middleware which is exclusive to the IBM Watson NLU code (at this time). We have our Watson key and endpoint in our .env file. You can get these values from your IBM Cloud account in the Watson developer portal when you create an NLU instance.

Once you have the middleware set up, you can check the emotion detection off of the turn state with something like the following:

context.turnState.get('emotionDetection')`
      

This will be an object, whose properties represent various emotions. Each emotion is associated with a score.

Here's a quick code snippet from the example that's inside the repository for the package:

adapter.use(new EmotionDetection(process.env.WATSON_API_KEY, process.env.WATSON_ENDPOINT));
      
      server.post("/api/messages", (req, res) => {
          adapter.processActivity(req, res, async (context) => {
              if (context.activity.type === "message") {
                  await context.sendActivity(`You said "${context.activity.text} with an emotion score of [joy] at ${context.turnState.get("emotionDetection").joy}"`);
              }
          });
      });
      

In this example, we're simply using the emotion detection on each message, and getting the score for the joy emotion.

Where do we go from here?

There's a lot of improvements that can be made to this package, including utility methods. It's the first step towards allowing the Bot Framework to be "cross-platform" in the sense that back-end services can be swapped out. That way if IBM Watson has the better text analysis for your project, you can use that with little fuss, but still use the awesomeness that is the Bot Framework for your dialog management.