ADSP Logo

How To Build Your Own Slack Bot šŸ¤–

How to set up a Slack Bot for instant, automatic notifications.

Slack bot

Full Stack Data Scientist: Part 7ā€” How to set up a Slack Bot for instant, automatic notifications

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.

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.

Permissions setting

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.

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.

Python Code for Slack Bot