Send Message to Slack with Python

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:

  1. Create a new Slack App
  2. Create a Bot user
  3. Add the Bot user to your App
  4. Install the App on your Slack workspace and assign it to specific channel
  5. Copy bot user token
  6. 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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.