skip to Main Content

Bots Have Feelings: Making a Sentiment Analysis Slackbot

Sentiment Analysis

I’m lazy. I love when computers jump in to do things I fail at. When I mention that I’m emailing a picture but don’t include an attachment? Gmail reminds me. When I’m going somewhere with smartphone navigation and a faster route opens up? Maps just guide me to the right place. Summarize a billion financial records for important people in suits. Well…actually, I’m glad there are computers because I don’t think a human being could even do that.

But my laziness isn’t satisfied. I still have to do things like program computers, physically move from place to place, and – most exhausting of all – interact with other people. *shudder* Imagine how much brainpower you waste evaluating what other people say in chat windows! We, humans, bundle tons of emotion into the words we produce, instead of simply stating exact, precise, factual statements. It takes time and effort to understand that emotional content.

Think about it. When someone puts this in a chat window:

It’s so cold today! I can’t believe I’m expected to shovel snow off my car and come to the office for this dumb meeting!

They’re stating facts and emotions all mixed up together! Wouldn’t it be more efficient to separate the facts from the emotion:

The temperature today is significantly lower than average for this climate and time of year. Frozen precipitation has accumulated around my vehicle. I must remove it to enable road passage to the office. These factors together affect my emotional state, making me less happy.

Piece of cake! Wasn’t that so much easier? If you want the facts, just ignore the last sentence. If you want to understand how someone feels, just read the last sentence. Just think how much time you could save by deferring emotional processing!

OK, OK. I get it. It wouldn’t actually be that useful. But it’s sure as heck fun to make. Check this out:


Did you notice those little faces that appeared below the comments I made in the chat? I made a bot – I call it the “Emojifier” – that automatically reads the content of what someone types and responds with a little emoji icon indicating how positive or negative their message was. Smiling angel for most positive, all the way down to frowning angry guy for most negative. Read on to find out how this works, and how to set it up.

How Did It Know?

Marketing and sales folks live and die by product perception. If you have ever mentioned a product/TV show/movie/band in a public Facebook/Twitter/Instagram post, you can rest assured that somewhere in a dark, cool server room a computer crunched that text and made a guess at whether you love or hate that product. If you’ve ever left a product review on Amazon, rest assured that a machine processed it. Same deal if you comment on a news article.

Sentiment analysis is a branch of machine learning that allows computers to identify subjective human emotions out of raw information. In their simplest form, sentiment analysis algorithms just count happy or sad words. To get really fancy, you start by using natural language processing techniques to identify subject/object/entities in sentences, then pair that with machine learning that has operated against brain-shatteringly huge collections of texts.

If you think about it, it would be super hard to declaratively program a simple algorithm for saying whether a statement was positive or negative. No developer would want to (or could) spend all their time fine-tuning edge cases of weird sentences like “I don’t hate when you don’t pick up the dishes” (multiple negation words), or “I just love when nothing goes my way!” (is this sarcasm?) Tough!

So – being lazy – I didn’t program this one myself. I just used Google’s cloud natural language processing tool, which includes a sentiment analyzer. Since it comes with a nice REST API, it’s pretty easy to plug into other stuff.

How I Did It

Here’s the flow:

  1. Slack sees a message typed into a chat.
  2. Slack sends the content of the message to an API endpoint.
  3. API endpoint performs sentiment analysis.
  4. API endpoint replies with emoji to Slack messages as a bot user.
  5. ??
  6. Profit!

5 and 6 are left as exercises for the reader.

Make a Slack App

Start here to create a Slack app. Make sure you pick your team:

Sentiment Analysis

Once you click “Create App”, you’ll be able to set up features for your app. Here are the ones I set up for this example (note that you don’t need “Slash Commands” – that was me trying something else):

Sentiment Analysis

When you click “Event Subscriptions”, enable the events and subscribe to the messages.channels event. Make note of the “Request URL” – we’ll show how to get that set up later.

Sentiment Analysis

Next set up the “Bots” item. Pretty simple:

Back on the main Slack app screen for your app, you’ll need to install your app to your team. This process will generate some tokens you’ll need in the “Permissions” section.

Next set up the “Permissions”. The top part of the permissions settings includes access tokens – so I won’t screengrab it. Just know that you’ll need those tokens later, so do whatever it is you do to securely store tokens. You will need to add two permission scopes to the “Scopes” section at the bottom:

Make a Web Service

The “Request URL” setting for the “Event Subscriptions” setup needs a web service to call. Let’s make one!

I have a couple favorite ways to spin up fast web services:

  • Use ngrok to create a tunnel to your local machine.
  • Use glitch to create a fast, live-editable Node.js web app.
  • Use Google App Engine to spin up a managed service.

Any of these would suffice for a Slack bot. I’ll show you an example in App Engine with Python as my language of choice.

First, follow the quick Hello, World App Engine tutorial to get your instance up and running. Use this code as the main request handler of your project, and deploy the code to the cloud. Get the URL of your App Engine app and put it in the “Request URL” field mentioned above.

Rather than print out all the code in this blog and provide commentary, I’ve sprinkled helpful bits into the code file itself. The only other thing to do to prepare the code to work is to sign up for Google Cloud Natural Language Processing API and get an access key. The code sample will direct you where to put the access key.

After setting up the Slack app and the web service you’re good to go. You should be able to start typing weird things into the public channels on your team and watch as people get a machine-learned rating on what they say!



If you have an interest in viewing similar content, visit our blog, here

View our LinkedIn, here

Paul Modderman loves creating things and sharing them. He has spoken at SAP TechEd, multiple ASUG regional events, ASUG Fall Focus, Google DevFest MN, Google ISV Days, and several webinars and SAP community gatherings. Paul's writing has been featured in SAP Professional Journal, on the SAPinsider blog, and the popular Mindset blog. He believes clear communication is just as important as code, but also has serious developer chops. His tech career has spanned web applications with technologies like .NET, Java, Python, and React to SAP soutions in ABAP, OData and SAPUI5. His work integrating Google, Fiori, and Android was featured at SAP SAPPHIRE. Paul was principal technical architect on Mindset's certified solutions CloudSimple and Analytics for BW. He's an SAP Developer Hero, honored in 2017. Paul is the author of two books: Mindset Perspectives: SAP Development Tips, Tricks, and Projects, and SAPUI5 and SAP Fiori: The Psychology of UX Design. His passion for innovative application architecture and tech evangelism shines through in everything he does.

Back To Top