Posting messages to Slack using incoming webhooks and Python3 Requests API

Slack has become a favorite of DevOps/SRE teams across the globe. It's a very easy-to-use and programmatic messaging platform. Here's the quick and easy process to posting messages to your Slack group from Python.

I wanted to make a short post on using Slack’s incoming webhooks feature to post messages to Slack using the human-friendly Requests API in Python3.

First things first. You’re going to need to set up incoming webhooks in your Slack account, which you can do by going here. An incoming webhook is simply a cryptic and unique URL which can take JSON-formatted POST requests and use that to post to your Slack account. This URL does not require you to authenticate, so please do not share it with anyone.

When you set it up, you will need to configure the default channel that the webhook will post to. For now, I’ll use the private @slackbot channel. You can change this dynamically in the JSON payload you will send to the webhook later on.

Setting up incoming webhooks in Slack for use with Python.
Enlarge — Setting up incoming webhooks in Slack for use with Python.

Once you set it up, you should get a webhook URL like this:

Your incoming webhooks URL after you set it up. Please keep this safe, and don\'t share it with anyone.
Enlarge — Your incoming webhooks URL after you set it up. Please keep this safe, and don\'t share it with anyone.

Keep this URL somewhere safe.

Once you have the above URL, you can write a simple Python3 script like so:

import requests
import json

if __name__ == '__main__':

    wekbook_url = '--your-webhook-url-goes-here--'

    data = {
        'text': 'I am putting myself to the fullest possible use, which is all I think that any conscious entity can ever hope to do.',
        'username': 'HAL',
        'icon_emoji': ':robot_face:'
    }

    response = requests.post(wekbook_url, data=json.dumps(
        data), headers={'Content-Type': 'application/json'})

    print('Response: ' + str(response.text))
    print('Response code: ' + str(response.status_code))

Place the above code in a .py file, put your webhook URL in webhook_url, and run the file using python3 <file.py>. You should see this:

Response: ok
Response code: 200

The above response means the message was successfully posted. Check your slackbot channel in Slack, under Direct Messages, and you should see this:

If you manage to set up Slack and Python successfully, you can finally know what AI think.
Enlarge — If you manage to set up Slack and Python successfully, you can finally know what AI think.

And it’s really as simple as that: get your webhook URL and begin POSTing data in JSON format to that URL. You can find more configuration options here, including error codes.

Source code for the script can be found here.

← previous post
Using Packer and Ansible to create immutable servers, deploying code, and recycling instances

next post →
I'm killing Disqus comments on my blog. Here's why.

56TechnologyView source