Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building Slack Bots With IBM Watson Conversation

DZone's Guide to

Building Slack Bots With IBM Watson Conversation

This cool (and simple) project makes use of IBM Watson Conversation through the open source project Botkit to create some interesting features in a Slack bot!

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

I’ve open sourced a simple sample that shows how to leverage IBM Watson Conversation in Slack bots via the open source project Botkit. With Botkit and a Watson middleware, text messages defined in Conversation dialogs can easily be used in Slack bots. My sample shows, additionally, how to use Slack buttons in messages and how to invoke business logic at certain stages of the conversation.

Botkit is an open source framework to build bots that can be connected to popular messaging platforms like Slack and Facebook Messenger. IBM provides a middleware to easily leverage the conversation flows defined in Watson Conversation dialogs. The following code shows how to pass user input to Watson and how to return text messages to Slack.

var watsonMiddleware = require('botkit-middleware-watson')({
    username: YOUR_CONVERSATION_USERNAME,
    password: YOUR_CONVERSATION_PASSWORD,
    workspace_id: YOUR_WORKSPACE_ID,
    version_date: '2016-09-20',
slackController.middleware.receive.use(watsonMiddleware.receive);
slackController.hears(['.*'], ['direct_message', 'direct_mention', 'mention'], function(bot, message) {
      bot.reply(message, message.watsonData.output.text.join('\n'));

In order to provide the best possible user experience, my sample shows how to use Slack buttons in messages so that users can, for example, easily pick one of the available options. Additionally, the sample demonstrates how to invoke a REST API provided by Weather Company Data. Here is a simple sample conversation:

slack

Slack provides a Message Builder to define sophisticated JSON messages that include elements like buttons. Watson Conversation is a general purpose tool and doesn’t support messaging client specific functionality. In order to include the Slack message, I’ve put the JSON into a context attribute in a dialog node.

workspace1

This is the complete JSON:

          "In which city? Berlin, Munich or Hamburg?"
              "title": "Which location?",
              "attachment_type": "default"

When the ‘slack’ attribute is part of the message, the content of this attribute is sent as a message to Slack rather than the text message defined in the Conversation dialog.

Similarly, I’m using another attribute ‘action’ to mark a node in the dialog as the stage where business logic is triggered- in this case, a REST API to the weather service is invoked. Note that you can put the chosen location entity into the JSON.

          "looking up weather information for @location ..."
      "selection_policy": "sequential"

This is the JavaScript code for the action.

function  lookupWeather(watsonDataOutput, bot, message) {
      let  location = watsonDataOutput.context.action.location;
              coordinates = '48.13/11.58'  ;
              coordinates = '53.55/9.99'  ;
              coordinates = '52.52/13.38'  ; // Berlin
      let  weatherUsername = process.env.WEATHER_USERNAME;
      let  weatherPassword = process.env.WEATHER_PASSWORD;
      let  weatherUrl = 'https://'  + weatherUsername + ':'  + weatherPassword + '@twcservice.mybluemix.net:443/api/weather/v1/geocode/'  + coordinates + '/observations.json?units=m&language=en-US'  ;
      request(weatherUrl, function  (error, response, body) {
          var  info = JSON.parse(body);
          let  answer = "The current temperature in "  + info.observation.obs_name
              + " is "  + info.observation.temp + " °C"
          bot.reply(message, answer);

Check out the GitHub project for more details and the setup instructions.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
bots ,watson ,ai ,ibm ,integration ,slack

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}