Photo by Stephen Phillips — Hostreviews.co.uk on Unsplash

How To Build You Own Slack Bot 🤖

🥞 Full Stack Data Scientist: Part 7— How to set up a Slack Bot for instant, automatic notifications

Daniel Sharp
5 min readJan 17, 2022

--

Dashboards are the most popular way of presenting data in businesses — but they’re not the only way!

There are times when users need to be immediately notified when something happens. For example…

💧 A maintenance team should be notified immediately if a pipe has burst

🏷 An analyst should be notified if the price of a product changes online

🔧 An engineer should be notified if a process has ended successfully.

This is where building a Slack Bot is extremely useful. You can quickly set one up to write messages into a Slack channel as soon as something happens.

In this tutorial I will show you how to build a Slack Bot that notifies you when certain process starts and ends, and whether it was successful or not. It will also be capable of sending matplotlib charts to the conversation.

You can find the repo for this project here.

Let’s go!

✅ Requirements

📓 Instructions

Step 1: Setting up the bot

  1. Go to api.slack.com, log into your workspace and click on Create an app
  2. Click on From scratch and then give it a name and select your workspace. I’ll call mine ProcessMonitor.
  3. Click on the Bots box under the Add features and functionality header
  4. Click on Review scopes to add
  5. Scroll down to the Bot token scopes header and add chat:write, chat:write.customize, channels:read and files:write. These are the permissions the bot needs to write messages and send files into the Slack channels.
  6. Finally, scroll all the way up and click on Install to workspace, and Allow on the following screen. This should now show a screen like this, with the Bot User OAuth Token visible. Take note of this token, since it’s the one we will be using to send messages.
Make note of the Bot User OAuth Token!

The very last step is to create the Slack channel where you want the Bot to post, and add it to it. You can do that by running the following command inside the channel: /invite @<botname>

For example, In my case I used /invite @ProcessMonitor.

Step 2: Set up the processes to monitor

For this example, our Bot will be notifying us about some dummy processes running on my computer. I’ve created a simple function to run these dummy processes with a probability of failure, just to show how the different messages can be sent to Slack.

process_runner.py

The file above uses functions from a helper script called slack, which contains the logic for sending the messages, as well as defining the message layout. Slack messages are structured through their ‘Block Kit’. They have a good guide on how to use blocks here.

The slack.py file looks as follows. It contains two base functions (post_message_to_slack and post_file_to_slack) which send POST requests to the Slack endpoint with the Bot token and channel. For these to work you’ll have to set SLACK_APP_TOKEN, to the Bot User OAuth Token value, and SLACK_APP_CHANNEL, to the channel name (remember to include the #), as environment variables.

slack.py

Step 3: Demo! 🚀

We’re now ready to test the Slack Bot! I will run two tasks to test my ProcessMonitor Bot. Remember all my tasks are fake, but this is analogous to working with real tasks and data.

Copy files task

I decided this task takes 4 seconds, and, as you can see from the Slack messages, it completes successfully!

A successful task, Copying files in 4 seconds

Run simulation task

This task takes 8 seconds, which due to how our process running code is defined, increases its likelihood of failure. In this example, the task failed and we get a matching message on Slack.

This task fails after just two seconds.

Post a Matplotlib graph

Finally, there’s a function is the Slack code to post a matplotlib graph into the Slack channel. For sake of simplicity I’ve just taken this example from their website and used the post_matplotlib_to_slack function in the slack.py file to have the Bot post it into the channel.

Send a Matplotlib plot into Slack!

In this blog post we covered different use cases for a Slack Bot and how it might be more useful than dashboards for specific scenarios.

We implemented a Slack Bot to report on running processes and notify a channel about whether they completed successfully or not.

Hopefully this will come useful for your work or your personal projects. Let me know what you used it for in the comments! 💪

In Part 2 of this blog, we will discuss how to add interactivity, so a user can send specific requests to the Bot and get the response in real time.

Applied Data Science Partners is a London based consultancy that implements end-to-end data science solutions for businesses, delivering measurable value. If you’re looking to do more with your data, please get in touch via our website. Follow us on LinkedIn for more AI and data science stories!

--

--