Build a Hamilton Song Recommendation SMS Bot with Machine Learning
Time to read: 4 minutes
Hamilton the Musical will start streaming on Disney Plus this Friday, so happy Hamilfilm week! To celebrate, learn how to build a SMS chatbot that recommends the Hamilton song most relevant to you right now using Twilio Programmable SMS and Functions, Microsoft Azure Cognitive Services, and JavaScript.
See it in-action: text how you’re feeling to +13364295064. The longer and more descriptive your message is, the more data the app has to analyze what Hamilton song you need now!
Prerequisites and setting up Azure Cognitive Services
To follow along with this post, you need three things:
- A Twilio account - sign up for a free one here and receive an extra $10 if you upgrade through this link
- A Twilio phone number with SMS capabilities - configure one here
- Microsoft Azure - make a free account here if you don't have one already
To use Azure Cognitive Services, you will need an Azure key and endpoint. Follow the directions here to create a Cognitive Services resource using the Azure services portal.
After filling out the Resource, click Create. Once your Resource deploys, click Go to resource. You should see your endpoint and key in the quickstart pane that opens, or you can also click Keys and Endpoint beneath Resource Management on the left-hand pane.
If you are shown two keys, you will only need the first one which we will now configure to be the value for an environment variable.
Configure a Twilio Function with Azure
Configure your Twilio Functions with your Azure endpoint and key as environment variables from the last step.
Then add the dependencies @azure/ai-text-analytics 1.0.0
and whichx *
as shown below. This post will also use Whichx, a naive Bayesian classifier that can succinctly and cleanly analyze data. You can read more on Naive Bayes here.
Click Save and you can now use Azure AI Text Analytics and reference your Azure endpoint and key in any of your Twilio Functions!
Make a Twilio Function
On the left-hand panel underneath Functions, click Manage. To make a new Function, click the red plus button and then select a Blank template followed by Create.
Give your Function a name like "What Ham Song do you need" and a path, like "/hamilfilm".
Analyze an inbound SMS using Azure Cognitive Services and Naive Bayes with Node.js
Replace the Function code with the following:
Wow, that's a lot of code. Let's break it down.
We import Azure AI Text Analytics and WhichX at the top with:
Then we make our Function asynchronous to give the Function more time to analyze the inbound SMS input, make a MessagingResponse
object that we will later return as an outbound SMS, create variables referencing our Azure endpoint and key environment variables, and pass them to textAnalyticsClient
. Lastly, we pass in the inbound text message body to an array input
.
Next we make the key-value object holding the collection of Hamilton songs the user can be classified as. Each song has a brief corresponding description the classifier will attempt to match to according to the inbound SMS. The complete code for the `songs`object is on GitHub here.
Now we call our client's analyzeSentiment
method, which returns a SentimentBatchResult
object, and create some global variables.
Iterate through the list of results, and print each document's ID and document-level sentiment (analyzes the whole text) with confidence scores. For each document, result
contains sentence-level sentiment (analyzes just a sentence) along with confidence scores (percent confident the model is that the sentiment is positive, negative, or neutral) and more information that we do not need for this post. Lastly, we find the key (positive, negative, or neutral) that has the highest confidence level value.
Finally, we build our naive Bayesian classifier, using it to classify the inbound text according to Hamilton songs by adding the labels of the Hamilton songs we want to classify. You could build a classifier in a variety of ways, but this is a succinct way to do so.
Save your Function. You can view the complete code on GitHub here. Let's now configure a Twilio phone number to analyze text messages to it, sending back the recommended Hamilton song.
Configure your Twilio Phone Number with a Twilio Function
If you don't have a Twilio number yet, go to the Phone Numbers section of your Twilio Console and search for a phone number in your country and region, making sure the SMS checkbox is ticked.
In the Messaging section of your purchased number, in the A Message Comes In section, set the dropdown menu to Function instead of Webhook and then on the right select your Function from the larger dropdown menu, as shown below. Hit Save.
Whip out your phone and text your Twilio number how you are feeling to see what Hamilton song you should listen to right now.
I will be listening to Hamilton to celebrate Hamilton coming to Disney Plus. In the meantime, you can use different tools to analyze texts like IBM Watson, Google Cloud Natural Language, TensorFlow.js, and more. You could also recommend a Hamilton lyric (must include "You're On Your Own. Awesome. Wow! Do You Have A Clue What Happens Now?") Let me know what you're building and what your favorite Hamilton song is online or in the comments.
- Twitter: @lizziepika
- GitHub: elizabethsiegle
- Email: lsiegle@twilio.com
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.