How To Build Your Own Slack Bot š¤
How to set up a Slack Bot for instant, automatic notifications.
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
- A free Slack workspaceĀ š©āš»
- Something to monitor! š
š Instructions
Step 1: Setting up the bot
- Go toĀ api.slack.com, log into your workspace and click onĀ Create an app
- Click onĀ From scratchĀ and then give it a name and select your workspace. Iāll call mineĀ ProcessMonitor.
- Click on theĀ BotsĀ box under theĀ Add features and functionality header
- Click onĀ Review scopes to add
- 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.
- 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.
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.