We just got our hands on the Amazon Echo – a new Alexa-powered voice
assistant device from Amazon. Let’s see what we can do with it.
The Amazon Alexa platform is based on skills. Skills are the equivalent of
apps for this voice-powered platform. While there are countless skills we
could potentially build for Alexa, today we’ll focus on letting users translate
words using the Google Translate API.
The result
Lucky for you, the first step is the easiest! Clone this repo on GitHub by
pasting the following in your terminal:
Our new Skill will be a voice-powered tool for translating text. This skill
means that the user will be able to ask Alexa to translate words or phrases
from over 90 languages. For example, the user might say, Alexa translate
“butterfly” to German and Alexa will respond by saying, The word for
“butterfly” in German is Schmetterling. Pretty cool, right?
To get started, we’ll head over to the Amazon Developer Portal and login. If
you don’t yet have a developer account, the site will ask you to add some
information to your profile (ex. company name), accept a registration
agreement, and declare any financial purpose your app may serve (ex. if
you plan on monetizing your app).
Pro Tip: the account you use to log in here should be the same account
your device is configured with. That way, any skills that you build will
sync to your Echo automatically.
Once logged in, head over to the Alexa tab and click “Get Started” under
the Alexa Skills Kit block. Next, you’ll click the “Add a New Skill” button
near the header of the page.
The first step in creating a new skill is filling out some basic information in
the Skill Information tab. Set your skill’s language to English. The skill’s
name is what shows up in the developer dashboard, and the invocation
name is what you will actually say out loud when you ask Alexa to call the
skill. For ease of use, set your skill name and invocation name “Translate.”
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 2 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
The next step will be to define the skill’s intent schema on the Interaction
Model tab. Every skill in the Alexa platform is made of intents. An intent is
basically an action that the user can perform with the skill. Our skill will
have one intent – Translate. Thus, our definition will look like this:
Copy and paste this code snippet into the intent schema area.
As you can see in the intent schema code, the intent has two slots. A slot is
like a placeholder for a piece of data that the user can supply to the intent.
The slots in our case will be:
Each of the slots has slot name (“Source” and “Language”) and a slot type
(SOURCE and LANGUAGES_LIST). A slot type defines what information
can go into the slot. You define slot types by providing examples of words
that can go in the slot.
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 3 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
As you can see, the slot type values for SOURCE are sample words that you
may ask Alexa to translate (ex. airplane, butterfly, book etc.). The values
for LANGUAGES_LIST are the actual languages you’d like the phrase
translated into (ex. German, Dutch, Spanish).
Notice that our slots are represented in curly braces {}. These braces allow
Alexa to separate variables from static text when the user says a command.
The more utterances you define, the better Alexa will be at querying your
data. Now that we have the intent, its slots types, and its utterances set, we
can move on to configuring our Amazon skill. This step will connect our
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 4 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
Head over to AWS in a new tab and sign in to your console. The login
should be the same as your Amazon login from creating a skill. You may
have to re-enter some data if you do not already have an AWS account. It
will ask you for credit card information, but don’t worry. You won’t be
charged unless you go over one million free requests a month. Read more
information on AWS pricing if you have questions.
You should now be on the “Configure function” tab. Give your new Lambda
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 5 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
Before uploading the zip file, you’ll need to install all of the dependencies
for this particular directory by doing the following:
The code that we zip and upload here will be the code that runs every time
that we call an intent on Echo. We will upload a compressed file from
directory 01 in our GitHub repo. All this directory 01 code will do is reply
to the request by saying something like Translating **butterfly** to
German*. The reply will let us see that all the data is passed correctly.
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 6 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
Head over to the directory 01, compress its files and upload the
directory to the Lambda page.
Make sure to compress the files and the node_modules folder
and not the directory 01 folder overall, otherwise the skill will not
work.
Upload this zip file to the function package button. You don’t have to
enable encryption helpers nor add any environment variables for this
project.
After uploading the directory 01 zip file, move on to the Lambda function
and role section. For the role, just choose the “Create a custom role” option
and when sent to another page, choose “lambda_basic_execution” as the
IAM Role. You won’t need to create a new Role policy.
After you fill in your information, the Lambda function code section should
look like this.
Since this project doesn’t require any advanced settings, go ahead and click
“Next,” then click “Create function.”
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 7 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
To connect the Alexa skill to the Lambda function, simply copy the ARN
function identifier of the Lambda function and paste it in the
Configuration tab on the Amazon skill settings page (as shown below).
We have now gone through all the steps necessary to create a basic Alexa
skill! Woohoo!
In the Alexa developer portal, you can say/type inputs and see this output:
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 8 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
If you have an Amazon Alexa compatible device like the Amazon Echo, you
can try the skill on it. If you used the same account while configuring and
creating the skill, the Translate skill will show up automatically under Your
skills on the Alexa app.
If all goes well, you should get a result where the Alexa skill repeats exactly
what you are saying (ex. “Translate butterfly to German”) versus an actual
translation.
While this is all very exciting (we have an Alexa skill now!), our skill
doesn’t actually translate anything. Yet!
The next (and last step) will be to add the actual translation functionality.
We’ll use RapidAPI to connect to the Google Translate API. Here’s how it
works:
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 9 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
We’re using RapidAPI for two reasons: it lets you test and call APIs in the
browser and you can export the code snippet that calls the API directly into
the project.
Head over to RapidAPI’s Google Translate package. You’ll notice that this
API requires an API key. To get the Google Translate API key, do the
following:
3. Enable billing for your project. Don’t worry–you’ll get a thirty day free
trial and Google will not automatically charge you after the trial ends.
4. Click “Use Google APIs” and search for the Google Translate API.
5. Click the “Enable” button on the Google Translate API page.
6. Navigate to the credentials tab on the left of the screen to get an API key
(select API key when prompted). Note: If you have an existing API key,
you can use that key.
Now, you can test the call with RapidAPI. Go back to the Google Translate
package page, fill in your API key and parameters for an API call to test it
out for yourself.
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 10 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
In order to export the code snippet for the API call, you’ll need to get a
RapidAPI account. You can sign in with GitHub, Facebook, Google or a
custom email/password.
Once you sign up for RapidAPI, you will be prompted to create a new
project. Go ahead and name it RapidAPI Translate. The next window will
prompt you to pick a language. Leave the language setting at Node.js and
copy the two lines under “Require SDK.” They should look something like
this:
Open the index.js file. This is the code run by the Lambda function to
handle requests sent to the skill. At the top of the file, add the copied lines
to connect to RapidAPI.
Once you have the RapidAPI credentials in index.js file, navigate to the 01
directory and install the RapidAPI SDK by running the following line in the
command line.
We’re on the home stretch! Now, we’ll copy and paste the following code
snippets into index.js.
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 11 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
First, put this language code object at the top of the Translate function.
Your code should look like this:
Next, you’ll need to add a variable that can access the language codes from
the above object. Add this line directly beneath the object:
Next, grab the RapidAPI call and put it at the very bottom of the function.
So, what did we just do? Every time the Lambda function gets a request, it
call the exports.handler function (last function in the code). If the type of
the request is LaunchRequest (meaning the user is trying to get the skill to
perform an intent, “ask Translate”), the request will call the
“LaunchRequest” function. If the name of the intent is Translate (we
defined this as the only valid intent in our skill), the request will call the
“Translate” function. That’s the function we’ll use to perform the
translation.
If you followed the directions above, you should have a fully working
translation function! If you’re running into trouble, compare your directory
to the reference directory 02.
This is the final step! Moment of truth. Compress all the files in the
directory 01 (remember to compress the individual files vs. the directory 01
folder). Next, go back to Amazon Lambda and upload your compressed file
under “Lambda Function Code.”
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 12 of 13
Build an Amazon Alexa Project | RapidAPI Blog 12/24/17, 4(09 PM
When you test the function this time, it should have full translation
capabilities. No more repeating what you’ve said, Alexa should now be able
to translate your phrases.
Success!
Ask Alexa to check flight status with the Laminar Flight Data API
Prompt Alexa to send text messages with Twilio’s API (here’s a Twilio
tutorial)
Have Alexa check you in with the Foursquare API
http://blog.rapidapi.com/2016/10/07/alexa-api-project/ Page 13 of 13