Estimated time: 60 min

Build a besserwisser bot for Slack with BERT

Philipp Eisen, a research engineer at Peltarion, got the brilliant idea to play around with the newly released BERT to see if he could make a model for identifying patterns in Slack messages to let people know if they posted in the wrong channel.

It turns out, he could. And you can do it too! If you need another besserwisser (do you, really?) at work, here’s how to build one.

Target audience: Data scientists and developers

Peltarion Besserwisser live on Slack. Image on Slack icon via buttsss.com by Pablo Stanley.

You will learn to

  • Collect and preprocess data.
  • Use BERT – We have prebuilt the whole BERT network as a snippet, so you don't have to build this huge network yourself.
  • Set your model live on Slack.

Start here

  1. Clone the Peltarion/slack-besserwisser repo.
  2. Navigate to this repo in the Terminal.
  3. Make sure to have Docker installed. One way to do that is to check which version you have installed by running this command:
    docker --version

For Linux only: Make sure to have docker-compose installed as well (on MacOS and Windows it comes with the Docker client).

Create a Slack app that can collect your conversation history

  1. In a browser, navigate to Creating, managing, and building apps for Slack.
  2. Click Create a Slack app. Name the app (e.g., Besserwisser, TheWhip, or AnnoyingBot), select Development Slack Workspace, and click Create App.
  3. Under Add features and functionality there is a heading called Permissions. Click it.
  4. Under the heading Scopes click Add an OAuth Scope and add:
  • channels:history – This will allow the app to view messages and other content in your public channels.
  • channels:read – This will allow the app to view basic information about public channels in your workspace.
  1. At the top of the page, click Install App to workspace and then allow the app access your Slack workspace.
  2. Copy the OAuth Access Token.

Paste OAuth Access Token to reader.env

Open the file slack-channel-reader/env/reader.env and set SLACK_APP_TOKEN to the OAuth access token you just copied.

You have just allowed the app to read your Slack history.

The Data Scrape your conversation history

It’s time to scrape your Slack history. At Peltarion, we believe it's our job to make life – or at least AI – easy for you, so we've built a script for you to use.

  1. Navigate to the slack-channel-classifier repo in the Terminal.
  2. Run the following command. By default, it will read up to 6,000 Slack messages, each from Random, and General and save it to channels.csv in the root of this repository.
    docker-compose -f docker/docker-compose.yml run slack-reader

Change channel or number of messages

If you want, you can change which channels to scrape, the number of messages per channel and the name of the output file.

Example: Scrape 500 messages from Frontend and Research and save to 2channels.csv.
docker-compose -f docker/docker-compose.yml run slack-reader --limit 500 --channels frontend research --output 2channels.csv

Get help with optional arguments

To get the help message on optional arguments, run:
docker-compose -f docker/docker-compose.yml run slack-reader --help

Build, train and deploy a model on the platform

Now it’s time to build, train and deploy a model.

If you haven't yet, sign up to the Peltarium Platform. The community edition is free and users automatically receive:

  • Access for up to two users
  • 50 GPU hours for FREE
  • 50 GB storage for FREE
  • Option to 'pay as you go' for further quota

Import the messages to the Peltarion Platform

When you're in the platform:

  1. Create a project on the platform and navigate to the Datasets view.
  2. Click New dataset and upload the csv with your Slack messages.
  3. Select the message feature, click the spanner and make sure that:
    - Encoding is Text
    - Sequence length is 64
    - Language model is English BERT uncased
  4. Select the channel feature and set Encoding to Categorical.
  5. Save this version of the dataset and navigate to the Modeling view (click the Modeling icon).

Use the BERT snippet

  1. In the Modeling view create a new experiment. Select the BERT English uncased snippet. Make sure the Weights trainable (all blocks) checkbox is selected.
  2. In the Input block set Feature to message.
  3. In the Target block set Feature to channel.
  4. Click the Settings tab and change:
  • Batch size to 64 or larger depending on your sequence length check in our BERT cheat sheet
  • Epochs to 2
  • Learning rate to 0.000001 (5 zeros)

Click Run to fire up BERT!

Navigate to the Evaluation view and watch the model train. Because BERT is a very large and complex model, nothing much will show in the beginning since the training will take a long time.

 You can currently expect more than four hours for two epochs when you use sequence length 512.

Enable the deployment

Finally, when the model has been trained, navigate to the Deployment view and create a new deployment. Click Enable to set the deployment live. Keep this window open. You'll soon need to copy information from the Deployment view.

Set the Slack bot live in a #channel

  1. Go to your Slack app page.
  2. Select your existing app.

Create a bot user for your app

  1. Open Add features and functionality.
  2. Select Bots.
  3. Click Add a Bot User and then Add Bot User.

Get the Bot User OAuth Access Token

  1. Open OAuth & Permissions in the left-side menu.
  2. Click Reinstall App.
  3. Copy the Bot User OAuth Access Token.

