Shape up your Slack chaos
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: Expert users
You will learn to
Collect and preprocess data.
Use BERT – We have prebuilt the whole BERT network as a block, so you don’t have to build this huge network yourself.
Set your model live on Slack.
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
In a browser, navigate to Creating, managing, and building apps for Slack.
Click Create a Slack app. Name the app (e.g., Besserwisser, TheWhip, or AnnoyingBot), select Development Slack Workspace, and click Create App.
Under Add features and functionality there is a heading called Permissions. Click it.
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.
At the top of the page, click Install App to workspace and then allow the app access your Slack workspace.
Copy the OAuth Access Token.
Paste OAuth Access Token to reader.env
In the repo directory, open the file
reader.env. It’s located in the
slack-channel-reader/env/ folder. 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.
Navigate to the
slack-channel-classifierrepo in the Terminal.
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.
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 Peltarion Platform.
Import the messages to the Peltarion Platform
When you’re in the platform:
Create a project on the platform and navigate to the Datasets view.
Click Upload file and upload the csv file with your Slack messages.
Select the message feature, click the wrench and make sure that:
Encoding is Text
Select the channel feature and set Encoding to Categorical.
Click Create an experiment and the Experiment wizard will pop up.
Use the Experiment wizard
In the Experiment wizard make sure your Slack dataset is selected.
Set the Input feature to message and the Target feature to channel.
Choose the Single-label text classification.
Click Create and the prepopulated BERT model will appear in the Modeling canvas.
Click the Settings tab and check that:
Batch size is 64 or larger depending on your sequence length. Read more in our BERT cheat sheet
Epochs is 2.
Learning rate is 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
Go to your Slack app page.
Select your existing app.
Create a bot user for your app
Open Add features and functionality.
Click Add a Bot User and then Add Bot User.
Get the Bot User OAuth Access Token
Open OAuth & Permissions in the left-side menu.
Click Reinstall App.
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.
Navigate to the
slack-besserwisserrepo in the Terminal.
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".
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:
In the slack-besserwisser repo, open the file slack-besserwisser/listen_and_reply.py.
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
Go to console.cloud.google.com.
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.
Type Container Registry in the search bar, then select the Container Registry API.
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
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
To set up the gcloud sdk, run:
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
Make sure you are in the directory slack-besserwisser.
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.
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
Go back to console.cloud.google.com.
Type compute engine in the search bar and select it.
Press Create Instance and select a name for the instance.
For machine type select f1-micro. You could choose a larger machine if you want your Slack bot to be faster.
Tick the checkbox: Deploy a container image to this VM instance.
For the container image, paste the name of the image you pushed (for example: eu.gcr.io/YOUR_PROJECT_ID/besserwisser:1.0.0)
Press Advanced container options and scroll down to Environment Variables.
Enter the same values as you did in slack-besserwisser/env/bot.env.
Scroll all the way to the bottom and press Create.
The image for your Slack bot is now in Google Cloud.