Created
April 29, 2017 18:28
-
-
Save joshafeinberg/72263442c5b967f7131661b7635fd071 to your computer and use it in GitHub Desktop.
CTA Bus Lookup for API.AI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2017 Josh Feinberg | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
'use strict'; | |
process.env.DEBUG = 'actions-on-google:*'; | |
const Assistant = require('actions-on-google').ApiAiAssistant; | |
const http = require("http"); | |
const apiKey = "XXXXXXXXXXXXXXXXX"; | |
const defaultBusStopId = "XXXX"; | |
exports.findBus = (req, res) => { | |
const assistant = new Assistant({ request: req, response: res }); | |
function responseHandler(assistant) { | |
var busNumber = assistant.getArgument('busnumber'); | |
var busDirection = assistant.getArgument('direction').toLowerCase(); | |
http.get('http://www.ctabustracker.com/bustime/api/v2/getpredictions?key=' + apiKey + '&stpid='+ defaultBusStopId + '&rt=' + busNumber + '&format=json', (res) => { | |
res.setEncoding('utf8'); | |
var rawData = ''; | |
res.on('data', (chunk) => { rawData += chunk; }); | |
res.on('end', () => { | |
try { | |
parseData(rawData, busNumber, busDirection); | |
} catch (e) { | |
assistant.ask("Sorry, I was unable to load bus information. Please try again.") | |
console.error("error: " + e.message); | |
} | |
}); | |
}) | |
} | |
/** | |
* parses the respond from the API and determines a response | |
* @param rawData the raw data from the HTTP response | |
* @param busNumber the bus number the user is looking for | |
* @param busDirection the bus direction the user is looking for | |
*/ | |
function parseData(rawData, busNumber, busDirection) { | |
const parsedData = JSON.parse(rawData); | |
var response = parsedData['bustime-response']; | |
if (response.error != null) { | |
noBusses(busNumber); | |
} | |
var prd = response.prd; // array of preditions | |
var predictionsCount = prd.length; | |
if (predictionsCount == 0) { | |
noBusses(busNumber); | |
} else { | |
var busFound = findBus(busNumber, busDirection, prd, predictionsCount); | |
if (!busFound) { | |
noBusses(busNumber); | |
} | |
} | |
} | |
/** | |
* finds the proper bus and informs the user of the arrival time (in minutes) | |
* @param busNumber the bus number the user is looking for | |
* @param busDirection the bus direction the user is looking for | |
* @param prd the JSON array of predictions | |
* @param predictionsCount the amount of predictions that were returned | |
* @return if a bus was found | |
*/ | |
function findBus(busNumber, busDirection, prd, predictionsCount) { | |
var busFound = false; | |
for (var i = 0; i < predictionsCount; i++) { | |
var routeDirection = prd[i].rtdir.toLowerCase(); | |
if (routeDirection.localeCompare(busDirection) == 0) { | |
var prediction = prd[i].prdctdn; | |
if (prediction == "DUE") { | |
assistant.tell("Quick, the " + busNumber + " is here!") | |
} else { | |
assistant.ask("<speak>The next <say-as interpret-as=\"cardinal\">" + busNumber + "</say-as> will arrive in " + prd[i].prdctdn + " minutes. Would you like to find another?</speak>") | |
} | |
busFound = true; | |
break; | |
} | |
} | |
return busFound; | |
} | |
/** | |
* alerts that no busses were found and asks if the user would like a different route | |
* @param busNumber the bus number the user is looking for | |
*/ | |
function noBusses(busNumber) { | |
assistant.ask("<speak>It does not appear there are any <say-as interpret-as=\"cardinal\">" + busNumber + "</say-as> buses on the way, would you like to try another route?</speak>") | |
} | |
const actionMap = new Map(); | |
actionMap.set('bus-requested', responseHandler); | |
assistant.handleRequest(actionMap); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "get-cta-bus", | |
"version": "1.0.0", | |
"description": "API.AI Bus Lookup", | |
"author": "Josh Feinberg <josh@joshafeinberg.com>", | |
"license": "Apache-2.0", | |
"dependencies": { | |
"actions-on-google": "^1.0.9" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment