Slack Python client to send messages

slack_rgbWhat is Slack ?

Slack is a boosted instant messaging app, it's as simple and powerful as this. Indeed, it is an innovative way to let your teams communicate via channels you can subscribe. But that's not all. You have public and private channels, chat capabilities, files sharing, searching, notifications etc. There is a Slack client for each plateform Windows, Mac and Linux, as well as Android phone, iPhones and Windows phones.

But the truth is out there.... You can also fully customize Slack with a lot of apps (just see there "store" for that).

For example, you can add an app for Github or Bitbucket to be notified of every commits. There are also bots. Bots are little applications hosted somewhere (on Heroku to name one) you can interact with. For exemple, there is a bot to have the last quote of a stock ticker. Nice ! Last but not least, Slack provides an API so you can build your own bot, interact with people, send messages etc. In this post, I will briefely explore the API (only two or three methods of it) with the help of the official Python client. The documentation of the methods can be found here.

Setup

The first thing to do is to install the client for python

And you're done ! You can now enjoy the client by simply importing it in your script

Well, you're almost done. In order to be able to use the API, you need a token to authenticate. Slack provides you a test token you can generate for each team. This token will be used for development purposes, but you can also generate an OAuth for an official application. However, you should never share your test token nor use it in an app as mentionned in the doc

These tokens provide access to your private data and that of your team. Keep these tokens to yourself and do not share them with others. Tester tokens are not intended to replace OAuth 2.0 tokens

For more details on security concerns, see the safely storing credentials page. The approach used in this example is to export the token in an environment variable, for example in your .profile file

From here, it will be easy to retrieve your token in the script by

Now you're really done ! You can enjoy the python Slack client 🙂

List channels and retrieve informations

The list of available channels is simply retrieved with the channels.list method of the API. Slack returns back the results in a dictionary with two keys: ok and channels.

The details of the API call result can be found in the documentation

ok key tells us if the API call was successful, then channels contains the data we need on the list of channels. With this methods however, you will only see public channels. To access private channels, the methods is just slightly different: groups.list
Here a simple function that will do the job:

One of the most important data in the previous call result is the ID of a channel that can be used instead of a human readable channel name in further calls. For example,

In the documentation of a group, the return result has the following structure:

One interesting information about the private channel is for example the latest messages. These can be accessed via the latest key. Here is a proposed implementation:

Well that's great, we can now retrieve messages and print a channel informations. But let add some more fun..... After reading messages, send them !

Send messages

In the API documentation, we find a method to send messages : chat.postMessage , so a simple use with the Python client could be:

As already mentionned, the API method needs a channel id to send a message. Then to customize a little, we can add a username that will appear as the sender, and an icon for the avatar. And here is an example of use where we send a multiline message on the test private channel:

Have fun !

Leave a Reply

%d bloggers like this: