The smartest member of my team is a Slack bot (or, how to build an AI powered assistant)

Mike Sheldon
Bloomsbury AI
Published in
7 min readJan 15, 2018

Cape is an AI that reads documents and then answers questions about them.

It does this by finding the bit of text in a document that answers your question. It allows you to leverage state-of-the-art tech to build your own AI with a few lines of code.

In this tutorial, we’ll show you how you can build an AI powered slack bot in 10 minutes.

Join our public Slack channel and get to know our own bot, capebot, here.

The above video shows two of the bots we use internally at Bloomsbury AI.

The first, capebot, makes use of the saved reply feature to answer questions about a wide range of topics, e.g.

  • What’s the office WiFi password?
  • Where are certain documents kept?
  • What are our best practices?

It uses a sentence similarity model to recognise questions however they’ve been phrased (for example, “What’s our website?” vs “Where’s our web page?”)

The second, pythonbot, answers questions about our python API. It does this using Cape’s machine reading feature to automatically extract answers from our API docs.

Both bots use exactly the same code, they’ve just had different information uploaded into Cape.

Getting Set Up

To follow this tutorial you will need:

First clone our tutorial repository. This contains example code at various stages of development:

git clone https://github.com/bloomsburyai/cape-slack-tutorial.git

Inside the new cape-slack-tutorial directory you’ll find the following files:

  • step-1.py The skeleton of a Slack bot which just responds with “Hello!” whenever a user sends it a message.
  • step-2.py A bot capable of answering questions about documents that have been uploaded through the Cape admin interface.
  • step-3.py A bot with additional functionality to allow users to add saved replies from directly within Slack.
  • get-id.py A helper script for finding out your bot’s Slack ID.
  • requirements.txt A list of all the python module dependencies required to complete the tutorial.

To install the python dependencies first run:

pip3 install -r requirements.txt

This will fetch the slackclient andcape-client modules.

Step 1: A Basic Slack Bot

To begin with we’ll create a simple slack bot that just responds with “Hello!” whenever a user sends it a message.

First visit the Slack bot page and click the “Install” or “Add configuration” button to create a new bot and add it to your workspace. If you don’t see the button you’ll either need to ask permission from your slack workspace owner or simply create your own workspace.

You’ll then be presented with your Slack bot’s API key:

You’ll also need to discover your Slack bot’s ID. Included in the tutorial repository is a script called get-id.py which can help with this. Simply run:

./get-id.py your_slack_key you_bot_name

This will output your bot’s ID, which will look something like: U123A4BCD

Next edit step-1.py to include your details:

Whenever someone sends a message that includes our bot’s username the handle_question function will be run:

Which currently responds to all messages by sending the string “Hello!” to the channel it was contacted on. In the next section we’ll modify this function to send the user’s message to Cape and get a more intelligent response.

We’ve only covered the bare essentials required for creating a basic Slack bot here, for a more in depth tutorial focusing purely on creating Slack bots check out Matt Makai’s “How to Build Your First Slack Bot with Python.”

Step 2: Answering Questions

Training our bot

Before our bot can start replying to users we need to feed it some useful information. We can do this through the Cape admin dashboard, if you don’t already have a Cape account you can sign up for free.

You can then train the bot in two different ways:

  • Documents — After uploading documents Cape will use machine reading to find answers to a user’s question within them.
  • Saved replies — These are simple pairs of questions and answers. Cape uses a sentence similarity model to match user questions with the saved questions and then provide the corresponding answer.

You can make use of both documents and saved replies within the same bot. If a saved reply already exists which matches the user’s question then Cape will respond with this, otherwise it will read your documents and attempt to find the answer within them.

Adding our Cape API key

To retrieve your API key visit the Cape dashboard and click the settings icon in the top right:

Settings

This will then reveal a drop down showing your API key:

Your user token

Copy this and add it to step-2.py alongside your Slack settings:

Responding to users

Now when we receive a question from a user we can pass it along to Cape using CapeClient’s answer method:

This returns a list of answers ordered by confidence, we then send the user the text of the first answer that we found.

If Cape doesn’t find an answer above our requested threshold we let the user know that we weren’t able to answer the question. You can set the confidence threshold for your bot in the Cape dashboard settings.

Now when we talk to the bot we get a response back based on any saved replies we entered or documents we uploaded:

Step 3: Allowing users to teach the bot

Adding our admin token

To allow our bot to do more than just answer questions we need to provide it with access to our admin token. The admin token gives the client access to API methods which can make modifications to our bot, such as adding saved replies and uploading documents.

You’ll find your admin token in the settings panel just underneath your question answering token:

Your admin token

This can then be added to the settings at the top of step-3.py:

Handling user input

We then modify the client so that if a message starts with .add-saved-reply we call the add_saved_reply method, otherwise we treat it as a question and answer it normally:

We can then make use of the add_saved_reply method in CapeClient to create a new saved reply based on the user’s input:

If we encounter any exceptions from Cape (e.g. if the supplied question already exists) we report this back to the user via a Slack message.

Now when this question (or a similar phrasing of it) is asked our bot responds with the answer we supplied:

Next Steps

For a more advanced bot check out our cape-slack repository, this is the Slack bot we use ourselves at Bloomsbury AI. In addition to the features in this tutorial it can also:

  • Run multiple bots simultaneously.
  • Allow users to add paraphrases to questions.
  • Provide multiple possible answers to a question.
  • Explain why it has answered a question the way it has.
  • Show the context surrounding an answer in a document.

Wrap Up

In this tutorial we showed you how simple it is to use Cape to create your own AI Slack bot.

You could also use Cape to:

  • Build a super-powered ctrl+f that finds the answer to questions like ‘Who is the CFO?’, rather than just all the occurrences of a keyword. See our tutorial and demo.
  • Build an expressive query tool for textual data — e.g. find the number of product reviews where a customer mentions they made a complaint.
  • Build an add-on to your private search that mimics Google’s “direct answers”.

This is our mission at Bloomsbury AI (the company behind Cape) — to make the expertise stored in documents and in people’s heads as accessible as possible.

We’re constantly trying to improve our documentation and support developers using our AI so please reach out with any feedback!

--

--