REST API Tutorials

Amazon Alexa Tutorial: Build Google Translate Skill

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. Amazon Alexa 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.

We’ll be working from this GitHub tutorial. Directory 01 is for you to edit and work in. If you get stuck, we’ve also created Directory o2 as a reference with the completed code.

This article will go through…

  1. Adding the translation skill to Amazon Alexa
  2. Creating the skill’s backend in AWS Lambda
  3. Connecting Alexa to the Google Translate API using RapidAPI

The result

The end result will look something like this…

What you’ll need:

  • Amazon Echo device
  • Amazon account

On that note, let the hacking begin!

Step 0: Clone our Repo on GitHub

Lucky for you, the first step is the easiest! Clone this repo on GitHub by pasting the following in your terminal: 

git clone https://github.com/RapidSoftwareSolutions/Alexa_Google_Translate_Skill.git

Step 1: Define the Skill’s Elements

1a: Create the skill

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.”

Here’s what those first steps look like.

1b. Define the intent

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:

{
    "intents": [
        {
            "intent" : "Translate",
            "slots": [
                {
                    "name" : "Source",
                    "type" : "SOURCE"
                },
                {
                    "name" : "Language",
                    "type" : "LANGUAGES_LIST"
                }
            ]
        },
       { 
          "intent": "LaunchRequest" 
        }
    ]
}

Copy and paste this code snippet into the intent schema area.

1c. Define the slots and slot types

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:

  • Source: the original word to be translated.
  • Language: the language we want to translate it to.

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.

For us, the slot types will look like this:


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).

1d. Define the utterances

The last part of the definition is the utterances. An utterance is an example phrase that the user can say to invoke this specific intent. In other words, utterances are the data Alexa uses to recognize commands. Our utterances will be pretty basic:

Translate translates {Source} to {Language} 
Translate ask Translate how to say {Source} in {Language} 
Translate ask Translate to translate {Source} to {Language} 
Translate get translation for {Source} in {Language} 
Translate what is {Source} in {Language}

We’ll copy/paste these utterances into “Sample Utterances,” the last section of the interaction model tab.

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 new skill to a ‘backend’ that will actually perform the translation.

Step 2: Connect to Amazon Lambda

In the Configuration tab, define your Service Endpoint to use AWS Lambda. AWS Lambda is a service that lets you write code that runs whenever an event (ex. an HTTP request or an AWS Alexa command) occurs. We will use Lambda in this tutorial so that we will not have to worry about setting up servers, load balancer etc.

2a. Create or login to AWS account

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.

2b. Add an Alexa Skills Kit trigger

In the console, type “Lambda” in the search bar to be directed to the Lambda service. Next, create a new blank Lambda function and click “Blank Function.” Add the trigger “Alexa Skills Kit” and click next. Here’s what that process looks like:

You should now be on the “Configure function” tab. Give your new Lambda function the name “ALEXA-TRANSLATE-SKILL”–the description is optional.  Since our code is in Node.js, keep the runtime at Node.js 4.3.  Next, set the  “Code entry type” to “Upload a .ZIP file,” as seen below.

.

2c. Install the Directory 01 to NPM

Before uploading the zip file, you’ll need to install all of the dependencies for this particular directory by doing the following:

  • Go to the Terminal and navigate to the directory 01
  • Run “npm install”

This step will allow Alexa to recognize the alexa-sdk dependency.

2d. Zip and upload the Directory 01 file to AWS

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. Note: directory 01 will not actually translate the response yet.  

If you haven’t cloned this repository, go ahead and clone it now.

  • 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.

2e. Fill in the Lambda Function and Role section

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.”

2f. Connect Lambda to your Alexa skill

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!

2g. Test your skill

In the Alexa developer portal, you can say/type inputs and see this output:

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.

Step 3: Add the Translation Functionality with RapidAPI

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:

3a. Connect to the Google Translate API with RapidAPI

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:

  1. Go to the projects page
  2. Select or create a Cloud Platform Console project.

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.

Nifty, am I right? Feel free to test out multiple calls/languages.

3b. Install RapidAPI into Directory 01

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:

 

const RapidAPI = new require('rapidapi-connect');
const rapid = new RapidAPI('PROJECT-NAME', 'RAPIDAPI APP ID');

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.

npm install rapidapi-connect --save

3c. Incorporate Google Translate snippet

We’re on the home stretch! Now, we’ll copy and paste the following code snippets into index.js.

First, put this language code object at the top of the Translate function. Your code should look like this:

'Translate': function() {
    const langCodes = {
        "German" : "de",
        "Dutch" : "nl",
        "English" : "en",
        "French" : "fr",
        "Italian" : "it",
        "Polish" : "pl",
        "Russian" : "ru",
        "Spanish" : "es"
    };

 

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:

var langCode = langCodes[language];

Next, grab the RapidAPI call and put it at the very bottom of the function.

rapid.call('GoogleTranslate', 'translateAutomatic', {
       'apiKey': 'Your Google API key',
        'string': word,
        'targetLanguage': langCode

    }).on('success', (payload) => {
      this.emit(":tell", `${word} is ${payload} in ${language}`);
    }).on('error', (payload) => {
      this.emit(":tell", "Sorry, translation was unsuccessful..");
    });

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.

3d. Compare your code to Directory 02

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.

3e. Compress the files and upload them to the Amazon Lambda function

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.”

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!

Congratulations–you’ve just built an Alexa skill! While this project used the Google Translate API, you can also build more skills with different APIs and a similar process. Here are some other Amazon Alexa skill ideas to keep you coding:

Comment with links to any of your projects and happy hacking!

 

 

 

4.9/5 - (189 votes)

View Comments

Share
Published by

Recent Posts

Power Up Your Enterprise Hub: New March Release Boosts Admin Capabilities and Streamlines Integrations

We're thrilled to announce the latest update to the Rapid Enterprise API Hub (version 2024.3)!…

3 weeks ago

Unveiling User Intent: How Search Term Insights Can Empower Your Enterprise API Hub

Are you curious about what your API consumers are searching for? Is your Hub effectively…

3 weeks ago

Rapid Enterprise API Hub Levels Up Custom Branding, Monetization, and Management in February Release

The RapidAPI team is excited to announce the February 2024 update (version 2024.2) for the…

1 month ago

Supercharge Your Enterprise Hub with January’s Release: Search Insights, Login Flexibility, and More!

This January's release brings exciting features and improvements designed to empower you and your developers.…

3 months ago

Enhanced Functionality and Improved User Experience with the Rapid API Enterprise Hub November 2023 Release

Rapid API is committed to providing its users with the best possible experience, and the…

5 months ago

The Power of Supporting Multiple API Gateways in an API Marketplace Platform

In today's fast-paced digital world, APIs (Application Programming Interfaces) have become the backbone of modern…

6 months ago