In this article I want to deviate from my normal cybersecurity related posts while still staying technical and focus on the python automation topic of using Slack to send notifications from your python scripts.
There are many useful reasons why you’d want to send slack messages to a specific person or a team during the execution of your automation scripts. Anything from error notifications, condition notifications or even just completion notifications. I find these kinds of notification in my automation scripts extremely useful and figured others might as well. While the Slack API guide is very detail and helpful it can be a bit daunting to those of us that aren’t full fledged developers looking to write a major Slack Bot with tons of functionality. When I set out, I certainly felt that way as I am by now means a professional developer, I just put together some python scripts from time to time to automate something and I was just looking for something super simple. All I wanted was to be able to send a message to a specific person or a channel.
After some time I figured out a way to do this and wrote a little function to accomplish this. This function is just 40 lines, and you are welcome to use it in your code if you want. I have a script that just tests the function on GitHub at https://github.com/siggib007/Utils/blob/master/SlackNotify.py. Just copy the
SendNotification function from that script and use it in your python scripts.
You need three things to call that function:
- The message you want to send. In the script I truncate long messages so not to run into message length errors on Slack. Plus I found that messages longer than 1000 char on slack aren’t all that useful. I default this truncation to 199 characters, but you can adjust that. I would recommend staying below 500 characters. This shouldn’t be a logging mechanism, more notification that someone should check the logs.
- The channel ID, which identifies who should be getting the message, either a person or a channel. I’ll show you how to find this shortly.
- The Authentication token, which authorizes your script into the right slack workspace. I’ll explain this better later.
In my test script this is how I call the function with “More Testing” being the message I want to send.:
BTW you’ll see in the script that I am fetching the channel ID and notification token from an environment variable. I’m actually storing this in a Doppler project which allows me to store this and access it in a very secure manner. Naturally only the access token needs to be stored securely, however I choose to store the notification ID as well for this demo to make it easy to switch test cases. For more details on how this works check out my Secrets Management article.
The Channel ID
OK here is how you find the right channel number to send.
A specific user
Let’s start with how to find the channel user for a specific person. There is more than one way to approach this, and this is just my favorite way and assumes you are already chatting with this person in slack. The channel ID for each user is in users’ profile in the more menu, if you already know how to get there, just do that, otherwise follow along here.
Start by finding the person in your direct messages list and click on their name in the list as if you were going to manually send them a message. Then click on their name in the message.
Then click on View Full Profile
In the profile side bar, click on the “more …” button.
Click “copy member ID” in the menu that comes up
Now you have the channel ID for this person in your clipboard, paste it someplace safe.
A specific Channel
Now for the ID of a specific channel. Start by finding the channel you want to send messages to in your channel list and click on it as if you were going to manually post something in that channel. Then click on the little down arrow (or chevron) next to the channel name the top of the screen.
The channel ID is at the bottom of the screen that comes up. There is a little copy icon next to it that will put the channel ID in your clipboard
The Authorization Token
OK now you two thirds of what you need to call the function. The authorization token is the only thing left and this one is a little more complicated.
You start by going to https://api.slack.com/ and click on Your Apps in top right corner
Then sign into the slack workspace where you want to send your messages to. If you have multiple workspaces you want to send automated messages to, you’ll need separate auth tokens for each workspace (also the channel ID is workspace specific), so you’ll just repeat this process for each of them. If you are already logged in for whatever reason you will not see this, so just jump to the next step, and create new app.
Once you are logged in, you should see a “create new app” button.
- Click Create New App
- Then click “from scratch”
- Give the Bot a cool name, I recommend something short that makes it clear what this bot does.
- Select the workspace this bot will go into. If the workspace you want isn’t in your list, click on the “sign into a different workspace” link
- Click “Create App”
Now on the left under Features, click on “OAuth & Permissions”
Scroll down the page to the Scopes section and click on “Add an OAuth Scope” under Bot Token Scopes
Add “chat:write” scope.
Now scroll back up and click on the “Install to Workspace” and approve the request screen that comes up.
Now your authorization token will be shown where the install button once was.
Now you have everything you need to send slack messages from your script, but there is one final task left. You need to add the bot to all the channels you want it to be able to send to. While there are at least couple of different approaches I’m just going to illustrate the one I recommend, which involves adding the bot directly into the channel one by one. Start by going back to the screen where you got the Channel ID and click on integrations
Click on add apps
Find the name of your new bot and click Add
You should be all set now. Please feel free to reach out if you have any questions.