• Stars
    star
    208
  • Rank 189,015 (Top 4 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A middleware to connect Watson Conversation Service to different chat channels using Botkit

Use IBM Watson's Assistant service to chat with your Botkit-powered Bot! Build Status Greenkeeper badge

This middleware plugin for Botkit allows developers to easily integrate a Watson Assistant workspace with multiple social channels like Slack, Facebook, and Twilio. Customers can have simultaneous, independent conversations with a single workspace through different channels.

Table of Contents

Middleware Overview

  • Automatically manages context in multi-turn conversations to keep track of where the user left off in the conversation.
  • Allows greater flexibility in message handling.
  • Handles external databases for context storage.
  • Easily integrates with third-party services.
  • Exposes the following functions to developers:

Function Overview

Installation

$ npm install botkit-middleware-watson

Prerequisites

Create an instance of Watson Assistant

💡 You can skip this step if you have credentials to access an existing instance of Watson Assistant. This would be the case for Cloud Pak for Data.

  1. Sign up for an IBM Cloud account.
  2. Create an instance of the Watson Assistant service and get your credentials:
    • Go to the Watson Assistant page in the IBM Cloud Catalog.
    • Log in to your IBM Cloud account.
    • Click Create.
    • Copy the apikey value, or copy the username and password values if your service instance doesn't provide an apikey.
    • Copy the url value.

Configure your Assistant

  1. Create a workspace using the Watson Assistant service and copy the workspace_id. If you don't know how to create a workspace follow the Getting Started tutorial.

Acquire channel credentials

This document shows code snippets for using a Slack bot with the middleware. You need a Slack token for your Slack bot to talk to Watson Assistant. If you have an existing Slack bot, then copy the Slack token from your Slack settings page.

Otherwise, follow Botkit's instructions to create your Slack bot from scratch. When your bot is ready, you are provided with a Slack token.

Bot setup

This section walks you through code snippets to set up your Slack bot. If you want, you can jump straight to the full example.

In your app, add the following lines to create your Slack controller using Botkit:

import { WatsonMiddleware } from 'botkit-middleware-watson';
import Botkit = require('botkit');
const { SlackAdapter } = require('botbuilder-adapter-slack');

const adapter = new SlackAdapter({
    clientSigningSecret: process.env.SLACK_SECRET,
    botToken: process.env.SLACK_TOKEN
});

const controller = new Botkit({
    adapter,
    // ...other options
});

Create the middleware object which you'll use to connect to the Watson Assistant service.

If your credentials are username and password use:

const watsonMiddleware = new WatsonMiddleware({
  username: YOUR_ASSISTANT_USERNAME,
  password: YOUR_ASSISTANT_PASSWORD,
  url: YOUR_ASSISTANT_URL,
  workspace_id: YOUR_WORKSPACE_ID,
  version: '2018-07-10',
  minimum_confidence: 0.5, // (Optional) Default is 0.75
});

If your credentials is apikey use:

const watsonMiddleware = new WatsonMiddleware({
  iam_apikey: YOUR_API_KEY,
  url: YOUR_ASSISTANT_URL,
  workspace_id: YOUR_WORKSPACE_ID,
  version: '2018-07-10',
  minimum_confidence: 0.5, // (Optional) Default is 0.75
});

If your service is running in the IBM Cloud Pak for Data use:

const watsonMiddleware = new WatsonMiddleware({
  icp4d_url: YOUR_CLOUD_PAK_ASSISTANT_URL,
  icp4d_access_token: YOUR_CLOUD_PAK_ACCESS_TOKEN,
  disable_ssl_verification: true,
  workspace_id: YOUR_WORKSPACE_ID,
  version: '2018-07-10',
  minimum_confidence: 0.5, // (Optional) Default is 0.75
});

Tell your Slackbot to use the watsonMiddleware for incoming messages:

controller.middleware.receive.use(
  watsonMiddleware.receive.bind(watsonMiddleware),
);

Finally, make your bot listen to incoming messages and respond with Watson Assistant:

controller.hears(
  ['.*'],
  ['direct_message', 'direct_mention', 'mention'],
  async function(bot, message) {
    if (message.watsonError) {
      await bot.reply(
        message,
        "I'm sorry, but for technical reasons I can't respond to your message",
      );
    } else {
      await bot.reply(message, message.watsonData.output.text.join('\n'));
    }
  },
);

The middleware attaches the watsonData object to message. This contains the text response from Assistant. If any error happened in middleware, error is assigned to watsonError property of the message.

Then you're all set!

Features

Message filtering

When middleware is registered, the receive function is triggered on every message. If you would like to make your bot to only respond to direct messages using Assistant, you can achieve this in 2 ways:

Using interpret function instead of registering middleware

slackController.hears(['.*'], ['direct_message'], async (bot, message) => {
  await middleware.interpret(bot, message);
  if (message.watsonError) {
    bot.reply(
      message,
      "I'm sorry, but for technical reasons I can't respond to your message",
    );
  } else {
    bot.reply(message, message.watsonData.output.text.join('\n'));
  }
});

Using middleware wrapper

const receiveMiddleware = (bot, message, next) => {
  if (message.type === 'direct_message') {
    watsonMiddleware.receive(bot, message, next);
  } else {
    next();
  }
};

slackController.middleware.receive.use(receiveMiddleware);

Minimum Confidence

To use the setup parameter minimum_confidence, you have multiple options:

Use it manually in your self-defined controller.hears() function(s)

For example:

controller.hears(
  ['.*'],
  ['direct_message', 'direct_mention', 'mention', 'message_received'],
  async (bot, message) => {
    if (message.watsonError) {
      await bot.reply(message, 'Sorry, there are technical problems.'); // deal with watson error
    } else {
      if (message.watsonData.intents.length == 0) {
        await bot.reply(message, 'Sorry, I could not understand the message.'); // was any intent recognized?
      } else if (
        message.watsonData.intents[0].confidence <
        watsonMiddleware.minimum_confidence
      ) {
        await bot.reply(message, 'Sorry, I am not sure what you have said.'); // is the confidence high enough?
      } else {
        await bot.reply(message, message.watsonData.output.text.join('\n')); // reply with Watson response
      }
    }
  },
);

Use the middleware's hear() function

You can find the default implementation of this function here. If you want, you can redefine this function in the same way that watsonMiddleware.before and watsonMiddleware.after can be redefined. Refer to the Botkit Middleware documentation for an example. Then, to use this function instead of Botkit's default pattern matcher (that does not use minimum_confidence), plug it in using:

controller.changeEars(watsonMiddleware.hear);

Note: if you want your own hear() function to implement pattern matching like Botkit's default one, you will likely need to implement that yourself. Botkit's default set of 'ears' is the hears_regexp function which is implemented here.

Implementing app actions

Watson Assistant side of app action is documented in Developer Cloud A common scenario of processing actions is:

  • Send message to user "Please wait while I ..."
  • Perform action
  • Persist results in conversation context
  • Send message to Watson with updated context
  • Send result message(s) to user.

Using sendToWatson to update context

const checkBalance =  async (context) => {
  //do something real here
  const contextDelta = {
    validAccount: true,
    accountBalance: 95.33
  };
  return context;
});

