It’s never been a more exciting time to create a chatbot. With the rise of AI-based APIs from cognitive initiatives such as IBM Watson, and thanks to great SDKs like open source toolkit Botkit, it’s easier than ever to create your own chatbot to respond to FAQs from your customers, optimize deployment of your customer service staff, and complete those routine and mundane tasks. In the developer journey Create a cognitive news search app, which demonstrates how to serve news articles with the Watson Discovery API, we use Botkit to create a chatbot that acts as a middleman to facilitate queries to the Watson Discovery Service. These queries come from conversations with users in Slack, the real-time messaging and collaboration platform.

Slack’s Real Time Messaging API

Slack has multiple APIs that you can use to create bots and commands, but we decided to use the Real Time Messaging (RTM) API in our developer journey. This API lets you create a connection with Slack. Any request to the bot user, to the bot directly, or from a Slack channel in which the bot is a member, is sent to the server, which starts the connection. Slack uses the bot token as a means to authenticate. Botkit provides a great abstraction layer over this Slack API, enabling you to easily create a Slack bot in few minutes in a Node.js application. Following is the code you need to establish a connection with Slack and make your bot user active on Slack:
const Botkit = require('botkit');
const controller = Botkit.slackbot();
const bot = controller.spawn({
  token: ''
}).startRTM();

Listen and respond to messages

Listening for messages is as easy as specifying the keywords you want to listen to. You also need to specify whether those keywords are directed to the bot user or the Slack channel. Here, we’re listening for a direct message or mention of the bot user in a Slack channel, and we respond with a text reply of Hello:
controller.hears(['hello', 'hi'], 'direct_message,direct_mention,mention', function(bot, message) {
  bot.reply(message, 'Hello');
});

Having a conversation

In the developer journey, we’re trying to create a bot that feels realistic by having it start a conversation with the user to determine what news the user is interested in. We can start a conversation this way:
bot.startConversation(message, function(err, convo) {
  convo.say('Hi there!');
  convo.ask('What news are you interested in?', function(response, convo) {
    // Response passed here
  });
});

Calling convo.next(); inside the ask callback function moves the bot forward in conversation until convo.on('end', callback) is called. You can also end the conversation at any time by calling convo.stop(), or repeat a conversation with convo.repeat(). Inside the callback for the end event, the callback gets passed the status of the conversation, which can be marked as completed to indicate the end of the conversation without being stopped anywhere. You can make a network request to the Watson Discovery Service to get the list of articles and reply to the user with links to the news articles for that topic. Check out the Create a cognitive news search app developer journey to learn how to implement a news chatbot, and then get busy creating your own. Pull requests for the journey are welcome, as are any comments you care to leave below.

1 comment on"Integrating Slack with Watson Discovery News"

Join The Discussion

Your email address will not be published. Required fields are marked *