Created
December 28, 2018 16:50
-
-
Save shivam-k/4eac613181e4957d81fe4f66bd05a70d 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
'use strict'; | |
const {WebhookClient} = require('dialogflow-fulfillment'); | |
const { | |
Permission, | |
dialogflow, | |
Image, | |
BasicCard, | |
BrowseCarousel, | |
BrowseCarouselItem, | |
Button, | |
Carousel, | |
LinkOutSuggestion, | |
List, | |
MediaObject, | |
Suggestions, | |
SimpleResponse, | |
Table, | |
} = require('actions-on-google'); | |
const host = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json?'; | |
const apiKey = 'my key goes here'; | |
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const app = dialogflow({debug: true}); | |
// initialise DB connection | |
admin.initializeApp({ | |
credential: admin.credential.applicationDefault(), | |
databaseURL: 'https://mcertificate-27106.firebaseio.com/', | |
}); | |
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements | |
app.intent('ask_location', (conv) => { | |
// Choose one or more supported permissions to request: | |
// NAME, DEVICE_PRECISE_LOCATION, DEVICE_COARSE_LOCATION | |
const options = { | |
context: 'To help you find a nearby place where you can get your medical certificate made', | |
// Ask for more than one permission. User can authorize all or none. | |
permissions: ['DEVICE_PRECISE_LOCATION'], | |
}; | |
conv.ask(new Permission(options)); | |
}); | |
app.intent('receive_location', (conv, params, permissionGranted) => { | |
const {coordinates} = conv.device.location; | |
const longitude = conv.device.location.coordinates.longitude; | |
const latitude = conv.device.location.coordinates.latitude; | |
if (permissionGranted) { | |
if (coordinates) { | |
//conv.ask(`Your location is longitude: ` + longitude + ` and latitude: ` + latitude); | |
return admin.database().ref('hospitals/').once("value").then((snapshot) => { | |
var count = 0; | |
var temp = ""; | |
var clong = ""; | |
var clat = ""; | |
var countcell = ""; //keep the cell number | |
var distance = ""; | |
var tempdistance = ""; | |
while(snapshot.child(count).val()){ | |
clong = snapshot.child(count + '/Longitude').val(); | |
clat = snapshot.child(count + '/Latitude').val(); | |
tempdistance = Math.pow((longitude-clong), 2) + Math.pow((latitude-clat), 2); | |
tempdistance = Math.sqrt(tempdistance); | |
if(!distance){ | |
distance = tempdistance; | |
} | |
if(tempdistance < distance){ | |
distance = tempdistance; | |
countcell = count; | |
} | |
count = count + 1; | |
} | |
var nstate = snapshot.child(countcell + '/State Name').val(); | |
var ndistrict = snapshot.child(countcell + '/District Name').val(); | |
var nsubdistrict = snapshot.child(countcell + '/Subdistrict Name').val(); | |
var bname = snapshot.child(countcell + '/Facility Name').val(); | |
var nlongitude = snapshot.child(countcell + '/Longitude').val(); | |
var nlatitude = snapshot.child(countcell + '/Latitude').val(); | |
//conv.ask('Min Distance: ' + distance + ', State: ' + nstate + ', District: ' + ndistrict + ', Longitude: ' + nlogitude + ', Latitude: ' + nlatitude); | |
var urlmap = 'https://www.google.com/maps/dir/?api=1&origin=' + latitude + ', ' + longitude + '&destination=' + nlatitude + ', ' + nlongitude + '&travelmode=walking'; | |
//API Works | |
var query = 'Museum%20of%20Contemporary%20Art%20Australia&inputtype=textquery&fields=photos,formatted_address,name'; | |
var data = host + 'query=' + query + '&key=' + apiKey; | |
//Basic Card - Check Screen Support | |
if (!conv.surface.capabilities.has('actions.capability.SCREEN_OUTPUT')) { | |
conv.ask('Sorry, try this on a screen device or select the ' + | |
'phone surface in the simulator.'); | |
return; | |
} | |
conv.ask(bname + ' is the nearest place to get your medical certificate made.'); | |
conv.ask(new SimpleResponse({ | |
speech: 'I will tell you the distance to the destination.', | |
text: 'distance', | |
})); | |
conv.ask(new Suggestions('Call')); | |
// Create a basic card | |
conv.ask(new BasicCard({ | |
text: `Address: to come`, | |
subtitle: nsubdistrict + ', ' + ndistrict + ', ' + nstate, | |
title: bname, | |
buttons: new Button({ | |
title: 'Open on Google Map', | |
url: urlmap, | |
}), | |
image: new Image({ | |
url: 'https://maps.googleapis.com/maps/api/staticmap?center=' + nlatitude + ',' + nlongitude + '&zoom=15&size=750x500&markers=color:red%7Clabel:S%7C' + nlatitude + ',' + nlongitude + '&key=I have given my API key here', | |
alt: 'Image alternate text', | |
}), | |
display: 'CROPPED', | |
})); | |
}); //end return | |
} | |
else{ | |
return conv.close('Sorry, I could not figure out where you are.'); | |
} | |
} | |
else { | |
return conv.ask('Sorry, permission denied.'); | |
} | |
}); | |
app.intent('Default Welcome Intent', (conv) => { | |
conv.ask('Hello! How can I help you today?'); | |
}); | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment