DEV Community

Cover image for Sentiment Analysis for Tweets in a Chat Bot - Part 1 The Setup
Albert Bennett
Albert Bennett

Posted on • Updated on

Sentiment Analysis for Tweets in a Chat Bot - Part 1 The Setup

If you liked what you read feel free to connect with me on linkedin or follow me on dev.to :)

Hi and welcome to my blog post.
Today I'd like to show you how you can use the Twitter API, luis.ai and the Microsoft Bot Framework to create a chat bot that does sentiment analysis on tweets posted on twitter. To be fair though the chat bot part is more for display and demonstration purposes. The function App with the twitter integration is where the actual magic ✨ happens.
See below for links to the GitHub repos:
https://github.com/Albert-Bennett/TwitterSentimentAnalysisFunctionApp
https://github.com/Albert-Bennett/TwitterSentimentAnalysisChatBot

This is going to to a three part series.
In this part I'm going to explain how to set up various resources so that we can start programming in part 2. If you'd like you can click on the thumbnail below for a demonstration of the chat bot in action.
twitter sentiment bot demo video

Getting Started
For this part you'll need access to an Azure account and you'll need to create a twitter developer account. This part shouldn't be to complex just a lot a forms and procedures.

Step 1 - Twitter Authentication App
With the Twitter API it is fairly straight forward. Just go here: Twitter API Signup Page and sign up if you haven't already.
When that's done you'll need to create a new project and an app in that project. With the app created you should be shown that app's authentication details. This would include:

  • An API Key
  • An API Key Secret
  • A Bearer Token

❗ As soon as that screen pops up copy the - API Key, API Key Secret and Bearer Token to a new notepad page or something. As soon as you transition off that page those details will be hidden. Although you can regenerate them at anytime, you will need to save those details for the Twitter integration later on.
You can read through and explore the docs if you want at this point now that you have your bearer token you can access some of the API endpoints and try them out. The Twitter docs are amazing, everything is fairly easy to find and they have an in browser testing feature where you can see what the endpoints are going to look like built from the fields that you have selected.

Step 2 - Azure Resources
We need to create some resources in Azure to get us started. Mostly to support the luis app.
Firstly you need to create a new resource group in Azure to hold all of the resources that we will be using in this application. This is fairly straight forward, just search in Azure for resource group and create a new one.
new resource group It is important to make sure that the region that you select to create the resource group in is close to where you are and that all resources that you create are in the same region this is so that data doesn't have to travel far in-between the differnt applications and services.
Next we need to create a new 'language understanding' resource in Azure in our new resource group. new language understanding
This resource is essentially luis in Azure. We need it so that when we create a new app in luis we can associate it with this resource.

Step 3 - luis app
We are going to be using luis.ai in this application to preform the sentiment analysis on the tweets that we get back from Twitter. luis.ai is a NLP (natural language processor) meaning that it is used to process natural language not just a set of commands but, unstructured language like this and these words as well.
Firstly you'll need to sign in to the luis service and set the authoring resource to be the new authoring resource that should have been created with the language understanding resource from the previous step.Setting authoring resource
From here we can create a new app.
new luis app
We don't need to create entities, utterances or anything for our app to do the sentiment analysis. What we do need is to click train at the top of the screen.
top menu in luis
With the model trained the publish button should be enabled.
Next we need make sure that when the luis model gets published it has setup to do sentiment analysis. This is done by just changing the publish settings which is found when you click publish.
luis publish settings I've set mine up to use the production slot when it gets deployed just to make the calls in the function app a little simpler.
Although we can test the luis app before publishing it, the sentiment analysis is something inbuilt in luis and I don't think that what we do in the app can influence the sentiment analysis of the tweets from twitter. We still need to test the service though to make sure everything has been setup correctly.
Sentiment analysis test
Last but not least while we are in luis, if you go to the manage page at the bottom there is an example query. We will need this for step 2 as it will for the foundation of our luis endpoint.
luis query example
With that setup and tested it's on to the next step.

Thanks for reading and I'll see you in the next one!

If you like this post and are interested in learning a little more about the Microsoft bot framework feel free to look through my other blog posts:

Top comments (0)