const processWatsonResponse = async (bot, message) => {
  if (message.watsonError) {
    return await bot.reply(message, "I'm sorry, but for technical reasons I can't respond to your message");
  }
  if (typeof message.watsonData.output !== 'undefined') {
    //send "Please wait" to users
    await bot.reply(message, message.watsonData.output.text.join('\n'));

    if (message.watsonData.output.action === 'check_balance') {
      const newMessage = clone(message);
      newMessage.text = 'balance result';

      try {
        const contextDelta = await checkBalance(message.watsonData.context);
        await watsonMiddleware.sendToWatson(bot, newMessage, contextDelta);
      } catch(error) {
        newMessage.watsonError = error;
      }
      return await processWatsonResponse(bot, newMessage);
    }
  }
};

controller.on('message_received', processWatsonResponse);

Using updateContext to update context

sendToWatson should cover majority of use cases, but updateContext method can be useful when you want to update context from bot code, but there is no need to make a special request to Watson.

if (params.amount) {
  const context = message.watsonData.context;
  context.paymentAmount = params.amount;
  await watsonMiddleware.updateContext(message.user, context);
}

Implementing event handlers

Events are messages having type different than message.

Example of handler:

controller.on('facebook_postback', async (bot, message) => {
  await bot.reply(message, `Great Choice. (${message.payload})`);
});

