A Xamarin + IoT + Azure sample that detects the sentiment of incoming text messages, performs sentiment analysis on the text, and changes the color of a Philips Hue lightbulb
Clone or download

README.md

TextMood

An IoT + Azure sample that detects the sentiment of incoming text messages, performs sentiment analysis on the text, and changes the color of a Philips Hue lightbulb to correspond with the average sentiment.

Architecture

Text Mood Architecture

Demo

  1. The phone on the left sends a text message to the Twilio phone number.
  2. Twilio forwards the text message to an Azure Function
  3. This first Azure Function determines the sentiment of the message then sends an emoji response back to the phone on the left
  4. The second Azure Function saves the message to an Azure SQL Database
  5. The third Azure Function invokes SignalR
  6. SignalR sends a real-time update to the phone on the right, containing the new message
  7. The phone on the right displays the new message, then updates its background color to match the average sentiment of the received messages
  8. The phone on the right then simultaneously changes the light color of the Philips Hue light to match the average sentiment

Text Mood Demo

Cognitive Services Setup

POST Request

{
  "documents": [
    {
      "language": "en",
      "id": "251c99d7-1f89-426a-a3ad-c6fa1b34f020",
      "text": "I hope you find time to actually get your reports done today."
    }
  ]
}

Response

{
"sentiment": {
  "documents": [
    {
      "id": "251c99d7-1f89-426a-a3ad-c6fa1b34f020",
      "score": 0.776355504989624
    }
  ]
}

1. Create a Sentiment Analysis API Key

To use the Sentiment Analysis API, we'll need to first create an API Key using the Azure Portal.

  1. Navigate to the Azure Portal

  2. On the Azure Portal, select + Create a Resource

  3. In the New window, select AI + Machine Learning

  4. In the Featured frame, select Text Analytics

  5. In the Create window, make the following selections

    • Name: TextMood
    • Subscription: [Select your Azure subscription]
    • Location: [Select the location closest to you]
    • Pricing Tier: F0 (5K Transactions per 30 days)
      • This is a free tier
    • Resource Group: TextMood
  6. In the Create window, select Create

  7. On the Azure Portal, select the bell-shaped notification icon

  8. Stand by while the Notifications window says Deployment in progress...

  9. Once the deployment has finished, on the Notifications window, select Go to resource

  10. In the TextMood Resource page, select Keys and locate KEY 1

    • We will use this API Key when we create our Azure Function
  11. In the TextMood Resource page, select Overview and locate the Endpoint

    • We will use this Url when we create our Azure Function

2. Create an Azure Function

Azure Functions are a serverless offering in Azure. In these steps, we will use Azure Functions to create a POST API.

  1. Clone or download this repo

    • To clone the repo using git, open a cmd prompt and enter the following command:
      • git clone https://github.com/brminnick/TextMood.git
    • To download the repo use the following steps:
      • Click Clone or Download
      • Click Download Zip
  2. After downloading/cloning the repo, locate TextMood.sln

  3. Launch TextMood.sln in Visual Studio

  4. In CognitiveServicesConstants.cs, for TextSentimentAPIKey, replace Your API Key with the Key from the TextAnalytics Cognitive Service resource created earlier

  5. In CognitiveServicesConstants.cs, for BaseUrl, replace Your Cognitive Services Base Url with the Base Url from the TextAnalytics Cognitive Service resource created earlier

  6. In the Visual Studio Solution Explorer, right-click the TextMood project

  7. In the right-click menu, select Publish

Right Click Publish

  1. In the Publish window, select Function App
  2. In the Publish window, select Create New
  3. In the Public window, select Publish

Publish Window

  1. In the Create App Service window, enter an App Name

    • Note: The app name needs to be unique because it will be used for the Function's url
    • I recommend using TextMood[LastName]
      • In this example, I am using TextMoodMinnick
  2. In the Create App Service window, select your subscription

  3. In the Create App Service window, next to Resource Group select New...

Create App Service

  1. In the New resource group name window, enter TextMood
  2. In the New resource group name, select OK

Create App Service Group

  1. In the Create App Service window, next to Hosting Plan, select New...

New Hosting Plan

  1. In the Configure Hosting Plan window, enter a name for App Service Plan

    • I recommend using TextMood[LastName]Plan
      • In this example, I am using TextMoodMinnickPlan
  2. In the Configure Hosting Plan window, select a Location

    • I recommend selecting the location closest to you
  3. In the Configure Hosting Plan window, for the Size, select Consumption

  4. In the Configure Hosting Plan window, select OK

Configure Hosting Plan

  1. In the Create App Service window, next to Storage Account, select New...

New Storage Account

  1. In the Storage Account window, enter an Account Name
    • I recommend using textmood[LastName]
      • In this example, I am using textmoodminnick
  2. In the Storage Account window, for the Account Type, select Standard - Locally Redundant
  3. In the Storage Account window, select OK

New Storage Account

  1. In the Create App Service window, select Create

Create App Service

  1. Stand by while the Azure Function is created

Deploying App Service

  1. In the Visual Studio Solution Explorer, right-click the TextMood project
  2. In the right-click menu, select Publish

Right Click Publish

  1. In the Publish window, select Publish

Azure Functions Publish

4. Copy Azure Functions Url

  1. Navigate to the Azure Portal
  2. On the Azure Portal, select Resource Groups
  3. In the Resource Group window, select TextMood

Resource Group TextMood

  1. In the TextMood Resource Group window, select Overview
  2. In the TextMood Overview window, select the Azure Function resource, TextMood[LastName]

Open Functions

  1. In the Azure Functions window, select AnalyzeTextSentiment
  2. In the AnalyzeSentiment window, select Get Function Url
    • We will use this Url when we create our Twilio phone number

Get Function Url

Twilio Setup

1. Create Twilio Phone Number

  1. Log into your Twilio account and navigate to the Manage Numbers page.

  2. Click the + sign to buy a new number

    • Note you may skip this step if you have an existing Twilio Phone Number

Buy a new number

  1. On the Buy a Number page, select a Country

    • For this example, I am using United States
  2. On the Buy a Number page, tap the Number drop down and change it to Location

  3. On the Buy a Number page, enter a location name

    • For this example, I am using San Francisco
  4. On the Buy a Number page, check SMS

  5. On the Buy a Number page, click Search

Buy a new number page

  1. On the search results page, locate the number you'd like to purchase and select Buy

Buy Number

  1. In the confirmation window, select Buy This Number

Buy This Number

  1. In the purchase confirmation window, select Setup Number

Setup Number

  1. In the Messaging section, next to A Message Comes In, select Webhook
  2. In the Messaging section, next to A Message Comes In, enter the Azure Function Url that we created in the previous section
  3. In the Messaging section, next to A Message Comes In, select HTTP POST
  4. In the Manage Numbers window, select Save

Messaging

2. Send a Text

  1. Open a text messaging app and send a text message to your Twilio phone number

Happy Text

Resources