Adding VoiceLabs to your Amazon Alexa Skills in C#

If you’re a C# programmer and have created Amazon Alexa Skills, you already know it’s tough to get some C# code samples, SDK’s and just a overall clear path to satisfy your curiosity for creating Alexa Skills. These days, it’s just tough to get C# support on a lot of the new services out there. We are just now seeing the big fish like Google and Amazon support C# on their cloud offerings which is great, but startups like VoiceLabs for instance, come out of the gate with SDK’s for Node.js, Python, Java and Ruby… no C#.

If you’re not familiar, VoiceLabs is a free analytics platform for Voice that supports Amazon Alexa, Google Home, Cortana and Siri. Actually, it supports just about any platform really. I have my API.ai chatbots logging on VoiceLabs right now, mainly just to see if I could. I just set my VoiceLabs project to Google Home and made a note in the metadata that says it’s really for API.ai, works for me!

So back to my SDKs rant, as I navigated their “Install SDK” section realizing no support for C# at the time of this writing, I figured, ok, nothing new, I’ll just write my own… again… just need to find the HTTP API documentation. After clicking on page after page and a couple of Google searches later, I could not find any information on any kind of HTTP API. At first I was upset, I mean who doesn’t post their HTTP API docs! Then, I took this as a challenge. Yes, I could of called or emailed, but no, that’s all too easy. I had to break down their Node.js SDK and figure it out for myself.

To make a long story short, here’s what I came up with. Works great on my machine…


var payload = new
{
app_token = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
user_hashed_id = MD5Hash(request.Session.User.UserId),
session_id = request.Session.SessionId,
intent = request.Request.Intent.Name,
data = new {
metadata = request.Request.Intent.Slots,
speech = response.response.outputSpeech.text
},
event_type = "SPEECH"
};

var data = JsonConvert.SerializeObject(payload);

using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.UploadString("https://api.voicelabs.co/events/?sdk=n_1&auth_token=" + payload.app_token, data);
}

A couple of notes to consider when implementing the above code, first, get your “app_token” from VoiceLabs.co and replace the “XXX…” value. Then make sure that “request” is set to your AlexaRequest object. Resolve all usings for JsonConvert (Newtonsoft), WebClient and so on. Lastly, that MD5Hash is function I found online, can’t remember who or where I stole it from but this is what that looks like if you just want to steal it from here, have at it…


public static string MD5Hash(string text)
{
MD5 md5 = new MD5CryptoServiceProvider();
md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));
byte[] result = md5.Hash;

StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
strBuilder.Append(result[i].ToString("x2"));
}

return strBuilder.ToString();
}

So there you have it, drop that in your C# Alexa Skill, Google Assistant Action, API.ai chatbot or whatever else you have that handles logic for voice or chat intents.

Overall, despite it’s HTTP API & C# SDK shortcomings, VoiceLabs is a promising analytics platform for voice. Works fast and my intent requests are visualized instantly in the Voice Insights interface. I definitely recommend checking them out for yourself at http://voicelabs.co.

EDIT: I just chatted with Adam from VoiceLabs, cool dude! And yes, they do have information on their HTTP API, you just need to contact them to get it. I say, if you’re up for the challenge, let’s get some community supported open source SDKs going on Github for C#, Unity, C++ and whatever other language you want to support!

If you run into any problems hit me up on http://twitter.com/WaltQue and if you haven’t done so yet, check out my Pluralsight course on creating Alexa Skills in C#. It’s getting a bit dated but still plenty of relevant information. Enjoy!

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on Reddit

One Reply to “Adding VoiceLabs to your Amazon Alexa Skills in C#”

Comments are closed.