
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
botkit-middleware-watson
Advanced tools
A middleware for using Watson Conversation in a Botkit-powered bot.
This middleware plugin for Botkit allows developers to easily integrate a Watson Conversation workspace with multiple social channels like Slack, Facebook, and Twilio. Customers can have simultaneous, independent conversations with a single workspace through different channels.
before
: pre-process requests before sending to Watson Conversation (Conversation).after
: post-process responses before forwarding them to Botkit.$ npm install botkit-middleware-watson --save
The middleware needs you to provide the username
, password
, and workspace_id
of your Watson Conversation chat bot. If you have an existing Conversation service instance, follow these steps to get your credentials.
If you do not have a Conversation service instance, follow these steps to get started.
This document shows code snippets for using a Slack bot with the middleware. (If you want examples for the other channels, see the examples/multi-bot folder. The multi-bot example app shows how to connect to Slack, Facebook, and Twilio IPM bots running on a single Express server.)
You need a Slack token for your Slack bot to talk to Conversation.
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.
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:
var slackController = Botkit.slackbot();
Spawn a Slack bot using the controller:
var slackBot = slackController.spawn({
token: YOUR_SLACK_TOKEN
});
Create the middleware object which you'll use to connect to the Conversation service:
var watsonMiddleware = require('botkit-middleware-watson')({
username: YOUR_CONVERSATION_USERNAME,
password: YOUR_CONVERSATION_PASSWORD,
workspace_id: YOUR_WORKSPACE_ID,
version_date: '2016-09-20'
});
Tell your Slackbot to use the watsonMiddleware for incoming messages:
slackController.middleware.receive.use(watsonMiddleware.receive);
slackBot.startRTM();
Finally, make your bot listen to incoming messages and respond with Watson Conversation:
slackController.hears(['.*'], ['direct_message', 'direct_mention', 'mention'], function(bot, message) {
bot.reply(message, message.watsonData.output.text.join('\n'));
});
The middleware attaches the watsonData
object to message. This contains the text response from Conversation.
Then you're all set!
The watsonMiddleware object provides some useful functions which can be used for customizing the question-answering pipeline.
They come in handy to:
receive
The receive function is the one which gets triggered on incoming bot messages. One needs to bind it to the Botkit's receive middleware in order for it to work.
// Connect to Watson middleware
slackController.middleware.receive.use(middleware.receive);
Then simply respond to messages as follows:
slackController.hears(['.*'], ['direct_message', 'direct_mention', 'mention'], function(bot, message) {
bot.reply(message, message.watsonData.output.text.join('\n'));
});
Note: The receive function is triggered on every message. Please consult the Botkit's guide to the receive middleware to know more about it.
interpret
The interpret()
function works very similarly to the receive function but unlike the receive function,
The interpret function only gets triggered when an event is heard by the controller. For example, one might want your bot to only respond to direct messages using Conversation. In such scenarios, one would use the interpret function as follows:
slackController.hears(['.*'], ['direct_message'], function(bot, message) {
middleware.interpret(bot, message, function(err) {
if (!err)
bot.reply(message, message.watsonData.output.text.join('\n'));
});
});
hear
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, function(bot, message) {
bot.reply(message, message.watsonData.output.text.join('\n'));
// now do something special related to the hello intent
});
Used globally:
slackController.changeEars(watsonMiddleware.hear);
slackController.hears(['hello'], ['direct_message', 'direct_mention', 'mention'], function(bot, message) {
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 callbacks can be used to perform some tasks before and after Conversation 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 = function(message, conversationPayload, callback) {
// Code here gets executed before making the call to Conversation.
callback(null, customizedPayload);
}
middleware.after = function(message, conversationResponse, callback) {
// Code here gets executed after the call to Conversation.
callback(null, conversationResponse);
}
This library is licensed under Apache 2.0. Full license text is available in LICENSE.
v1.3.0
Fixed the parameters for the after() call in the interpret function.
FAQs
A middleware for using Watson Assistant in a Botkit-powered bot.
The npm package botkit-middleware-watson receives a total of 5 weekly downloads. As such, botkit-middleware-watson popularity was classified as not popular.
We found that botkit-middleware-watson demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.