Skip to content

Instantly share code, notes, and snippets.

@antonydandrea
Created March 9, 2017 19:58
Show Gist options
  • Save antonydandrea/1bdcb4f709678865be68e9f309b053f2 to your computer and use it in GitHub Desktop.
Save antonydandrea/1bdcb4f709678865be68e9f309b053f2 to your computer and use it in GitHub Desktop.
Amazon Echo I.S.S. Tracker Skill
'use strict';
var http = require('http');
const Alexa = require('alexa-sdk');
var alexa;
const handlers = {
'LaunchRequest': function () {
this.emit('GetLocation');
},
'GetLocationIntent': function () {
this.emit('GetLocation');
},
'GetLocation': function () {
var speechOutput = '';
var cardOutput = '';
httpGet('api.open-notify.org', '/iss-now.json', function (response) {
var responseData = JSON.parse(response);
if (responseData === null) {
speechOutput = 'I have no idea where it is.';
cardOutput = 'There was an error.'
} else {
speechOutput = 'I have found it. It is at latitude ' + responseData.iss_position.latitude + ', and longitude ' + responseData.iss_position.longitude;
cardOutput = responseData.iss_position.latitude + ', ' + responseData.iss_position.longitude;
}
alexa.emit(':tellWithCard', speechOutput, 'I.S.S. Current Co-ordinates - ' + new Date(responseData.timestamp * 1e3).toISOString(), cardOutput);
});
},
'AMAZON.HelpIntent': function () {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('HELP_MESSAGE');
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'AMAZON.StopIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'SessionEndedRequest': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
};
exports.handler = (event, context) => {
alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};
function httpGet(location, file, callback) {
var options = {
host: location,
path: file,
method: 'GET'
};
var req = http.request(options, (res) => {
var body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', function () {
callback(body);
});
});
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment