Access public API

Based on an example from

https://dabblelab.com/

Get the example from and view the video

https://dabblelab.com/templates/2-alexa-remote-api-example-skill

Download the template to a local directory. Since this example is v1 version of Alexa code you need to upgrade to v2

cd to where the template was downloaded. ask deploy fails because the example is v1 in the 2017 example.

$ ask deploy
[Warn]: File /home/allen/Alexa_skills/2-alexa-remote-api-example-skill-master/lambda/ask-resources.json not exists. If this is a skill project managed by v1 ask-cli, please run ‘ask util upgrade-project’ then try the command again.

$ ask util upgrade-project
Template project migration finished.

The ask-resources.json file exists only in the latest version 

$ ask deploy
Deploy configuration loaded from ask-resources.json
Deploy project for profile [default]

==================== Deploy Skill Metadata ====================
Skill package deployed successfully.
Skill ID: amzn1.ask.skill.cb3a2bb7-0c55-45e3-a5ff-a40734696ed3

==================== Build Skill Code ====================

added 3 packages, and audited 4 packages in 627ms

found 0 vulnerabilities
Skill code built successfully.
Code for region default built to /home/allen/Alexa_skills/2-alexa-remote-api-example-skill-master/.ask/lambda/custom/build.zip successfully with build flow NodeJsNpmBuildFlow.

==================== Deploy Skill Infrastructure ====================
✔ Deploy Alexa skill infrastructure for region “default”
The api endpoints of skill.json have been updated from the skill infrastructure deploy results.
Skill infrastructures deployed successfully through @ask-cli/lambda-deployer.

==================== Enable Skill ====================
Skill is enabled successfully.

ask deploy uploads the skill from a local directory into Alexa/AWS

 

 

A run in Alexa

 

Access takes place in lambda/index.js to access the API Astronauts currently in space.

The API is http://api.open-notify.org/astros.json

Returns this json text
{“number”: 7, “message”: “success”, “people”: [{“name”: “Mark Vande Hei”, “craft”: “ISS”},
{“name”: “Oleg Novitskiy”, “craft”: “ISS”}, {“name”: “Pyotr Dubrov”, “craft”: “ISS”},
{“name”: “Thomas Pesquet”, “craft”: “ISS”}, {“name”: “Megan McArthur”, “craft”: “ISS”},
{“name”: “Shane Kimbrough”, “craft”: “ISS”}, {“name”: “Akihiko Hoshide”, “craft”: “ISS”}]}

This function reads a list of Astronauts in space from the public API above and formats the text.

const GetRemoteDataHandler = {
canHandle(handlerInput) {
returnhandlerInput.requestEnvelope.request.type === ‘LaunchRequest’
|| (handlerInput.requestEnvelope.request.type === ‘IntentRequest’
&& handlerInput.requestEnvelope.request.intent.name === ‘GetRemoteDataIntent’);
},
asynchandle(handlerInput) {
letoutputSpeech = ‘This is the default message.’;
awaitgetRemoteData(‘http://api.open-notify.org/astros.json’)
.then((response) => {
constdata = JSON.parse(response);
outputSpeech = `There are currently ${data.people.length} astronauts in space. `;
for (leti = 0; i < data.people.length; i += 1) {
if (i === 0) {
// first record
outputSpeech = `${outputSpeech}Their names are: ${data.people[i].name}, `;
} else if (i === data.people.length – 1) {
// last record
outputSpeech = `${outputSpeech}and ${data.people[i].name}.`;
} else {
// middle record(s)
outputSpeech = `${outputSpeech + data.people[i].name}, `;
}
}
})
.catch((err) => {
console.log(`ERROR: ${err.message}`);
// set an optional error message here
// outputSpeech = err.message;
});
returnhandlerInput.responseBuilder
.speak(outputSpeech)
.getResponse();
},
};

 

Logging can be seen in the Alexa Console page, check the box

Log output