Algorithmia Blog - Deploying AI at scale

Build a Sentiment Analysis Slack Chatbot in Python

This is a guest post by Chris Hannam, a professional Python and Java developer. Want to contribute your own how-to post? Let us know here.

As companies adopt chat tools like Slack to manage internal communication, they’re learning that a lot can get lost when communicating over text.

With this in mind, I built a Slack chatbot to keep an eye on messages, and flag negative ones to give the commenter a little nudge to be nicer in the future.

To achieve this, we’re going to use Slack’s API to analyze messages in real time. We’ll also be able to ask our Slack chatbot for overall sentiment of the channel from the previous 24-hours of messages.

Please note that this post only covers the how to setup a Slack chatbot to check the sentiment of messages. It’s not about building conversational chatbots.

Ready? Let’s go.

Build Your Slack Chatbot

Slack already has huge number of bots available, but I wanted something simple and lightweight to monitor a Slack channel.

We’ll be using the Slack Real-Time Messaging API (RTM) to communicate with our channel. It’ll send us messages as they happen, which allows us to spot negative comments in real-time. We can then nudge users that send negative messages to reconsider their word choice in the future. We’ll also use this to request the current mood of a Slack channel.

The Slack rtmbot works using plugins. Their code will connect you to a Slack channel, and pass all messages to your bot. In the root of the code is a plugins directory. Any Python files added to this directory will be run when messages are sent in your Slack account.

Step 1: Setup Your Slack Bot

The first thing we need to do is visit the Custom Integrations dashboard for your Slack team to get your API keys. The URL will look like this:https://YOURSLACKCHANNEL.slack.com/apps/build/custom-integration

Select Bots, and have a look under “Integration Settings” for your API key. More information can be found in Slack’s Bot Users guide.

I’ve also added the outputs array we’ll use in the next step. The outputs array is a global variable for sending messages. The first item in the array is the channel ID, and the second is the message. It’ll be used like this:

outputs = []
outputs.append(["C12345667", "hello world"])

Now we need to write two functions. The first to process incoming messages, and the second to get the current mood of the channel.

Step 3: Process Incoming Slack Messages

To process the incoming messages from Slack, we grab the text of the response. The Social Sentiment Analysis algorithm requires an object with the sentence as a string.

We then pass the message to Algorithmia to analyze. We’re only going to use the compound result, which is how positive or negative the sentiment of the sentence is on a scale of -1 (very negative) to 1 (very positive).

If the compound sentiment is -.75 or less, then we send a message to the Slack channel it originated in, and let the user know that the previous comment was pretty negative.

That’s the basic step for sending messages to Slack in real-time. In order to ask our bot for current overall sentiment of the channel, we need to add some filtering, and math to average the sentiment over time.

Step 4: Track and Filter Slack Messages

The only slight gotcha is that you get all the messages for a channel. This includes people joining the channel, leaving, etc. Let’s filter this noise out, and UTF-8 encode the message while we’re at it.

Putting It All Together

Once your bot is up and running, go to your Slack team, and invite it to a channel. Every message – except for channel joins – will now flow through your plugin, and then the Social Sentiment Analysis algorithm.

Try it out by typing a few nice things. Then check your terminal:Comment "i think you’re great" was positive, compound result 0.6249

With a little more work, your Slack chatbot could be left in charge of looking after an entire Slack team, keeping the tone from ever slipping into negative territory. This approach could be extended to monitor social media, like Twitter or Instagram comments. For instance, you could use the Retrieve Tweets With Keyword microservice to check the sentiment of a topic, follower, or even your own tweets, like this NLP approach to analyzing profanity of tweets.