Skip to content

Instantly share code, notes, and snippets.

@KlavierCat
Created August 22, 2017 15:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KlavierCat/75a8d44c1d4ff4860ffb33c16b736f2a to your computer and use it in GitHub Desktop.
Save KlavierCat/75a8d44c1d4ff4860ffb33c16b736f2a to your computer and use it in GitHub Desktop.
My first working Alexa skill with AWS Lambda
/**
* user setting
**/
const favBus = '38D';
const buses = ['38A', '38'];
const Alexa = require('alexa-sdk');
const APP_ID = ''; // TODO replace with your app ID (OPTIONAL).
const sendRequest = (self) => {
var http = require("https");
var options = {
"method": "GET",
"hostname": "data.dublinked.ie",
"port": null,
"path": "/cgi-bin/rtpi/realtimebusinformation?stopid=1666&format=json",
"headers": {
"cache-control": "no-cache",
"postman-token": "a715bc18-b2a2-b710-8fbc-88c64239192d"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var result = body.toString();
console.log(result);
var res = parseResult(JSON.parse(result));
console.log(res);
self.emit(':tell', res);
});
});
req.end();
}
const parseResult = (res) => {
var otherBuses = '';
const resArray = res.results;
for (var i = 0; i < resArray.length; i++) {
var route = resArray[i].route;
if (route === favBus) {
return route + ' is ' + resArray[i].duetime + ' minutes' + ' away';
} else if (route === buses[0] || route === buses[1]) {
otherBuses += route + ' is ' + resArray[i].duetime + ' minutes ' + ' away. ';
}
}
if (otherBuses !== '') {
return otherBuses
}
return 'bus not in sign';
}
const getBusTime = () => {
sendRequest().then(function(result){
console.log('Dublin Bus API call result:', result);
if (!result) {
return 'bus not in sign';
} else {
return parseResult(result);
}
});
}
const handlers = {
'GetBusTime': function() {
sendRequest(this)
}
};
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
// alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment