Most recent posts...

Complexity – My Friend, My Enemy
Over my years of network engineering, I've learned that the fewer features you can implement while still achieving a business goal, the better. Why? Fewer
Brief Me At Interop?
Want to brief me about your product or otherwise have a chat? Send an e-mail to [email protected] while there's still room on my calendar. I
slack-python-flask

Connecting Python To Slack For Testing And Development

The scripting language Python can retrieve information from or publish information into the messaging app Slack. This means you can write a program that puts info into Slack for you, or accepts your queries using Slack as the interface. This is useful if you spend a lot of time in Slack, as I do.

The hard work of integrating Slack and Python has been done already. Slack offers an API, and there are at least two open source Python libraries that make leveraging these APIs in your Python code a simple task. I chose slacker after a bit of googling, but it’s not a preference borne of experience. The community seems to be behind slacker as opposed to Slack’s own python-slackclient, so I went that direction.

Steps

  1. I’ll assume you’ve got Python installed already. My environment is Ubuntu Server 16.04 with Python 2.7.12.
  2. Install the python package manger pip, if you don’t already have it.
    sudo apt install python-pip
  3. Install the slacker python library.
    pip install slacker
  4. Generate a testing and dev token at the Slack API web site.
    https://api.slack.com/web
    Slack_Web_API___Slack
  5. The token will be everything required for authentication to your Slack group. Protect it like a password.

Armed with the token and slacker library, your Python installation is now Slack-capable.

Example

I took this code right from the slacker github page to make sure things were working without having to read any documentation. I created a channel called #exp to run my test in.

from slacker import Slacker

# Replace abcd-etc. with your testing and dev token
slack = Slacker('abcd-*****-*****-*****-*****')

# Send a message to #exp channel
slack.chat.post_message('#exp', 'Python was here.')

I ran the test using python slack-test.py.

The result looked as follows.

slacker-test