Alexa

Creating Alexa Skills with Web-API and hosting on Microsoft Azure

This blog post will appear in The First C# Calendar being produced by @mgroves in the 8th December slot.

A lot of tutorials describing creating Alexa skills focus on using AWS to host it which immediately make the tutorials seem more complicated. (IMO).

This post hopefully shows just how easy it is to create an Alexa skill using the Microsoft stack and host it on Azure for free.

After all at the end of the day.. An Alexa skill is just an HTTP endpoint!

This blog post is based largely on the GitHub page for a NuGet Package I will be using called AlexaSkills.Net

The scenario skill: A Bin Calendar

It may sound a bit rubbish! Ha! Ha! (Pun intended). But I want to create a skill that will tell me which bins are being collected this week or next week on my estate (Bosden Farm).

Normally i'd have to refer to a calendar like this. And considering there are around 600 people on the estate, some of them may also find the skill useful.

Old bin calendar

Creating the Web API

In Azure you can create a website (App Service in Azure speak) and host it with Azure for free if you use their subdomain .azurewebsites.net. I will host my new skill at something like bosdenfarm.azurewebsites.net and it shouldn't cost me anything to host it there.

Create a blank Web API Application

You should know how to do this, make sure Web-API is chosen. If you don't know how to do this, check out a guide on how to create a blank web-api application.

Create a blank web-api application

Add NuGet package AlexaSkills.NET

Fortunately someone has already created a NuGet package called AllexaSkills.Net that abstracts a lot of the underlying lower level stuff away, allowing us to focus on the logic we want the skill to have.

Install a NuGet Package

Create your Skill

Create a class that inherits from the abstract class Speechlet, this will be your skill.

You will have to implement four methods (we're only going to use one)

  • OnIntent
  • OnLaunch
  • OnSessionEnded
  • OnSessionStarted

Inherit from speechlet class

At this point, you'll have to know what an Intent is. Your skill can have many intents.

Basically, you need an intent for each thing you want your skill to do.

My skill will have the following intents

  • Ask what this weeks bins are. I shall call this intent ThisWeeksBins.
  • Ask what next week bins will be. I shall call this intent NextWeeksBins.
  • Ask what last weeks bins were. I shall call this intent LastWeeksBins.

You will need to write the code that executes logic based on which intent you've requested.

Intent logic

Publishing the Web API to Azure

Let's upload the web app so far to Azure so that we can confirm the endpoint url among other things.

Right click the project and click Publish. I'm not going into detail on how to publish to an Azure web app resource.

Publish the web app to Azure

Creating a Skill in the Amazon Developer Console

Go to https://developer.amazon.com/home.html Sign in with an Amazon account, then go to the Alexa area.

Alexa area

Click 'Get Started' on the Alexa Skills Kit, then Click 'Add a new skill' on the page you're directed ti.

 

Creating the skill

Invocation word

When creating your own Alexa skill, you need to choose an invocation word.

The invocation word is the word you will use to activate your skill. It is often the name of your skill and is used in the format..

Alexa, ask [invocation_word] [phrase]

I shall set my invocation word to be Bosden Farm

Skill builder

When you hit next you will be redirected to a much more moder looking page called the 'Skill Builder'

Here you will list your Intents and the Utterences that trigger those Intents.

Skill builder

Utterences

Utterences are expected phrases. You map utterences to Intents.

So for example for the intent NextWeeksBins, (remembering that they will be prefixed by Alexa, ask BosdenFarm..) I may map the utterences

  • What are next weeks bins
  • For next weeks bins
  • Next weeks bins
  • Bins next week
  • What are the bins next week
  • What the bins are next week

You'll soon notice that the trick is to choose an invocation word and utterences that promote asking Alexa for things using natural language.

Here you can see I have created the new Intent called ThisWeeksBins and listed a number of utterences that I want to trigger that Intent.

This Weeks Bins

So combining the invocation word and utterences, i'm expecting my skill to respond to complete phrases such as..

  • Alexa, ask Bosden Farm what are next weeks bins
  • Alexa, ask Bosden Farm what the bins are next week
  • etc

Side note : Slots

I haven't used slots before but I believe there is a concept of slots in utterences. These are placeholders in the utterences that can be different variables.

I am not using / have not used them so wont go into any detail on them in this post.

Skill configuration

After building the model but before testing, you need to provide some configuration details.

Choose HTTPS and provide the url of your web-api where it is hosted in Azure.

Skill configuration

SSL Certificate

The free default Azure websites are https ready so choose the sub domain option as below.

SSL Certificate configuration

Testing utterences

If you get this far, remember to press the 'Save Model' and 'Build Model' button before continuing onto testing phrases.

The Amazon Developer Console allows you to test utterences. That is, you can type in a phrase and it will tell you which Intent it would map it to.

Testing utterences

Publishing the Alexa skill to Amazon

At the moment your skill, is only available on devices that are associated to your Amazon Account.
In order for anyone to install and use it, you'll need to publish to Amazon.

Gotchas:

The requested skill took too long to respond

You may get this the first time you make a request for your skill or if you haven't requested the skill for a while, then the second request will succeded. At first I blamed Amazon believing it was because the skill wasn't published. When in fact it was because the web application needed to 'spin up' from a cold state in Azure.

Summary

I hope you find enough information in this post to try to create your own skill(s). If you do end up creating your own skills i'd love to hear about them. You can find me on Twitter @LeeEnglestone.

Other references