Since they usually have no text, events aren't processed by middleware and have no watsonData attribute. If event handler wants to make use of some data from context, it has to read it first. Example:

controller.on('facebook_postback', async (bot, message) => {
  const context = watsonMiddleware.readContext(message.user);
  //do something useful here
  const result = await myFunction(context.field1, context.field2);
  const newMessage = { ...message, text: 'postback result' };
  await watsonMiddleware.sendToWatson(bot, newMessage, {
    postbackResult: 'success',
  });
});

Intent matching

The Watson middleware also includes a hear() function which provides a mechanism to developers to fire handler functions based on the most likely intent of the user. This allows a developer to create handler functions for specific intents in addition to using the data provided by Watson to power the conversation.

The hear() function can be used on individual handler functions, or can be used globally.

Used on an individual handler:

slackController.hears(
  ['hello'],
  ['direct_message', 'direct_mention', 'mention'],
  watsonMiddleware.hear,
  async function(bot, message) {
    await bot.reply(message, message.watsonData.output.text.join('\n'));
    // now do something special related to the hello intent
  },
);

Used globally:

slackController.changeEars(watsonMiddleware.hear.bind(watsonMiddleware));

slackController.hears(
  ['hello'],
  ['direct_message', 'direct_mention', 'mention'],
  async (bot, message) => {
    await bot.reply(message, message.watsonData.output.text.join('\n'));
    // now do something special related to the hello intent
  },
);

before and after

The before and after async calls can be used to perform some tasks before and after Assistant is called. One may use it to modify the request/response payloads, execute business logic like accessing a database or making calls to external services.

They can be customized as follows:

middleware.before = (message, assistantPayload) => async () => {
  // Code here gets executed before making the call to Assistant.
  return assistantPayload;
};
middleware.after = (message, assistantResponse) => async () => {
  // Code here gets executed after the call to Assistant.
  return assistantResponse;
});

Dynamic workspace

If you need to make use of multiple workspaces in a single bot, workspace_id can be changed dynamically by setting workspace_id property in context.

Example of setting workspace_id to id provided as a property of hello message:

async handleHelloEvent = (bot, message) => {
  message.type = 'welcome';
  const contextDelta = {};

  if (message.workspaceId) {
    contextDelta.workspace_id = message.workspaceId;
  }

  try {
    await watsonMiddleware.sendToWatson(bot, message, contextDelta);
  } catch(error) {
    message.watsonError = error;
  }
  await bot.reply(message, message.watsonData.output.text.join('\n'));
}

controller.on('hello', handleHelloEvent);

Information security

It may be necessary to be to able delete message logs associated with particular customer in order to comply with GDPR or HIPPA regulations

Labeling User Data

Messages can be labeled with customer id by adding x-watson-metadata header to request in before hook:

watsonMiddleware.before = async (message, payload) => {
  // it is up to you to implement calculateCustomerId function
  customerId = calculateCustomerId(payload.context);
  payload.headers['X-Watson-Metadata'] = 'customer_id=' + customerId;

  return payload;
};

Deleting User Data

try {
  await watsonMiddleware.deleteUserData(customerId);
  //Customer data was deleted successfully
} catch (e) {
  //Failed to delete
}

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.

More Repositories

1

node-sdk

☄️ Node.js library to access IBM Watson services.
TypeScript
1,482
star
2

python-sdk

🐍 Client library to use the IBM Watson services in Python and available in pip as watson-developer-cloud
Python
1,452
star
3

speech-to-text-nodejs

🎤 Sample Node.js Application for the IBM Watson Speech to Text Service
JavaScript
1,086
star
4

swift-sdk

📱 The Watson Swift SDK enables developers to quickly add Watson Cognitive Computing services to their Swift applications.
Swift
880
star
5

java-sdk

🥇 Java SDK to use the IBM Watson services.
Java
586
star
6

unity-sdk

🎮 Unity SDK to use the IBM Watson services.
C#
570
star
7

personality-insights-nodejs

📊 Sample Nodejs Application for the IBM Watson Personality Insights Service
JavaScript
558
star
8

visual-recognition-coreml

Classify images offline using Watson Visual Recognition and Core ML
Swift
489
star
9

assistant-simple

A simple sample application demonstrating the Watson Assistant api.
JavaScript
482
star
10

tone-analyzer-nodejs

Sample Node.js Application for the IBM Tone Analyzer Service
CSS
452
star
11

