I am making app for google assistant using dialog flow. My app uses a webhook function deployed on firebase. Main question is how to get user location - I need to pass the coordinates to a URL. I am able to prompt the message for location permission, but how to confirm and get coordinates.
Do I need to add any intent for confirmation? I am asking for location permission in default welcome intent.
The code is below:
'use strict';
process.env.DEBUG = 'actions-on-google:*';
const App = require('actions-on-google').DialogflowApp;
const functions = require('firebase-functions');
//----global variables-----
const http = require('https');
var body = "";
var body2="";
var address="";
var latitude="";
var longitude="";
var lati="";
var long="";
//------ADDRESS_ACTION to tell the current address based on current coordinates----
const ADDRESS_ACTION = 'address_action';
//-----DIRECTION_INTENT to output the map guidence through basic card------
const DIRECTION_INTENT = 'direction_action';
//--------during welcome intent, ask the permiisions------
const DIR_PERMISS_CONF= 'intent.welcome';
exports.addressMaker = functions.https.onRequest((request, response) => {
const app = new App({request, response});
console.log('Request headers: ' + JSON.stringify(request.headers));
console.log('Request body: ' + JSON.stringify(request.body));
function welcomeIntentDirection (app)
{
app.askForPermission("Hi! Welcome to !To show address/direction",app.SupportedPermissions.DEVICE_PRECISE_LOCATION);
if (app.isPermissionGranted())
{
// app.tell('You said ' + app.getRawInput());
}
else
{
// app.tell('You said ' + app.getRawInput());
}
}
function makeDirection(app)
{
//-------here we are extraction what user said, we will extract the place name, it is for "direction to PLACE_NAME"-------
var stringRequest=JSON.stringify(request.body);
var jsonRequest=JSON.parse(stringRequest);
var jsonSpeech=jsonRequest.originalRequest.data.inputs[0].rawInputs[0].query;
var arr=jsonSpeech.split("to");
var placeName=arr[1].trim();
//------Current Location--------------
let deviceCoordinates = app.getDeviceLocation().coordinates;
latitude=deviceCoordinates.latitude;
longitude=deviceCoordinates.longitude;
//-----now send this place name to api to get coordinates-----
var req2= http.get("https://api.url-with-PARAMETER-placeName",function(res){
res.on("data", function(chunk2){ body2 += chunk2; });
res.on('end', function()
{
if (res.statusCode === 200)
{
var data2 = JSON.parse(body2);
try
{
lati=data2.geometry.lat;
long=data2.geometry.lng;
}catch(e){app.tell("Invalid or non-existent address");}
}
});
});
//-------------------Here BUILDING the CARD (Rich RESPONSE)-----------------------
app.ask(app.buildRichResponse()
// Create a basic card and add it to the rich response
.addSimpleResponse('Your directions are here:')
.addBasicCard(app.buildBasicCard()
.addButton('Start Guidence', 'https://www.google.com/maps/dir/?api=1&origin='+latitude+','+longitude+'&destination='+lati+','+long+'')
.setImage('https://png.pngtree.com/element_origin_min_pic/16/11/30/a535f05d9d512610e25a036b50da036f.jpg', 'Image alternate text')
.setImageDisplay('CROPPED')
)
);
}
function makeAddress (app)
{
let deviceCoordinates = app.getDeviceLocation().coordinates;
latitude=deviceCoordinates.latitude;
longitude=deviceCoordinates.longitude;
var req=http.get("https://api.url/reverse?coords="+latitude+","+longitude+"",
function(res)
{
res.on("data", function(chunk){ body += chunk; });
res.on('end', function()
{
if (res.statusCode === 200)
{
try
{
var data = JSON.parse(body);
address=data.words;
app.tell('Alright, your address is '+address);
} catch (e) { }
}
else
{
app.tell("Unable to contact to server +"+res.statusCode);
}
});
});
}
// d. build an action map, which maps intent names to functions
let actionMap = new Map();
actionMap.set(DIR_PERMISS_CONF,welcomeIntentDirection);
actionMap.set(ADDRESS_ACTION, makeAddress);
actionMap.set(DIRECTION_INTENT, makeDirection);
app.handleRequest(actionMap);
});