To post to Slack API with Python you will need to create a Slack API app and get the Webhook URL. In this step-by-step guide, we will see how to get the webhook necessary to use the Slack API.
- Create Slack App
- Add the Incoming Webhooks
- Activate the Webhooks
- Add the Webhooks to the workspace
- Give access to your Workspace
- Add the Webhook URL into a credentials files
Create Your Slack App
Go to Slack App and click on “Create App”
Add the Incoming Webhooks
Webhooks are the simplest way to post messages from Python to Slack.
In your app features, go to Incoming Webhooks.
Activate the Webhook
Add Your Webhook to Your Slack Workspace
Now it is time to add the Webhook to your Slack Workspace.
Some organisations will go through a validation process, but you could use your personal Slack profile to make the process easier if your organisation isn’t responding to your request.
Click on “Add New Webhook to Workspace”.
Allow The Access to the Workspace
Now it is time to approve the access to your Slack Webhook.
Here, I decided to send the alerts to myself.
Copy the Webhook URL
The next step is to copy the Slack Webhook URL into a credentials.json
file.
It is good practice not to add the credentials into your code. By adding it to a JSON file, it allows you to add that file to Gitignore, so it is never committed to Github.
The structure of the credentials files goes like this:
{
"slack_webhook":"https://hooks.slack.com/services/XXXXXXXX"
}
This is it. We have seen how to get the Webhook URL to the Slack API. In the next posts, we will see how to send a message to the Slack API using Python and make an automated robots.txt checker.
SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. Writer in Python, Information Retrieval, SEO and machine learning. Guest author at SearchEngineJournal, SearchEngineLand and OnCrawl.