294 Words. Plan about 2 minute(s) to read this.
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.
- I’ll assume you’ve got Python installed already. My environment is Ubuntu Server 16.04 with Python 2.7.12.
- Install the python package manger pip, if you don’t already have it.
sudo apt install python-pip
- Install the slacker python library.
pip install slacker
- Generate a testing and dev token at the Slack API web site.
- 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.
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.
about | subscribe | @ecbanks