In this lab, we will create an Azure Logic App that monitors Twitter, analyzes the sentiment of customer tweets with Text Analytics v2 service, and creates incidents in Pager Duty. PagerDuty is an incident management platform that provides reliable notifications, automatic escalations, on-call scheduling, and other functionality to help teams detect and fix problems quickly. This application can help customer service agents react swiftly to customer complaints and avoid brand reputation damages.

Text Analytics Cognitive Service

You will require an active Azure subscription for building this project. If you do not have one, signup for a free account here. Next, you will need a Text Analytics API Key to access the sentiment analysis service. Enter the text “Text Analytics” in the search box on the Azure portal. From the search results, select the Text Analytics service.

Search Text Analytics service
Search Text Analytics service

Next, add the required details to create an instance of the Text Analytics service as follows.

Create Text Analytics service
Create Text Analytics service

Record the key and the URL of the service from the newly created service tenant. We will use these later when we compose our Logic App.

Record Key and URL of the service
Record Key and URL of the service

Let’s now move on to create a Pager Duty account and service.

Pager Duty Service

We require a Pager Duty account for this application. Pager Duty comes with a 14-day trial that is sufficient for this demo. Sign up for a free Pager Duty account, follow the signup instructions, and log in to your account.

After logging in, as the next step, we will create a Pager Duty service. From the menu, click on Configuration and select the Services option.

Navigate to Pager Duty services view
Navigate to Pager Duty services view

From the landing page, initiate the action to create a new service. On the Add Service form, enter the service’s name and set the Integration Type to Use our API directly, which will enable our Logic App connector to interact with the service.

Create new Pager Duty service
Create new Pager Duty service

I like to think of Pager Duty services as teams that need to be notified in case of incidents. A service in Pager Duty is referenced through its Integration Key. To record the integration key, navigate to the Integrations tab from your service’s dashboard, and save the 32-character service integration key.

Save service integration key
Save service integration key

Requests to Pager Duty are authorized with an API Key, which also uniquely identifies an account in Pager Duty. To generate an API key, click on the Configuration menu item and select the API Access option.

Navigate to API Access view
Navigate to API Access view

On the next screen, click on the Create New API Key button and enter the name of the key. On submitting the form, you will ve able to view the new 16 character API key once. Save this key for use in our Logic App later.

View Pager Duty API key
View Pager Duty API key

By now, we have provisioned and configured everything in Pager Duty. Let’s now head back to the Azure Portal to create our Logic App.

Logic App

In the Azure portal, search for the text “Logic App” to land on the Logic App dashboard. From the dashboard, proceed to create a new Logic App and fill in the details as shown in the screenshot below.

Create new Logic App
Create new Logic App

Click on the Review + create button and approve the deployment of the application. Once the application is ready, navigate to the Logic App Designer view and select the Blank Logic App template.

Choose the Logic App template
Choose the Logic App template

A Logic App workflow is initiated by a trigger. In our case, the trigger is a new tweet that contains the name of our dummy product - BeetsEarbuds.

In the search box of the trigger, enter the text “When a new tweet is posted” which will resolve to the trigger that we need. Click on the resulting action to begin configuring it.

Twitter: new tweet posted trigger
Twitter: new tweet posted trigger

Adding the trigger requires signing in to Twitter and authorizing the Twitter connector to read tweets. After authorizing the connector, you will be redirected to the designer. In the trigger action, enter the text that the Logic App should search, which is BeetsEarbuds, and set the search frequency to an appropriate interval. Next, click the New Step button to add the next action.

Configure Twitter trigger
Configure Twitter trigger

We will now try to detect the tweet’s sentiment using the Text Analytics service that you previously created. Look up the text “Detect sentiment” and select the Detect Sentiment (V2) preview action from the result.

Select the Detect Sentiment (V2) preview action
Select the Detect Sentiment (V2) preview action

Grab the Text Analytics service details that you captured earlier and populate the fields Account Key and Site URL with those details. Assign a name to the connection by entering a valid string in the Connection Name field. Finally, click on the Create button to finish creating the connection.

Configure Text Analytics connection
Configure Text Analytics connection

In the next view, you will be asked to supply a list of documents that you want to analyze with the Text Analytics service. The Text Analytics v2 action can simultaneously process multiple documents, which is a significant improvement over the V1 service. However, we will process only one tweet at a time, so we only need to supply a single document to this action. You can either use the document mode to enter the required values, as shown in the image below, or use the raw input mode to enter the following JSON text.

[{"id": "1","text": "<Tweet text block using the expression helper dialog>","language": "en"}]

Both approaches would result in the same input to the Text Analytics service.

Configure Text Analytics action
Configure Text Analytics action

We will now add a condition to determine whether the tweet should result in the creation of an incident. Click on the New Step button and search for the text “Condition” in the actions dialog list. We will configure it so that it evaluates to true if the score computed from the previous step is less than 0.5. Earlier, we discussed that the Detect sentiment v2 action can process several documents at once. Hence, it produces results in the form of an array of type SentimentResults, an array of type documents. You can read more about the request and response formats for this action on the Microsoft documentation. When you add the Condition action to the workflow, Logic Apps would automatically detect that you are trying to parse an array and populate the output from previous steps field. Add a condition that evaluates to true if the score is less than or equal to 0.5, as shown in the image below.

Configure condition action
Configure condition action

Finally, let’s add the Pager Duty action to the if true branch of the condition that will be executed when the previous condition evaluates to true. Use the text “PagerDuty Create Incident” to find the Pager Duty’s Create Incident action. Like before, you will be first prompted to create a connection. Assign a name to the connection and paste the 16 digit API key here that you copied previously from the Pager Duty console. Click on the Create button to proceed.

Configure Pager Duty connection
Configure Pager Duty connection

In the next step of the Create Incident wizard, you will be asked to enter the 32 character Service Integration Key that you copied from the Pager Duty console (remember service = team). In the Description field, enter the details of the incident. You can see that I used the dynamic content assistant to write a rich incident message in the image below.

Configure Pager Duty action
Configure Pager Duty action

All the required configurations are complete now. Click on the Save button to save the Logic App and click on the Run button to start the application.

Testing The Application

Head over to Twitter and post a negative sentiment tweet that includes the text “BeetsEarbuds”. Remember that our Logic App connector is continuously searching for this text on Twitter.

Post a tweet that should trigger our workflow
Post a tweet that should trigger our workflow

Twitter takes some time to index the tweets, and hence the workflow might not trigger immediately. After some time, you can view the Logic App run from the Overview section of the Logic App. Click on the run link to land on the view that presents the run’s details, including the parameters that were passed between actions. This view is handy if your Logic App fails and requires debugging.

View Logic App run
View Logic App run

Let’s check the Pager Duty dashboard now, which should have an incident created for our team.

View incident on Pager Duty
View incident on Pager Duty

Automatic incident management systems are a common ask of many organizations, but many of them still involve manual triaging processes. By leveraging the power of Azure AI, you can build such systems at a fraction of the cost and development time. I hope I was able to inspire you to come up with more such scenarios. I would love to hear what you are building with Logic Apps in the comments section below.

Did you enjoy reading this article? I can notify you the next time I publish on this blog... ✍