Hardware components | ||||||
| × | 1 | ||||
Software apps and online services | ||||||
|
Story
Read moreSeeing this developer challenge, I decided to whip up a quick and simple Alexa skill. For the api, I recently came across a number api (http://numbersapi.com/#42 ) and thought it would be a perfect pair alongside the Amazon Skills Kit.
how it worksIt works by constantly listening for a call from the Alexa Voice Service. I used the node module express to set up a web server, and the module alexa-verifier to handle the authentication process required to publish the skill. I then deployed it to the cloud so it would constantly be listening. I used the node module request to access the number facts api, and then I just plugged the two together.
var express = require('express');
var bodyParser = require('body-parser');
var verifier = require('alexa-verifier');
var request = require('request');
var app = express();
app.use(function(req, res, next) {
if (!req.headers
.signaturecertchainurl
) {
return next();
}
req._body =
true;
req.rawBody =
'';
req.on('data',
function(
data
) {
return req
.rawBody +=
data;
}
);
req.on('end',
function() {
var
cert_url,
er,
error,
requestBody,
signature;
try {
req
.body =
JSON
.parse(
req
.rawBody
);
}
catch (
error
) {
er
=
error;
req
.body = {};
}
cert_url
=
req
.headers
.signaturecertchainurl;
signature
=
req
.headers
.signature;
requestBody
=
req
.rawBody;
verifier
(
cert_url,
signature,
requestBody,
function(
er
) {
if (
er
) {
console
.error(
'error validating the alexa cert:',
er
);
res
.status(
401
)
.json({
status: 'failure',
reason: er
});
}
else {
next
();
}
}
);
}
);
});
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.send(
"hello"
);
})
app.post('/', function(req, res) {
var data = {
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "Please work!"
},
"shouldEndSession": true
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": "Can I help you with anything else?"
}
},
"sessionAttributes": {}
}
var info = req.body
.request;
//handle different types of requests
if (info.type ==
'LaunchRequest'
) {
data.response
.outputSpeech
.text =
"Hey! Ask me for a number fact."
data.response
.shouldEndSession =
false;
res.send(
data
);
}
else if (info
.type ==
'SessionEndedRequest'
) {}
else if (
info.type ==
'IntentRequest'
) {
if (
info
.intent
.name ==
"AMAZON.HelpIntent"
) {
console
.log(
"help intent"
);
data
.response
.outputSpeech
.text =
"You can ask me for a random number fact."
data
.response
.shouldEndSession =
false;
res
.send(
data
);
}
else if (
info
.intent
.name ==
"AMAZON.CancelIntent" ||
info
.intent
.name ==
"AMAZON.StopIntent"
) {
console
.log(
"cancel intent"
);
data
.response
.outputSpeech
.text =
"Good-bye."
data
.response
.shouldEndSession =
true;
res
.send(
data
);
}
else if (
info
.intent
.name =
"GetFact"
) {
var
randnum =
Math
.floor(
Math
.random() *
10
) +
2;
request
(
"http://numbersapi.com/" +
randnum,
function(
error,
response,
body
) {
if (!
error &&
response
.statusCode ==
200
) {
var
fact =
body;
data
.response
.outputSpeech
.text =
fact
data
.response
.shouldEndSession =
true;
res
.send(
data
);
}
else {
data
.response
.outputSpeech
.text =
fact
data
.response
.shouldEndSession =
true;
res
.send(
"whoops! there was a problem getting the fact."
);
}
}
);
}
}
})
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log(
'test'
);
});
Comments