By Sam Morgan, Head of Education at Makers Academy
Editor’s note: This is part one of our Makers Academy series for Ruby developers. Learn more about this free training on the Alexa Skills Kit in this blog post. And check out the full training course for free online.
Welcome to the first module of Makers Academy's short course on building Alexa skills using Ruby. Amazon's Alexa Skills Kit allows developers to extend existing applications with deep voice integration and construct entirely new applications that leverage the cutting-edge voice-controlled technology.
This course will cover all the terminology and techniques required to get fully-functional skills pushed live to owners of Alexa-enabled devices all around the world using Ruby and Sinatra.
This module contains a basic introduction to scaffolding a skill and interacting with Alexa. This module introduces:
During this module, you will construct a simple skill called “Hello World.” While building this skill, you will come to understand how the above concepts work and play together. This module uses:
Let's get started!
Our first step is to set up the skill on Amazon.
The invocation name is used by the user to access a certain skill. For example, "Alexa, ask Hello World to say hello world."
Intent Schemas
Now we have a new skill, let's construct the intent schema.
The intent schema lists all the possible requests Amazon can make to your application.
{ "intents": [ { "intent": "HelloWorld" } ] }
The minimal intent schema is a JSON object with a single property: intents. This property lists all the actions an Alexa skill can take. Each action is a JSON object with a single property: intent. The intent property gives the name of the intent.
Utterances
Now that we have the intent schema, let's make the utterances. Utterances map intents to phrases spoken by the user. They are written in the following form:
IntentName utterance
In our case, we have only one Intent: HelloWorld, and we'd like the user to say the following:
Alexa, ask Hello World to say hello world.
Our utterances are:
HelloWorld say hello world
We've now set up our skill on Amazon's Alexa Developer Portal.
Our second step is to set up our local Ruby application to be ready to receive encrypted requests from Amazon’s servers (i.e. HTTP requests over SSL or “HTTPS” requests).
We will walk through setting up a Ruby server using Sinatra. The server will run locally and be able to receive HTTPS requests through a tunnel.
Alternatively, you could set up a remote development server using Heroku (with Heroku SSL), Amazon Elastic Beanstalk (with a self-signed SSL certificate), or any other method you can think of.
We’re going to use ngrok to tunnel to a local development server.
Setting Up a Sinatra Application
# inside server.rb require 'sinatra' post '/' do p request.body.read end
Opening Your Sinatra Application to the Internet Using Ngrok
Our third step is to link the skill we set up on Amazon (1) with the tunnel endpoint (2) so our skill can send requests to our local application.
Configuring the Endpoint in the Alexa Skills Portal
When Amazon invokes an intent, Amazon sends a POST
request to the specified endpoint (web address).
Head back to your Alexa skill (for which you just entered intents and utterances). Hit “Next,” then set up the endpoint.
If using ngrok, your endpoint is the URL you copied, starting with "https" and ending with ".ngrok.io."
Configuring SSL
Amazon Alexa only sends requests to secure endpoints: ones secured using an SSL certificate (denoted by the 'S' in HTTPS). Since we used ngrok to set up our HTTPS endpoint, we can use ngrok's wildcard certificate instead of providing our own.
Testing in the Service Simulator
The Service Simulator in the Amazon Alexa Developer Portal allows you to try out utterances. Once you’ve written an utterance into the Service Simulator, you can send test requests to the application endpoint you defined. You can see your application’s response to each request that you send.
You’ve now hooked up your local development environment to an Alexa skill.
Now we have set up an Alexa skill (1), built a local development server with an endpoint tunnelled via HTTPS (2), and can make requests from Amazon to our local development server through that endpoint (3).
Our final step is to construct a response from our endpoint such that Amazon can interpret the response to make Alexa say, “Hello, world” to us.
Building the JSON Response
Amazon sends and receives JSON responses in a particular format. Let's set that up here.
# inside server.rb post '/' do { version: "1.0", response: { outputSpeech: { type: "PlainText", text: "Hello World" } } }.to_json end
There are a few parts to this JSON response object:
Testing Our Response in the Service Simulator...and Beyond!
Now that we've built a JSON response, we can restart the server and test out the new response in the Service Simulator.
If you would like to try your new Hello World skill out live, ask Hello World to say, “Hello, World” on any Alexa-enabled device registered to your developer account. You can also try it out on the browser-based Alexa skill testing tool Echosim.io.
Ready for the next module? Learn how to build a fact-checking skill with slots and custom slot types.
The Alexa Skills Kit (ASK) enables developers to build capabilities, called skills, for Alexa. ASK is a collection of self-service APIs, documentation, templates, and code samples that make it fast and easy for anyone to add skills to Alexa.
Developers have built more than 10,000 skills with ASK. Explore the stories behind some of these innovations, then start building your own skill. Once you publish your skill, mark the occasion with a free, limited-edition Alexa dev shirt. Quantities are limited.