Paste Bot User OAuth Access Token to bot.env

In your cloned slack-besserwisser repo, open the file slack-besserwisser/env/bot.env and set SLACK_BOT_TOKEN, to the Bot User OAuth Access Token you just copied.

Use the Peltarion deployment information

Go to the Deployment view of your project and find the deployment's URL and Token.

Paste Peltarion tokens to bot.env

In your cloned slack-besserwisser repo, open the file slack-besserwisser/env/bot.env and paste PELTARION_DEPLOYMENT_URL and PELTARION_DEPLOYMENT_TOKEN respectively.

Run your Slack bot with Docker

You're all set. Time to run the bot live.

  1. Navigate to the slack-besserwisser repo in the Terminal.
  2. Run this command to start the bot:
    docker-compose -f docker/docker-compose.yml up slack-bot

Stop bot if you want

If by any chance you want to stop the bot, just run the following command:

docker-compose -f docker/docker-compose.yml down slack-bot

Let the bot into your Slack

Open Slack and create a new channel.

Type @your_bot_name and click Invite them. Your bot is now live!

You could invite the bot to an already existing channel, but there is a risk that the bot will spam the channel. It could be funny but will most likely be annoying. Better to test the bot in a new channel.

Test the bot

Test the bot by typing "Hello bot," or some other generic text. Try to mimic the tone of the conversation in one of the channels you scraped.

If the bot classifies the post as belonging to another channel, the bot will answer with:
"I'm XX.XX% sure that this belongs in #the_channel_where_the_bot_is_set_live".

Our CEO posting in the wrong channel, according to Besserwisser anyway 😉

Bot answers too often? Change the threshold value

If the bot responds too often or seldom, change the threshold value.
We've set the threshold value to 0.7, i.e., if the bot calculates that it's more than 70% probable that a post belongs in another channel, then the bot will respond to the post. If the bot replies too often, it’s likely this isn’t a good value for your bot.

This is how you change the threshold value:

  1. In the slack-besserwisser repo, open the file slack-besserwisser/listen_and_reply.py.
  2. Locate the line where you set the THRESHOLD and change the value to higher or lower than 0.7, depending on what you need.

Deploy your besserwisser bot in Google Cloud

The bot is now running from your local machine. If you shut it down, your bot will go silent. For a more robust setup, you can use Google Cloud, as described in the next section.

Note that you will have to pay Google around $5 per month for the machine that you are running.

Besserwisser can be deployed in any of the public clouds out here (or private) but we will provide instructions for GCP deployment

Set up a project and enable the Container Registry API

  1. Go to console.cloud.google.com.
  2. Select or create a new project in which you want your bot to run in. Take note of your project id  this is not necessarily the same as the project name, i.e., you don't have to name it besserwisser, even though it's a fun name.
  3. Type Container Registry in the search bar, then select the Container Registry API.
  4. If you haven't used the container registry API, you will be asked to enable it.
    Press Enable Container Registry API.

Set up Google Cloud SDK and Docker

  1. If you don't have the Google Cloud SDK installed, follow the official instructions here to do so.
    On MacOS you can also install the Google Cloud SDK with brew. This might be more convenient for you. To do so, run:
    brew cask install google-cloud-sdk
  2. To set up the gcloud sdk, run: 
    gcloud init
  3. To make sure you can push images to Google Cloud with your Docker CLI, run: 
    gcloud auth configure-docker

Build and push an image to the Container Registry

  1. Make sure you are in the directory slack-besserwisser.
  2. After replacing YOUR_PROJECT_ID with the project id of your Google Cloud project run:
    docker build -t eu.gcr.io/YOUR_PROJECT_ID/besserwisser:1.0.0 -f docker/bot.Dockerfile .
    Note this will host your Docker image in an EU location. You can change that to North America, e.g., by changing the host component “eu.gcr.io” to “gcr.io”. Click here for more info.
  3. After building the image has succeeded push the image to Google Cloud container registry, run: docker push eu.gcr.io/YOUR_PROJECT_ID/besserwisser:1.0.0

Deploy in Google Cloud Compute Engine

  1. Go back to console.cloud.google.com.
  2. Type compute engine in the search bar and select it.
  3. Press Create Instance and select a name for the instance.
  4. For machine type select f1-micro. You could choose a larger machine if you want your Slack bot to be faster.
  5. Tick the checkbox: Deploy a container image to this VM instance.
  6. For the container image, paste the name of the image you pushed (for example: eu.gcr.io/YOUR_PROJECT_ID/besserwisser:1.0.0)
  7. Press Advanced container options and scroll down to Environment Variables.
  8. Enter the same values as you did in slack-besserwisser/env/bot.env.
  9. Scroll all the way to the bottom and press Create.

Done!

The image for your Slack bot is now in Google Cloud.

Get started