Created
April 18, 2018 09:01
-
-
Save hanspeide/506a35d6cfde80fa1b97a1ef01b6cbf9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const app = new DialogflowApp({request: request, response: response}); | |
const INTENT_AVAILABILITY_USER_LOCATION = 'availability-user-location'; | |
const INTENT_AVAILABILITY_USER_LOCATION_PERMISSION_RESPONSE = 'availability-user-location.response'; | |
const ARG_ASSET_TYPE = 'asset_type'; | |
const actionMap = new Map(); | |
actionMap.set(INTENT_AVAILABILITY_USER_LOCATION, availabilityUserLocation); | |
actionMap.set(INTENT_AVAILABILITY_USER_LOCATION_PERMISSION_RESPONSE, availabilityUserLocationPermissionResponse) | |
app.handleRequest(actionMap); | |
function availabilityUserLocation(){ | |
let assetType = app.getArgument('assetType'); | |
let preciseLocationPermission = app.SupportedPermissions.DEVICE_PRECISE_LOCATION | |
if (app.isPermissionGranted(preciseLocationPermission)){ | |
getAvailabilityForUserLocation(app); | |
} else { | |
app.askForPermission(`To look for available ${assetType}s`, preciseLocationPermission); | |
} | |
} | |
function availabilityUserLocationPermissionResponse(){ | |
getAvailabilityForUserLocation(app); | |
} | |
function fallback(){ | |
saySimpleResponse("I'm sorry, I didn't quite get that. Please try again.") | |
} | |
function getAvailabilityForUserLocation(app){ | |
let assetType = app.getContextArgument('availability', 'assetType').value; | |
if (!app.isPermissionGranted(app.SupportedPermissions.DEVICE_PRECISE_LOCATION)){ | |
saySimpleResponse(`I unfortunately cannot find nearest available ${assetType}s without knowing your location.`, | |
`I need to know your location to find available ${assetType}s`); | |
return; | |
} | |
if (assetType != null){ | |
CityBikeAPI.getStationsSortedByDistance(app.getDeviceLocation().coordinates, assetType) | |
.then(stations => { | |
if (stations != null && stations.length > 0){ | |
let availability = (assetType == "bike" ? stations[0].availability.bikes : stations[0].availability.locks); | |
let speechResponse = `There are ${availability} ${assetType}s available at ${stations[0].title}, | |
${stations[0].distanceString} from you.` | |
let textResponse = `${availability} ${assetType}s available at ${stations[0].title}` | |
let googleResponse = app.buildRichResponse().addSimpleResponse({ | |
speech: speechResponse, | |
displayText: textResponse | |
}).addBasicCard(app.buildBasicCard(textResponse) | |
.setSubtitle(`${stations[0].distanceString} away`) | |
.setTitle(stations[0].title) | |
.setImage(getGoogleMapsUri(availability, stations[0].center), stations[0].title, 400, 200) | |
.addButton("Unlock bike", "https://someurl") | |
); | |
app.ask(googleResponse); | |
} else { | |
saySimpleResponse(`I'm sorry, but I wasn't able to find any available ${assetType}s at the moment.`, | |
`No available ${assetType}s at the moment.`); | |
} | |
}); | |
} else { | |
saySimpleResponse("I'm sorry, I didn't understand what you meant. Please try again.", | |
"I didn't understand. Please try again.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment