Snake_Byte #35: Building Custom Slack Bots with Python - Mimic Your Coworkers

Here at PokitDok, we use slack not only as an internal communication tool, but also as a one stop shop for posting analytics reports and alerts using various integrations and bots. In some cases, we even build our own custom slack bots to handle more specific and complex tasks. In this post, I'll be walking you through how to set up a slack bot using the slackclient Python package from scratch.

Setting Up your Environment

Start by creating a virtualenv with slackclient installed

In order to connect to slack, we will need to generate an authentication token for our bot. You can create a new bot and get the authentication token for it here.

In addition, if you want your bot to be able to use the Slack API to query slack for messages, you will need to generate another token since, by default, bots are not allowed to do this. The most straightforward way is to generate a token for yourself, and allow the bot to use your token for querying purposes. Keep in mind that this will allow the bot to see and access everything you can see in slack, including private messages.

Real Time Activity Monitoring Using the slackclient Package

The slackclient package allows you to start seeing real time activities in slack with just a few lines of code.

Note that the bot will only be able to see real time activity in direct messages and/or channels that it is in.

Here we see a few examples of the types of activities available for building out a bot, most notably:

message
reaction_added
user_typing
presence_change

You might notice that channels and users are returned as id values that are not that useful to the naked eye. However, we can use our slackclient instance to easily call any of Slacks API methods, including channels.info and users.info.

Tying that back in with what we had earlier, we can see more easily who is posting messages, and which channel they are being posted in.

Using the above,

{u'text': u'Hello World', u'ts': u'1502350329.278088', u'user': u'U02DYEE7X', u'reply_to': 0, u'type': u'message', u'channel': u'C3A47KC1H'}

becomes

{u'text': u'Hello World', u'ts': u'1502350329.278088', u'user': u'alec_macrae', u'reply_to': 0, u'type': u'message', u'channel': '#random'}

Querying Slack Messages

Earlier I mentioned how you would need a secondary slack token to access the ability to query slack for historical messages. Again, the slackclient package makes this a simple task. Just specify the user and the channel you for which you want to query messages.

This query uses the search.messages API, which allows for additional parameters to be set as well. For example, you can specify how to sort the responses, how many items to return per page, and the page number of results to return.

Wrapping up - A Slack Bot to Mimic your Coworkers?

In an earlier Snake_Byte, I wrote about how you can use the markovify python package to use Markov models to easily mimic the tone and style of a corpus of text. Well, by using a combination of these two libraries (slackclient and markovify), it is actually quite simple to build a slack bot that can mimic your colleagues' slack writing tendencies. I'll leave that as an exercise to the reader. However, if you can't be troubled with putting the pieces together yourself or just want to get a jump start on mimicking your own team's slack habits, feel free to fork this starter repo that I've set up. Although mocking your coworkers with a chat bot can be fun, there are obviously many other more work-relevant and useful ways to use the slackclient python package. Hopefully, this post has highlighted just how easy and powerful using Python to integrate custom bots into slack can be.

About Alec Macrae

Alec is a member of the data science team at PokitDok, based out of the company's San Mateo office. He enjoys using data, machine learning, and AI to solve problems, reveal insights, and build cool software.

View All Posts

Leave a Reply

Your email address will not be published.