How to measure the sentiment of a Twitter #hashtag with Microsoft Flow
A month ago we had a Hackathon at our company. This is always a good time to try some new Tech stuff. In this weekend I managed to create a simple proof of concept for measuring sentiment on Twitter with some basic configuration in Office 365 and Azure. In the following article I will explain how to create this simple solution with SharePoint online, Flow and the Text Analytics Azure Cognitive Service.
Web care team sentiment tracking
It all started with this basic idea:
Why should a web care team need to manually read and monitor tweets on Twitter in order to be able to respond in an appropriate time frame?
I wanted to create a solution that sents notifications to a member of the web care team when the sentiment for tweets with a specific hashtag hits below a certain score.
I call it the Lazy Web Care Solution! 😉
Low Sentiment Incident Management
In this example we monitored #Office365, but you can configure it to use another hashtag. The score is also configurable and in this case we wanted to sent out notifications if the score was below 0.25 (on a scale of 0 to 1).
The funny thing was that during the test phase I ended up asking my colleague Rebekka to post a tweet with some low sentiment. This was because a lot of tweets with #Office365 had a pretty high sentiment score. This was her tweet:
So my colleague can finish his test for Text Analytics…. #Office365 Sucks Big Time!!!!!
— Rebekka Aalbers (@RebekkaAalbers) June 15, 2018
After a couple of minutes a notification about this low sentiment tweet ended up in the mailbox of my web care team member:
Thank you very much. The lazy web care solution based on Azure Text Analytics api works! pic.twitter.com/Rj13YKYXv9
— Dennis (@expiscornovus) June 15, 2018
The components to build this on your own
The building blocks of this solution are SharePoint, Azure and Microsoft Flow. In order to build this you need a SharePoint list, a Text Analytics API account and two Microsoft Flows.
Step 1: Creating the SharePoint list
Just create a custom SharePoint list on some random SharePoint site. You need some custom columns to store the Twitter attributes, but the most important column is of course the sentiment score column. This column is going to be used for the sentiment analysis.
You can create this list via the interface but of course you can also use PnP PowerShell, below is a simple script:
Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/somesite -UseWebLogin
New-PnPList -Title Social -Template GenericList
Add-PnPField -List "Social" -DisplayName "Sentiment" -InternalName "Sentiment" -Type Number
Add-PnPField -List "Social" -DisplayName "TweetDate" -InternalName "TweetDate" -Type DateTime
Add-PnPField -List "Social" -DisplayName "UserName" -InternalName "UserName" -Type Text
Add-PnPField -List "Social" -DisplayName "TweetLanguage" -InternalName "TweetLanguage" -Type Text
Add-PnPField -List "Social" -DisplayName "Tweet" -InternalName "Tweet" -Type Note
Add-PnPField -List "Social" -DisplayName "TweetLink" -InternalName "TweetLink" -Type URL
Step 2: Creating the Twitter monitoring Flow
In order to get Twitter data into the list I created a Microsoft Flow. This is probably the easiest step, because there already is an existing template for this called Save tweets that include a specific hashtag to a SharePoint list.
So simply use that template for the SharePoint list you created in step 1. Configure the Twitter action with the Search text you want to monitor, in our case #Office365. And create a mapping between your custom columns in the SharePoint list and the tweet attributes in the Create item action of the Flow. You should have something similar like below:
Step 3: Registering for an Text Analytics API account
The basic steps on how to create an Cognitive Services API account are described on docs.microsoft.com. You can find an example overhere. In this case you need to create an account of the Text Analytics API type. Write down you account key and the url of your endpoint. You need those in the next step when you are going to configure the Detect Sentiment action.
Step 4: The Text Analytics API flow
In the last step we are creating a custom Flow which measures the sentiment of tweets that are saved in our social list.
The flow starts with a When an item is created trigger. Configure it to use your social list, created in step 1. Add a Text Analytics action called Detect Sentiment and use the account key and endpoint url for the connection.
Use the tweet column value as the input for you Text Analytics action. After that I used a condition to check if the score is below 0.25. If this is the case I use a Send me an email notification action to sent the details of the tweet to a web care team member.
After that score condition I use a last action to update the list item with the sentiment score it got from the Text Analytics API. That last step could be useful if you want have an overview or if you want to do some kind of reporting on the tweets. I actually used some column formatting to give a simple overview of which tweets had a high sentiment score. Below is an example of that:
The origin of my idea came from the #SPC18. Of course I had some items on my to do list from #SPC18, the SharePoint conference which was in Las Vegas about two months ago.
I was triggered by a tweet of Chris McNulty at that conference:
— Chris McNulty (@cmcnulty2000) May 21, 2018
The Techcommunity blog that Chris posted contained a lot of nice announcements on the subject of Intelligence and Automation.
Another resource you should start reading is the Cognitive services overview site. The language service we used today is Text Analytics. You can submit text to this API and it can give back a sentiment score between 0 and 1. You can try this API yourself via this link.
Have fun playing around with this new features!