DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Building Slack Bots With IBM Watson Conversation

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!

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
Jun. 28, 17 · Integration Zone · Tutorial
Like (2)
Save
Tweet
6.06K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Slack (software) Conversations (software)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Image Classification Using SingleStore DB, Keras, and Tensorflow
  • How to Use Geofences for Precise Audience Messaging
  • Top 20 Git Commands With Examples
  • A Guide to Events in Vue

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo