Easy Slack integration for Watson Conversation

Share this post:

If you’re an application developer, you’ve already heard about the growing wave of chat bots. One of the great advantages of a chat bot over a web or mobile application is that, if done correctly, it doesn’t require a user to leave the place they are already working. A chat bot can be made available via text message, through a sidebar on a webpage, or even as a member in a group chat, providing value where it is most needed.

This tutorial, the first in a series, demonstrates how the IBM Watson Conversation service can be integrated with the Slack platform to connect with a third-party source of information—in this case, the Google Calendar API. The end goal is to enable you to create a Slack bot that can actively listen to conversations in a channel and assist with scheduling meetings.

At this first stage, your Slack bot will be able to:

  • Get permissions for multiple users’ calendars
  • Query the calendars for busy times
  • Query the calendars for free times

We’ve built this tutorial in Python and provided all the code here. Clone that repo and let’s get started!

The birth of your Slack bot

A lot of smart people have already written about how to build a Slack bot, so I’m not going to rehash here. Instead, I’d like to point you to this excellent tutorial by Matt Makai (Developer Evangelist at Twilio and speaker at the Watson Developer Conference). Matt gives you all the steps necessary to get your bot set up, and we’ve used what he shared as the starting point for our bot.

If you haven’t already, start by cloning our sample repo. Once you’ve done this, rename the folder to ‘slack-starterbot’, and navigate to this folder from your command line At this point, follow the steps in Matt’s tutorial, up until the ‘Coding Our StarterBot’ section. You can name your bot whatever you’d like (we’ve named ours ‘watson’, surprised?), just make sure you store your BOT_ID and SLACK_BOT_TOKEN as environment variables. In your repo you’ll find a ‘’ file ready to go with all the extra code for authenticating to Google and connecting to the Watson Conversation service.

Google Calendar authentication

Google uses OAuth2 for their Calendar API, which asks the user to provide explicit permission for your application. We’ll discuss how we handle this in Slack, but you will first need to obtain some application credentials from Google. To do this, go here and follow steps 1 and 2. When you download your ‘client_secret.json’ file, make sure to save it in your ‘slack-starterbot’ directory.

The authentication flow consists of three steps:

  1. User sends a message to your bot, and the bot returns a URL. This URL is generated by the ‘get_auth_url’ method in the ‘’ file and uses the information in the ‘client_secret.json’ file. This is what the process looks like for the user:screenshot1
  2. User clicks on link, launching an authentication page. Once the user accepts, this redirects to a page with a token.
  3. The user copies the token and shares with the Slack bot. This is handled in the ‘set_auth_token’ method.


Once a user has authenticated, they will no longer need to go through this               process.

 Training Conversation intents

It would be easy to build your Slack bot to follow a few simple commands, such as “show my schedule.” But remember, the goal is to have a bot that can listen in to conversations and provide help at the appropriate times. This requires some natural language understanding, which is where the Watson Conversation service will help.

In this first stage, you are going to train the Watson Conversation service to recognize two similar but distinct categories of request: show me my schedule, and tell me what free times I have available. No matter what words the user chooses, you want to provide the information formatted in a way that directly matches the request. For example, if I ask about free times I don’t want details about the meetings that are on my calendar, but if I am asking about my schedule this is important information.

I’ve already written about how to get started with Watson Conversation, so I’ll dive right in to the intents you are training for. If you’d like to use what we’ve already built out, upload the ‘calendar_bot.json’ file from the cloned repo as a new Conversation workspace. For this bot, we’re focusing on two main intents. We’ve named them “#schedule” and “#free_time,” and the training data for both looks like this:



You’ll notice there are other intents as well, including “#greetings,” “#goodbyes,” and “#anything_else.” These help drive a very simple dialog for any user input that doesn’t match one of our main tasks:


Now that you’ve configured and trained your Conversation instance, you’ll want to include it in your ‘’ script. The first step is to get the Workspace ID as well as the user name and password for your Conversation instance. You’ll find a spot for this information near the top of ‘’. Next, you’ll want to install the Watson Developer Cloud Python SDK:

pip install watson-developer-cloud

In the ‘handle_command’ method, you can see a call to the Conversation endpoint. Once the user has been authenticated, you pass all input to the Conversation service. If the intent is either “#schedule” or “#free_time,” a call is made to the ‘calendar_usage’ method to get up to ten upcoming events using the Google Calendar API. Depending on the specific intent, the response will be formatted differently. Schedule requests look like this:


And requests for free time look like this:



Finally, any other input will be handled according to our simple dialog:


Next steps

With the Slack bot set up, authentication ready to go, and a Watson Conversation instance trained and connected, you have everything you need to get your Slack bot working. As mentioned above, this is only the first stage. There are a number of improvements that can be made, including:

  • Using entity detection to pick out specific days and times for more refined calendar searches
  • Comparing calendars for multiple users to find common free times
  • Making the bot an active listener that provides useful information when appropriate, without needing to be explicitly addressed

In future tutorials we will dig into this list of additional functionality and further explore the features of the Watson Conversation service and the Slack platform. If you have questions or feedback, please let us know!


Experiment with Watson Conversation

Add Comment
No Comments

Leave a Reply

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

More Developers Stories
November 20, 2017

How fund managers can apply AI to turn data into insights, reduce bias in decisions and generate alpha

In this age of rampant data growth, the only way to reliably beat the market on a risk-adjusted basis is to mine unstructured data faster and more accurately than competitors. Companies that combine AI, and machine learning with speed, accuracy, nuance and contextual awareness will change the game of managing and growing investments.

Continue reading

November 16, 2017

Get the Forrester Report on how to make customer service smarter, faster and more cost effective

Call centers executives face the constant challenge of meeting customer expectations and business cost goals. Customers prefer interacting with virtual agents, and are choosing messaging over phone calls for issue resolution. This Forrester report outlines the trends that will enable call centers to become smarter and more strategic.

Continue reading

November 14, 2017

Top 10 ways that AI will impact business in the next decade

AI already impacts many aspects of our daily lives at work and at home. Over the next decade, experts predict that AI enterprise software revenue will grow from $644 million to nearly $39 billion. Here are the top 10 ways that we think AI will impact business over the next 10 years.

Continue reading