Intranoggin

Blither, Blather, Web Content Management.
Blog » Alexa on Azure: Part 1- Creating the Function

Alexa on Azure: Part 1- Creating the Function

This MSDN article explains how to get started with Azure Functions, but you’ll need to download and install the Azure .Net SDK and the Visual Studio Tools for Azure Functions.

Once you’ve got the tooling installed, you’ll be able to create a new project and select Azure Functions from the C# templates.

image

We end up with a pretty simple structure. Just a couple .json files and an html file and we want to delete the .html file anyway. At this point it’s only a container in which we’ll place our functions.

image

Right click the project root and select Add>New Azure Function

image

There are several types of functions available, for different triggers and in different languages. For integration with Alexa, select the HTTPTrigger – C# function, set it’s Authorization to Anonymous, and give it a name. Then hit Create

image

That will create a directory for the function name. Delete Project_Readme.html. Create a new folder called SpeechAssets. Inside SpeechAssets folder add two files called IntentSchema.json and SampleUtterances.txt

NOTE: The current version of the preview tools have a bug where the Add>New Item menu doesn’t provide any options. To create the IntentSchema.json and SampleUtterances.txt files, just copy one of the other files, like appsettings.json, into that directory and rename it.

image

Open the run.csx file. By default this function returns a simple Hello world type of response. At this point, we’re only going to make the update required to send back the response in a json format that Alexa expects.

Replace the returns statement (lines 18-21) with the function below.

image

becomes:

image

You can get the full file on GitHub at AlexaHelloWorld

Technically that’s all you need on the code side. Hitting F5 now will launch the service locally and give you a localhost url to test it on.

image

Doing so and then entering that url into your browser will yield back the .json response.

image

NOTE: Alexa will call your function with a post, not a get. If you want to do some more realistic testing, fire up Fiddler and then compose a Post request.

The IntentSchema.json and SampleUtterances.txt files aren’t used by the Azure function at all. They simply hold information that we need to paste into the Amazon developer portal when we register our service. Placing them here with the function code just gives us a good place to store them with the relevant code. Although we could push this to Azure now, we’ll first fill in these two files.

Open up IntentSchema.json. At this point, we’ve only got one response, so we only need one intent. Enter this text in the file. (full file on GitHub)

{   "intents": [     {       "intent": "HelloWorldIntent"     }   ]
}

The SampleUtterances.txt file holds all of the phrases that a user might say mapped to the intent where you want to route them. You could see how this would be handy if we had several types of responses we would want to send. In this case, we have only one intent and we’re sending the same response every time. Nevertheless, we can fill this in with entries such as: (GitHub Example)

HelloWorldIntent say hello
HelloWorldIntent say hello world
HelloWorldIntent Hello
HelloWorldIntent say hi
HelloWorldIntent hi
HelloWorldIntent what's up

NOTE:  You will need at least one of these entered on the Alexa registration page.

Now sign in to the Azure Portal. Click New, and then under Compute, choose Function App

image

A Function app is a container of functions, just like our function project in VS was a container of functions. With that in mind, give your container a name, mine is AlexaFunctions2 to indicate this is where all my functions relating to Alexa are going to be. If you had multiple Alexa Skills, you might end up creating a bucket for each skill. Don’t get too paralyzed trying to plan it out yet. It’s pretty simple to blow these away and republish the set of functions in a new bucket later on.

I’m also creating a new Resource Group and StorageAccount specific to this set of functions.

For hosting plan, I’m choosing the Consumption Plan, which according to the pricing page offers the first million executions free each month.

You can also choose an App Service Plan to host your functions. I haven’t done functions this way, but potentially if you were already hosting a web site or something else in an app service plan, you could do double duty and have it also host your functions. Of course, with the first million executions free, I’m struggling to find a scenario where your functions are being called in volumes high enough that the cost savings of this approach makes sense. If your app service is hosting functions with this volume, you’d likely need to increase its pricing tier. I thought maybe the draw would be deployment slots or custom URLS maybe Application Insights – similar DevOps features to what you get with hosting a web app, but that doesn’t seem to be the case.

image

After a minute or so to get the function app deployed, we should be able to deploy our function code to it. Back in Visual Studio, right click the project and select Publish

image

In the dialog box, choose Microsoft Azure App Service

image

Choose the Function App we just created and click OK.

NOTE that we see an available expansion there for Deployment Slots. I’m not sure if this is an upcoming feature or a bug in the tooling, but as far as I can tell, they aren’t supported at this time. (Hopefully they get built at at some point. Deployment slots rule.)

image

After clicking OK, the dialog box will refresh and add in the Connection, Settings, & Preview tabs.

image

You won’t need to change anything on the other tabs, so you can click Publish now. Later on, you may want to go to the Settings tab and check the option to ‘Remove additional files at destination’. that will keep our azure function container cleaned up of old functions.

Once it has successfully published, you can navigate to it in the Azure Portal. On the Function’s Develop tab, you’ll see the code from your Run.ctx file. Don’t make any changes here since we’re driving those from VS. However, you’ll note the URL of our function at the top of this page. Just like we tested our local version, we can copy and paste this into a browser and verify we get back our expected json string. You should also use Fiddler to create a post request and verify it’s return value.

image

To test in Fiddler see this image.

image 

The Function’s Integrate and Manage tabs don’t have anything we’re changing right now. The Monitor Tab is interesting in that it shows you how many times your function has been called and if the response was a success.

image

Down at the bottom of the left column, you’ll find a link to the Function app settings

image

There are some additional configuration settings available in here.

image

Of Note:

  • Configure app settings allows you to set up key value settings pairs just like for your web sites. We can store values in here like we would a web.config in the olden days, for example if you have a SQL connection string to store. Here again, we see some bleed through of the slot terms and options but since we can’t set up any slots, marking something as a slot setting is moot.
  • Dev Console opens a command line console in the browser, but I'm not sure what one would do with this yet.
  • Configure Continuous integration allows you to set up a web hook into VSTS, OneDrive, Git, GitHub, Bitbucket, Dropbox, or another External Repository and have function code automatically deployed when your repository is updated. So far I've only tried with VSTS and it did NOT successfully deploy any updates.
  • Go to App Service Settings – even more of the web app options than the above Configure app settings has. However, still no slots.
  • Configure CORS – if you were calling from a browser.
  • Configure API metadata – give it the url to your Swagger file if you create one.
  • Set Quota – allows you to set a maximum daily usage cap. One of the strengths of functions is automatic scaling, but it is potentially scary as well. This lets you take the risk out of it.

In the next article, I'll walk you through registering this skill in the Amazon developer portal.


Posted: 2/14/2017 7:00:00 AM by Ryan Miller | with 0 comments
Filed under: Alexa, Azure, Azure Functions