Coinmonks
Published in

Coinmonks

Develop Alexa Custom Skill with AWS Lambda to Control IoT Device

Figure 1 : Amazon Echo Dot
Figure 2 : Solution Design
Figure 3 : Create Skill
Figure 4 : Skill options
Figure 5 : Invocation
Figure 6 : Create new Intent
Figure 7 : Utterances
Figure 8 : Create new slot
Figure 9 : Create custom slot type
Figure 10 : Custom slots
Figure 11 : Crete AWS Lambda function
// These are the helper functions 
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
return {
outputSpeech: {
type: 'PlainText',
text: output,
},
card: {
type: 'Simple',
title: `SessionSpeechlet - ${title}`,
content: `SessionSpeechlet - ${output}`,
},
reprompt: {
outputSpeech: {
type: 'PlainText',
text: repromptText,
},
},
shouldEndSession,
};
}
function buildResponse(sessionAttributes, speechletResponse) {
return {
version: '1.0',
sessionAttributes,
response: speechletResponse,
};
}
function onSessionStarted(sessionStartedRequest, session) {
console.log(`onSessionStarted requestId=${sessionStartedRequest.requestId}, sessionId=${session.sessionId}`);
}

function onLaunch(launchRequest, session, callback) {
console.log(`onLaunch requestId=${launchRequest.requestId}, sessionId=${session.sessionId}`);
// Dispatch to your skill's launch.
getWelcomeResponse(callback);
}

function getWelcomeResponse(callback) {
const sessionAttributes = {};
const cardTitle = 'Welcome';
const speechOutput = 'Welcome to the light demo ' ;

const repromptText = 'You can say: what switch to control ' ;
const shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
function handleSessionEndRequest(callback) {
const cardTitle = 'Session Ended';
const speechOutput = 'Thank you for using light control , have a great day!';
const shouldEndSession = true;
callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}


function onSessionEnded(sessionEndedRequest, session) {
console.log(`onSessionEnded requestId=${sessionEndedRequest.requestId}, sessionId=${session.sessionId}`);
// Add cleanup logic here
}
function onIntent(intentRequest, session, callback) {
console.log(`onIntent requestId=${intentRequest.requestId}, sessionId=${session.sessionId}`);
const intent = intentRequest.intent;
const intentName = intentRequest.intent.name;
// Dispatch to your skill's intent handlers
if (intentName === 'AMAZON.HelpIntent') {
getWelcomeResponse(callback);
} else if (intentName === 'AMAZON.StopIntent') {
handleSessionEndRequest(callback);
} else if (intentName === 'lightcontrol') {
var color = intent.slots.color.value;
var lightstatus = intent.slots.lightstatus.value;
lights(callback,color,lightstatus);
}
}
function lights(callback,color,lightstatus) {

var _switch = "";
var _status = "";

if(color == "red")
_switch = "V1";
else if(color == "green")
_switch = "V2";
else if(color == "orange")
_switch = "V0";
else
_switch = "error";

if(lightstatus == "on")
_status = "1";
else if(lightstatus == "off")
_status = "0";

var endpoint = "http://<myip>:8080/<myport>/update/"+_switch+"?value="+_status;
var status ="offline";
var body = "";
http.get(endpoint, (response) => {
console.log("Got response: " + response.statusCode)
response.on('data', (chunk) => { body += chunk })
response.on('end', () => {
})
});

const sessionAttributes = {};

//Get card title from data
const cardTitle = 'device status';

//Get output from data
const speechOutput = 'The ' + color + ' light is turned '+ lightstatus;
const repromptText = '' ;
const shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
Figure 12 : Function test script
{
"session": {
"new": false,
"sessionId": "amzn1.echo-api.session.[unique-value-here]",
"attributes": {},
"user": {
"userId": "amzn1.ask.account.[unique-value-here]"
},
"application": {
"applicationId": "amzn1.ask.skill.[unique-value-here]"
}
},
"version": "1.0",
"request": {
"locale": "en-US",
"timestamp": "2016-10-27T21:06:28Z",
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.[unique-value-here]",
"intent": {
"slots": {
"color": {
"name": "color",
"value": "red"
},
"lightstatus": {
"name": "lightstatus",
"value": "on"
}
},
"name": "lightcontrol"
}
},
"context": {
"AudioPlayer": {
"playerActivity": "IDLE"
},
"System": {
"device": {
"supportedInterfaces": {
"AudioPlayer": {}
}
},
"application": {
"applicationId": "amzn1.ask.skill.[unique-value-here]"
},
"user": {
"userId": "amzn1.ask.account.[unique-value-here]"
}
}
}
}
Figure 13 : Test results
Figure 14 : Copy the ARN value
Figure 15 : Config the ARN in the custom skill
Figure 16 : Add the custom skill to Lambda function
Figure 17 : Config the Skill Id
Figure 18 : Custom skill’s testing console
  1. By Voice
  2. 2. By Text
Figure 19 : Testing the invocation
Figure 20 : Test the invocation

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--

Coinmonks (http://coinmonks.io/) is a non-profit Crypto Educational Publication. Follow us on Twitter @coinmonks and Our other project —  https://coincodecap.com, Email  — gaurav@coincodecap.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Priyal Walpita

CTO @ ZorroSign | Seasoned Software Architect | Expertise in AI/ML , Blockchain , Distributed Systems and IoT | Lecturer | Speaker | Blogger