text-to-speech-nodejs

This is a deprecated Watson Text to Speech Service Demo. A link to the newly supported demo is below
JavaScript
346
star
12

speech-javascript-sdk

Library for using the IBM Watson Speech to Text and Text to Speech services in web browsers.
JavaScript
256
star
13

node-red-labs

Node-RED labs on the use of the Watson Developer Cloud services
208
star
14

natural-language-classifier-nodejs

Deprecated: this demo will receive no further updates
JavaScript
158
star
15

dotnet-standard-sdk

🆕🆕🆕.NET Standard library to access Watson Services.
C#
146
star
16

android-sdk

🔆 Android SDK to use the IBM Watson services.
Java
145
star
17

assistant-with-discovery

DEPRECATED: this application is deprecated and thus will not receive fixes or security updates. It is archived for educational purposes, but may not function.
Java
145
star
18

natural-language-understanding-nodejs

🆕 Demo code for the Natural Language Understanding Service.
JavaScript
133
star
19

api-guidelines

👮 REST API guidelines created for the Watson Developer Cloud services
133
star
20

assistant-toolkit

Toolkit for experimentation with watsonx Assistant
HTML
100
star
21

car-dashboard

Application that demonstrates how the Watson Assistant service uses intent capabilities in an animated car dashboard UI.
JavaScript
91
star
22

node-red-node-watson

A collection of nodes for the IBM Watson services
HTML
83
star
23

discovery-nodejs

This is a deprecated Watson Discovery Service Demo. A link to the newly supported demo is below
JavaScript
76
star
24

go-sdk

🐭 go SDK for the IBM Watson services.
Go
71
star
25

assistant-improve-recommendations-notebook

Assistant Improve notebooks for Watson Assistant
Jupyter Notebook
68
star
26

investment-advisor

DEPRECATED: this repo is no longer actively maintained
JavaScript
66
star
27

speech-android-sdk

DEPRECATED - Please use https://github.com/watson-developer-cloud/android-sdk
Java
66
star
28

dialog-tool

DEPRECATED: this repo is no longer actively maintained
CSS
60
star
29

doc-tutorial-downloads

The download files from the IBM Watson Service documentation tutorials
Shell
57
star
30

language-translator-nodejs

Sample Node.js Application for the IBM Language Translator Service
CSS
51
star
31

sentiment-and-emotion

DEPRECATED: this repo is no longer actively maintained
CSS
50
star
32

simple-chat-swift

DEPRECATED: this repo is no longer actively maintained
Swift
48
star
33

ruby-sdk

♦️ Ruby SDK to use the IBM Watson services.
Ruby
45
star
34

raspberry-pi-speech-to-text

DEPRECATED: this repo is no longer actively maintained
JavaScript
44
star
35

food-coach

DEPRECATED: this repo is no longer actively maintained
JavaScript
39
star
36

assistant-skill-analysis

Dialog Skill Analysis framework for Watson Assistant
Jupyter Notebook
39
star
37

community

Example data that can be used for various Watson services
Shell
34
star
38

speech-to-text-swift

Speech-to-Text example using the Swift SDK
Swift
34
star
39

visual-recognition-code-pattern

JavaScript
33
star
40

react-components

DEPRECATED: this repo is no longer actively maintained
JavaScript
31
star
41

assistant-dialog-flow-analysis

Dialog Flow Analysis Notebook for Watson Assistant
HTML
28
star
42

salesforce-sdk

A Salesforce library for communicating with the IBM Watson REST APIs
Apex
28
star
43

conversation-connector

The Conversation connector is a set of components that mediate communication between your Conversation workspace and a Slack or Facebook app. Use the connector to deploy a chat bot that Slack or Facebook Messenger users can interact with.
JavaScript
27
star
44

document-conversion-nodejs

DEPRECATED: Please use https://github.com/watson-developer-cloud/discovery-nodejs
JavaScript
27
star
45

text-to-speech-java

DEPRECATED: this repo is no longer actively maintained
CSS
27
star
46

assistant-web-chat-service-desk-starter

A starter kit for building custom service desk integrations for Watson Assistant web chat
TypeScript
25
star
47

raspberry-pi-time-weather-demo

DEPRECATED: this repo is no longer actively maintained
JavaScript
24
star
48

assistant-demo

