Creating a sentiment bot in Slack with Node.js and Symanto’s Text Analytics API

sentiment-bot-in-Slack

In this post, we’ll take a look at how you can utilize Symanto’s sentiment model to create a Slack bot with Node.js.

By the end of this tutorial, you’ll know how to setup a Slack bot and analyze the user’s writing style with Symanto’s Psycholinguistic Text Analytics API to understand different aspects of their messages: The language of the message, general sentiment, emotions the user has expressed, and even how users talk. For example, whether they share personal feelings or express objective and factual information.

Our final application will look like this:

The setup

First, we’ll need to create a new Slack app. Let’s go to the Slack API portal and click “Create an App”. Give your new app a name; we’ll call this one “FirstTestApp” for now. Then, select your Slack Workspace where you want to create your new app.

Next, click “OAuth & Permissions” from the left menu and scroll down to “Scopes” to add the following OAuth Scopes for Bot Token Scopes:

  • chat:write (to allow the bot to send messages)
  • channels:history (to allow the bot to view messages on public channels that the bot has been added to)
  • users:read (to read the users’ info to access their usernames)

Once that’s done, scroll back up and click the button “Install App to Workspace” and allow the app to request permissions to access your workspace. You’ll receive a “Bot User OAuth Access Token”. Copy this token and save it for later.

Next, navigate to “Basic Information” and save the “Signing Secret Key” too.

Create your node.js project

First, create a new folder named “FirstTestApp”. In this folder, create a new file named “.env” and copy&paste the following settings:

Next, add your Signing Secret Key to SLACK_SIGNING_SECRET and your Bot User OAuth Access Token to SLACK_ACCESS_TOKEN. Finally, get your free API key by signing up to Symanto’s developer portal and paste it to API_KEY.

Download node.js if you haven’t yet, open the node.js command tool and navigate to the root of your development folder. Type “npm init” and answer the questions to create a package.json file. Then, open package.json and add “start” : “node index.js” to the scripts section.

The next step is to install the necessary project dependencies using npm install:

  • Axios (Promise based HTTP client for the browser and node.js)
  • Express (A minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications)
  • Body-parser (Parse incoming request bodies in a middleware before your handlers)
  • Dotenv (Dotenv is a zero-dependency module that loads environment variables from a .env file)

Your package.json should look something like this:

Then, create a new file “index.js” in your root directory. Copy the following code into your file:

Let’s start our server by typing npm start.

Create request URL

Now that we started our server, let’s create the request URL which Slack uses to send HTTP POST requests when events occur.

First, navigate to “Event Subscriptions” on the Slack API portal and enable events.

If you haven’t already, download and install ngrok to create the request URL. Ngrok creates a secure URL to your localhost server. Please note that this URL is only valid for a short period of time and should be used only for testing purposes. Open the ngrok terminal and type “ngrok.exe http 80” to expose port 80. It should look something like this:

Copy either the http or the https forwarding link and paste it into the “Request URL” field followed by /events. It should be verified now. Next, scroll down and click “Add Bot User Event” to add message.channels.

Great! We have our server running and connected to Slack. Let’s connect the bot to the API.

Call Symanto’s Text Analytics API

In this part, I’ll show you how you can call the sentiment endpoint of the text analytics API to determine the users’ sentiment. If you want to find out more about all the different endpoints Symanto’s Text Analytics API provide, visit Symanto’s developer portal and API documentation.

Let’s get started. We use the axios library to make a post request. Here is the sample code of the analyzeTextByDLApi function which gets a text and calls the sentiment endpoint.

Then, we can call this function in the event call back, this event is raised when a user writes something in a channel.

As you can see in the code, after calling Symanto’s sentiment detection endpoint, we call the Slack API to post the results in the slack channel (chat.postmessage).

Now, you can run your node.js app and add the bot to one of your channels. Let’s test it out: Write something in the channel and see how it works.

Congratulations, you did a great job! You’ve just built your first Slack bot using Symanto’s Text Analytics API.

View on Github

Wrap up

So that’s it! That’s the simplest path from nothing, to a working Slack app applying Symanto’s Psycholinguistic Text Analytics API. You have learned how to create a bot in Slack and how to call the sentiment endpoint.

I recommend you take a look at the language detection endpoint to automatically detect the language of a user’s message before sending it to one of the other endpoints. Also, play around with the emotion or personality detection to build more advanced capabilities. Happy coding!