Discover Positivity Score with Nexmo and React

Discover Your Twitter’s Positivity Score with React

Published July 01, 2019 by Lauren Lee

In 2019, I challenged myself to spread positivity and kindness in my day-to-day life.

But I quickly realized that that goal felt a bit vague and rather nebulous. While it had nice intentions, I found it difficult to judge its success or impact.

So for a few months, I pondered on how I might measure my goal and was lucky enough to discover that Microsoft Azure’s Text Analytics API offers a way for me to track my positivity pledge!

Azure’s team has open sourced their machine learning algorithms, meaning that I can analyze any text’s sentiment score on a numeric scale from 0 to 1 (0 being negative and 1 being positive).

This means that I can score my language and see if I truly have been spreading kindness and positivity, especially when it comes to my social media posts!

So I thought I’d create a React app that uses the Twitter API to pull down my most recent tweet and analyze its sentiment to then use Nexmo’s Messages API to send myself a report!

Check it out:


To work through this tutorial, you will need a Nexmo account. You can sign up now for free if you don’t already have an account.

In addition, you will also need:
* a Twitter Developers account
* a Rapid API account

How to Discover Your Twitter’s Positivity Score with React

You are going to accomplish the following tasks to create your app:

  1. Create a backend server with Express
  2. Create a frontend app with React
  3. Create a React component to receive a Twitter username
  4. Retrieve the most recent tweet with the Twitter API
  5. Calculate sentiment score with the Microsoft Azure API
  6. Send that report to your phone with Nexmo’s Messages API

This tutorial will take you through the process from scratch. Though if you’d like to see the finished code, you can clone the git repository for this project.

Create a Backend Server with Express

To begin, in this project’s root directory run:

This creates a package.json file, allowing you to get started installing just a few dependencies.
To do that run:

So that you’ll be able to run your server and your client using a single command npm run dev, open up your package.json file and change the scripts to look like this:

Next, create three more files in the base of your directory:.env, server.js and private.key:

Fill in your .env file with credentials for Nexmo and Twitter. It should look like this:

To fill in the API key and secret, visit Nexmo’s Getting Started page of your Dashboard. And you can get the Application ID and private key from the Create an Application page.
When creating the app, you are given the option to link the app to your Nexmo phone number. Be sure to copy and paste that phone number and put it in your .env file as the NEXMO_NUMBER.

Be sure to also generate a public/private key pair. I’ve directly pasted the key into the private.key file.

To fill in the consumer and access keys and secrets, create a Twitter app and generate the keys and tokens.

Now in your server.js file, let’s initialize our Express app and attach a few requirements:

Also add in the different credentials you’ll need to utilize the Nexmo and Twitter APIs:

At the bottom of this server.js file, add the two signatures of the endpoints we will return to in a bit to write:

Create a Frontend App with React

Next, still within the root of your directory, create your React app by running:

I’ve named the app client but feel free to name it whatever you like.
Once the React app has been created, move into it and start it up to ensure that everything worked correctly in your installation process:

So that you can run both ports simultaneously, in the client/package.json file add:

Running the create-react-app provides a bunch of boilerplate. Within the src/ folder there is an App.js file. In that file I added a call to a <TwitterName /> component which we will build out next.
My App.js file looks like this:

Next let’s create a components folder within src/:

Within that folder create three files:

Create a Component to Receive a Twitter Username

Inside of twitterName.js along with my boilerplate, let’s create two functions senduserName() and getTweet():

Into the render() add an <input /> to receive and store the Twitter user name as state from the user.

The senduserName() is called once a user has entered their username and hit the find most recent tweet button. That function sends information to the server.js backend and looks like this:

That function then calls the getTweet() function which looks like this:

This function receives the data from the server and then displays the most recent tweet on our React page.

The only thing left to do on this page is now call the next component, <SentimentCalculate /> and so at the bottom of the render() add that and pass the state (tweetContent and userName) to that component:

Retrieve Most Recent Tweet with the Twitter API

Let’s go back to the server.js file in the root of our project and fill in the'/userName'); function:

Here, the userName is passed in from the client and then sent to the Twitter API. The most recent tweet is then sent back to the client to be displayed in our React code.

Calculate Sentiment Score with the Microsoft Azure API

Let’s build out our sentimentCalculate.js file now with two different functions calculateSentiment() and showTweet():

Notice that I’m initializing state and loading in the tweetContent and userName as props from the <TwitterName /> component.

The showTweet() function will be quite simple and will display the tweet if it exists. It will also show a button to call the calculateSentiment() function:

That calculateSentiment() function will look like this:

This function calls the Microsoft Azure’s Text Analytics via Rapid API. I like using Rapid API as it allows me to connect to a bunch of different APIs from a single login.
I keep the key within an .env file in the client folder. So be sure to create a new .env file to protect your credentials as well:

The calculateSentiment() function sends the tweetContent to the API and returns a score.
Add to the render() function a place to display that new state:

Notice that underneath the score, the final component is called: <PhoneNumber />. Be sure to also pass the state via props to that component.

Send your SMS Sentiment Report with Nexmo’s Messages API

Let’s build out our final phoneNumber.js file now with the functions sendSMS(), renderPhoneInput(), and renderSendButton() to build out the functionality to send our SMS message via Nexmo Messages API:

The first method called is renderPhoneInput(). Let’s build that out now:

If the component has received a score from the last component, this function will show an input for the user to provide their phone number to send the sentiment analysis report. Once the user has provided a valid number, the renderSendButton() function will be activated:

This button then calls the final function, sendSMS(), which looks like this:

This function sends all of the state we’ve collected from the user to the server. And finally alerts the user once that function has successfully completed.

Our final step is to return back to the server.js and write the'/sendSMS') function:

There is a bit of logic at the top to do some sentiment analysis based on the score. You may certainly remove that if you’d prefer to send the exact number as opposed to emojis.
It then sends the report as an SMS from your Nexmo number to whatever number the user has provided.

Run your app

Here’s what the structure of my final app looks like:
structure of folders and files

The final step is to give the code a shot and see how things work! Move back into the root directory and run the command that will fire up the server and the client simultaneously:

Visiting localhost:3000 will display our React component that prompts the user for a Twitter handle.
That user’s most recent tweet will then be retrieved and displayed on the page.
Click the calculate the sentiment button to call the Microsoft Azure Text Analytics API and display the tweet’s score.
Enter your personal phone number and the SMS report will be sent to you via Nexmo’s Message API.

🎉Voila!🎉 You’ve successfully created a React app to analyze the sentiment of your Twitter that is capable of sending an SMS report to anyone!

Next Steps

If this has piqued your interest in doing more with messages and sentiment analysis, here are some ideas for things to try next:

  • Nexmo’s Dispatch API allows you to redirect your message depending on its delivery status. Play around with that and try sending your sentiment report to Viber or WhatsApp as well!

  • Play with Twitter’s /streaming capability and consider sending a report every time a username tweets.

Leave a Reply

Your email address will not be published.

Get the latest posts from Nexmo’s next-generation communications blog delivered to your inbox.

By signing up to our communications blog, you accept our privacy policy , which sets out how we use your data and the rights you have in respect of your data. You can opt out of receiving our updates by clicking the unsubscribe link in the email or by emailing us at