In part two of this article series, we will learn how we can create an AWS Lambda function and integrate it with Alexa skills. We will also learn how we can call an external API within our AWS Lambda function and process each intent. Let's get started.
Before you go through this article I recommend going through part one of this article here:
Log on to your AWS Console and click Services and search for "Lambda" function.
Now, on your AWS Lambda Console, click "create a function" and select Blueprints. Search for Alexa Skills in the search bar and select sdk-factskill and click the link. We need to work on the basic Alexa Skill template and change the existing code.
Give a name to your AWS Lambda function and in Role, select "create a custom role".
In the new tab created, you will be creating a new role for your Lambda function. Give a name to your role and click Allow.
Now, you will be able to see the role which you have created. So select the role you created and click Create Function. We will modify the Lambda Function code once the Lambda function is created.
Make sure you are in the Northern Virginia zone when you are creating. Alexa Skills kit trigger, as AWS Lambda function is available there.
Before we deep dive into the code let's integrate our Alexa Skill to this AWS Lambda function. Integration is done by configuring two things.
- Letting your AWS Lambda function exist from where this Lambda function is called. So we configure triggers in AWS Lambda function configuration.
- Configuring Alexa Skill to which Lambda function we have to call.
So we have to use two ids for integration - Skill Id and AWS Lambda ARN
Skill Id can be found in Alexa Console,
- In Alexa Console get skill ID by clicking view Skill ID and add the skill ID in Trigger configuration and click ok.
- AWS Lambda ARN is found in Lambda Console on top of your Lambda function.
Part 1 of Integration
Once Lambda function is created you have to add triggers to your AWS Lambda function. Select Alexa Skills Kit from the left menu, so now you are able to add a trigger to your AWS Lambda function.
Now click Configuration Required in the trigger and provide your Alexa Skill Id and click Add.
Part 2 of Integration
In Alexa Console, click "View Alexa Skill" and click "Endpoint" in the Left Menu to configure the AWS Lambda function.
Select AWS Lambda ARN and provide the AWS Lambda ARN of the lambda function you have created in the default region.
Now that Integration of Alexa Skills with lambda function is done, let us look at the lambda function code and modify the code to process our Alexa Skill request.
I have used WORDSAPI to get synonyms for the words. Create an account in WordsAPI to use it in Lambda function. Once you signup they will provide an X-Mashape-Key which we use for authorization of words API.
In Lambda function we use Alexa SDK and https node js modules.
- constAlexa = require('alexa-sdk');
- varhttp = require('https');
We have to create handlers for Alexa using Alexa SDK. In the handlers object, we have to write logic for each intent which we have created in our Alexa Skill. By default, we have code execution for Amazon-defined Intents like AMAZON, HelpIntent, etc.
- consthandlers = {
- 'LaunchRequest': function() {
- this.emit('GetNewFactIntent');
- },
- 'AMAZON.HelpIntent': function() {
- const speechOutput = 'You can ask me a word, I can help you with synonyms of the word';
- const reprompt = 'What can I help you with?';
- this.response.speak(speechOutput).listen(reprompt);
- this.emit(':responseReady');
- },
- 'AMAZON.CancelIntent': function() {
- this.response.speak(STOP_MESSAGE);
- this.emit(':responseReady');
- },
- 'AMAZON.StopIntent': function() {
- this.response.speak(STOP_MESSAGE);
- this.emit(':responseReady');
- },
- };
We do have some logic that gets executed for each Intent. Let us look at the logic for Help Intent.
- this.response.speak(speechOutput).listen(reprompt);
Here speaking the method within the response object will let Alexa Skills know that whatever is sent as a parameter has to be read to the end user.
The Listen method within the response object will make Alexa Skills wait for a response from the user for some time and keep the session active until the user responds.
We do also have a Launch Request Intent which gets invoked when the user says something like "Alexa, start Synonyms Finder", code within Launch the request intent gets executed. For LaunchRequest we have to maintain the session so we use the listen method.
- 'LaunchRequest': function() {
- this.response.speak('Welcome to Synonyms Finder. Know synonyms for the words you want').listen();
- this.emit(':responseReady');
- }
We are using https module to fetch data from Words API. Let us create a httpGet method within Lambda function where we will call the WordsAPI.
- functionhttpGet(query, callback) {
- var options = {
-
- host: 'wordsapiv1.p.mashape.com',
- path: '/words/' + encodeURIComponent(query) + '/synonyms',
- method: 'GET',
- headers: {
- "X-Mashape-Key": "Your MaSH api kEY"
- }
- };
- var req = http.request(options, res => {
- setEncoding('utf8');
- var responseString = "";
-
- on('data', chunk => {
- responseString = responseString + chunk;
- });
-
- on('end', () => {
- if (responseString != '') {
-
- if (!responseString.includes("word not found")) callback(JSON.parse(responseString));
- else callback(null);
- } else {
- callback(null);
- }
- });
- });
- end();
- }
We will use the above httpGet during SynonymsIntent invocation. So let's create a Synonyms Intent function within our Handlers object.
Let us first see what we will be coding within Synonyms Intent when a user asks "Synonyms of {word}" where word is a slot which we have created in part 1 of the article. So in Synonyms Intent function, we have to access the word slot, call the words API, get the response from Words API and format the response and use this.response.speak to return the message back to the user.
To access the slot within synonyms intent, we use event.request object.
- var word=this.event.request.intent.slots.word.value;
We can have validation checks like null and multiple words in slots for words and different responses based on the validation failure. So for invalid slot words we should maintain an active session, so we have to reprompt the user using listen method in the response object.
- 'Synonyms': function() {
- var word = this.event.request.intent.slots.word.value;
- if (word == undefined || word == null) {
- this.response.speak('You havent spoken the word you want synonyms for.').listen('Can you say it again');
- this.emit(':responseReady');
- return;
- }
- console.log(word);
- if (word.split(' ').length > 1) {
- this.response.speak('Problem with word you have asked. Ask a single word').listen('Can you say it again');
- this.emit(':responseReady');
- return;
- }
Now we call httpGet method by passing word slot and then process the response by splitting the json array of synonyms as a string message such as, Synonyms of word are ...... Here we have restricted the number of synonyms to five.
- httpGet(word, (theResult) => {
- if (theResult === null) {
- speech = 'Sorry! No Synonyms Found, Please try again';
- this.response.speak(speech).listen('Sorry I Didnt Get you.');
- this.emit(':responseReady');
- } else {
- var theFact = theResult.synonyms;
- console.log(theFact);
- if (theFact.length == 0) {
- speech = 'Sorry! No Synonyms Found, Please try again';
- this.response.speak(speech).listen('Sorry I Didnt Get you.');
- this.emit(':responseReady');
- return;
- }
- var speech = "The Synonyms for the word " + word + " are ";
- for (vari = 0; i < theFact.length && i < 5; i++) {
- speech = speech + theFact[i] + ", ";
- }
- }
- this.response.speak(speech);
- this.emit(':responseReady');
- });
- }
So far we have just created handler objects which we use to process the intents and we haven't used the alexa-sdk which we imported yet.
We create an Alexa handler and register handler objects which we created and then execute it.
- handler = function(event, context, callback) {
- const alexa = Alexa.handler(event, context, callback);
- alexa.APP_ID = APP_ID;
- alexa.registerHandlers(handlers);
- alexa.execute();
- };
Now that we are done with the code part of AWS Lambda function click save. We have already integrated our Alexa skill with AWS Lambda function. Now let us test the skill in Alexa simulator in Alexa Console.
Click Test in Skills console and test the skill in Alexa simulator. And it works fine.
You can even test the skill by providing a sample request as json using Manual JSON.
That's it for this article. You can create Alexa skills of your own by using different APIs based on the skill. Comment with your questions, if any, I am happy to help.