Connect slack with python
Using slack & python to track processing, failure & success of code using slack notification
If you want to track your python script progress using slack then this article is what you are looking for, connecting slack with python is very simple yet very effective process.
To connect a python bot with slack, you simply need to follow these two steps:
- Setup python webhook over slack interface
- Add slack notification code in your python project
Setup python webhook over slack interface
- On slack interface login to workspace where you want to create python bot
2. Click on Browse App Directory to get list of all available apps which can be integrated with slack
3. Click on Build option available just left to your workspace name
4. Click on Create a custom app
5. A dialog box for adding app information appears, where you can add your <app_name> and can select the workspace, after adding <app_name> and selecting workspace click on Create App button
6. Next from list appears below, select Incoming Webhook option
7. Just right to Activate incoming Webhooks, turn on webhook
8. Below there appears an option for adding new webhook to workspace, click on Add New Webhook to Workspace
9. Now you need to select channel where you python app will post any message/notification, you can select any channel from available list as shown in figure below
10. Once you select a channel for your app to post, it provides with you a webhook key (NOTE : Please don’t share this key with anyone)
Copy webhook key for later use
Now bot creation task is completed, all you need to do now is setup slack notification code and then add code to your project as per your use case. For this you can open your favourite IDE and add this small code there -
For code you can directly jump to bottom of this article, here are three different ways in which we can access webhook in our project
Ways to use webhook
Webhook which we saved earlier requires in our project, to add webhook their are multiple ways(three discussed here)
A. Simply declaring a variable in code as shown below
webhook = (
"https://hooks.slack.com/services/00000000000/aaaaaaa/abcdefghijklmnopqrst" )
Above option seems easiest but is never considered a good choice as it publicly available your webhook credentials
B. Adding webhook as environment variable and accessing webhook from there
You can add webhook as environment variable by any name using which you want to access webhook, to add webhook as environement variable
On IDE’s terminal, write —
export <webhook_name> = <webhook_key>##for example :
export webhook_slack = "https://hooks.slack.com/services/00000000000/aaaaaaa/abcdefghijklmnopqrst"
and then you can access webhook in your code using —
import os
webhook = os.environ.get("webhook_slack")
C. Configuration file
Another way is to create a configuration file for all different types of credentials used in your project and accessing webhook from there (make sure not to push this file to production, you only required to push a sample configuration file to production which consists to file format)
#config.pycredentials = {}
credentials["webhook_slack"] = "https://hooks.slack.com/services/00000000000/aaaaaaa/abcdefghijklmnopqrst"#maincode.py import configwebhook_slack = config.credentials["webhook_slack"]
Function to add notification code to your project
Below here is sample code for adding slack notifiaction to your project
import osdef notify_slack(message):
#message -> string message which you want to pass ass notification
webhook = os.environ.get("webhook_slack")
data = {"text": message}
try:
requests.post(webhook, json.dumps(data))
except:
print("[ERROR] Not able to execute slack notification.")
You can add this function to your project, and can call wherever you need to push any notification on your slack channel. For example, on completion of any run/task you need to add a notification in your slack channel, you can call this function just after completion of your task.
Hope you like this article, in case of any doubt/suggestion/correction please feel free to connect with me linkedIn