Sentiment Analysis for Tweets in a Chat Bot - Part 1 The Setup
Tags: azure, webdev, bot, twitter
Published: 2022-07-22 07:56:00

If you liked what you read feel free to connect with me on [linkedin](https://www.linkedin.com/in/albert-bennett/) or follow me on dev.to :)
<br/>

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 :sparkles: 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](https://img.youtube.com/vi/3SvQtcVuQEQ/0.jpg)](https://www.youtube.com/watch?v=3SvQtcVuQEQ)
<br/>

**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.
<br/>

**Step 1 - Twitter Authentication App**
With the Twitter API it is fairly straight forward. Just go here: [Twitter API Signup Page](https://developer.twitter.com/en/docs/twitter-api/getting-started/getting-access-to-the-twitter-api) 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


:heavy_exclamation_mark: 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.
<br/>

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or15ol7f3ndislbpxqhc.png) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xllixecv4u6p6ku2l20j.png)
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](https://www.luis.ai) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i36dug6fkfts0tp3rr4q.png)
From here we can create a new app.
![new luis app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67gihhk4ozzwpby448tl.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndfl0hbmgjjs5smfkqyf.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jl5gn42cu3oiec2pcm2.png) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/diql9xslya68p4hkxa8j.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sovpj4fcpitxf4i500s9.png)
With that setup and tested it's on to the next step.

Thanks for reading and I'll see you in the next one!
<br/>


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:
- [How to: Connect a MS Bot Framework Chatbot to MS Teams](https://dev.to/albertbennett/how-to-connect-a-ms-bot-framework-chatbot-to-ms-teams-193d)
- [How to: Adaptive Cards and the Bot Framework](https://dev.to/albertbennett/how-to-adaptive-cards-and-the-ms-bot-framework-25km)
- [How to: Save conversation data in an Microsoft Chatbot] (https://dev.to/albertbennett/how-to-save-conversation-data-in-an-microsoft-chatbot-22ml)