Assistant demo
JavaScript
23
star
49

discovery-starter-kit

IBM Watson Discovery Starter Kit
JavaScript
22
star
50

assistant-intermediate

An intermediate example of Watson Assistant in a Node.js application
JavaScript
22
star
51

discovery-components

IBM Watson Discovery components
TypeScript
22
star
52

assistant-with-discovery-openwhisk

DEPRECATED: this repo is no longer actively maintained
CSS
21
star
53

company-insights

DEPRECATED: this repo is no longer actively maintained
JavaScript
20
star
54

text-to-speech-swift

DEPRECATED: this repo is no longer actively maintained
Swift
20
star
55

social-customer-care

DEPRECATED: this repo is no longer actively maintained
CSS
19
star
56

speech-to-text-websockets-ruby

Ruby client that interacts with the IBM Watson Speech to Text service through its WebSockets interface
Ruby
19
star
57

customer-engagement-bot

DEPRECATED: this repo is no longer actively maintained
JavaScript
18
star
58

abap-sdk-nwas

ABAP code for using IBM Watson Developer Services with SAP NetWeaver Application Server, imported via abapGit
ABAP
18
star
59

assistant-web-chat

Language strings for web chat integration of IBM watsonx assistant
JavaScript
16
star
60

python-primer-companion-code

DEPRECATED: this repo is no longer actively maintained
Python
15
star
61

spring-boot-starter

Spring Boot support for Watson services
Java
13
star
62

personality-insights-java

DEPRECATED: this repo is no longer actively maintained
CSS
13
star
63

watson-developer-cloud.github.io

Index page with links to SDKs, docs, etc.
HTML
13
star
64

simple-chat-objective-c

DEPRECATED: this repo is no longer actively maintained
Objective-C
12
star
65

text-bot-openwhisk

DEPRECATED: this repo is no longer actively maintained
JavaScript
12
star
66

ui-components

DEPRECATED: this repo is no longer actively maintained
CSS
12
star
67

openwhisk-sdk

🆕 SDK for using Watson Services on IBM Cloud Functions (based on Apache Openwhisk) - DEPRECATED
JavaScript
12
star
68

app-insights-discovery

DEPRECATED: this repo is no longer actively maintained
Swift
10
star
69

customer-engagement-nodejs

Customer Engagement
JavaScript
10
star
70

token-generator

Basic Node.js Server to generate watson auth tokens from user-supplied credentials.
JavaScript
6
star
71

watson-vision-coreml-code-pattern

Watson Visual Recognition CoreML Code Pattern
CSS
5
star
72

abap-sdk-scp

ABAP code for using IBM Watson Developer Services with SAP Cloud Platform, imported via abapGit with dependencies via APACK
ABAP
5
star
73

restkit

Core networking and authentication library for the Watson Swift SDK
Swift
4
star
74

cognitive-client-java

DEPRECATED: this repo is no longer actively maintained
Java
4
star
75

speech-to-text-utils

Speech to text CLI that helps you manage speech customizations.
JavaScript
4
star
76

homebrew-tools

DEPRECATED: this repo is no longer actively maintained
Ruby
3
star
77

language-translator-tooling

DEPRECATED: this repo is no longer actively maintained
JavaScript
2
star
78

natural-language-classifier-intent-classification-demo

Deprecated
JavaScript
2
star
79

discovery-nodejs-static

Sample Node.js application that uses the IBM Watson Discovery Service
JavaScript
2
star
80

natural-language-understanding-code-pattern

Natural Language Understanding Code Pattern
JavaScript
2
star
81

speech-to-text-code-pattern

React app using the Watson Speech to Text service to transform voice audio into written text.
JavaScript
2
star
82

swift-playgrounds

Swift playgrounds for Watson Developer Cloud services
Swift
2
star
83

sdk-example-editor

Web application that helps edit SDK examples from an OpenAPI file.
JavaScript
1
star
84

actions-logging-server

HTML
1
star
85

Watson-Assistant-Workspace-Retrain

Python
1
star
86

actions-analytics-dashboard

JavaScript
1
star
87

assistant-web-chat-react

A React library to make integration of Watson Assistant web chat with a React application easy.
TypeScript
1
star
88

visual-recognition-utils

Command line tools to make creating & managing Watson Visual Recognition Custom Classifiers easier.
JavaScript
1
star