Recently Slack changed the way you publish messages via API, so it took me a while to figure out how to do it by myself and today I’ll show the short tutorial of what you need to do in order to be able to send messages to Slack with Python API.
All you need is just follow these steps:
-
Create a new Slack App
-
Create a Bot user
-
Add the Bot user to your App
-
Install the App on your Slack workspace and assign it to specific channel
-
Copy bot user token
-
And finally write this code:
import slack SLACK_API_TOKEN = "xoxb-XXXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX" client = slack.WebClient(token=SLACK_API_TOKEN) response = client.chat_postMessage( channel='#my-channel', text=msg) if(response["ok"]): print("Notification sent to Slack")
Notice that SLACK_API_TOKEN
is your bot user token.