I am creating a bot on Dialogflow, and I am using Dialogflow-fulfillment for dynamic response and Firebase Real-time Database as a database.
What I am trying to do here is, suppose a hospital which provides certain facility has several centres(locations) in the country. Now when the user requests for such hospital, the bot should be able to output him the location of the nearby hospital. I do have the list of all such hospitals (with unique latitude & longitude) in Firebase Real-Time Database. My approach to this is that I will get the precise location of the user and compare it with locations available in the database to find the nearest hospital. Now I wish to output about this hospital to the user with extra information (like the address of the hospital, contact, website, opening hours, etc.) with the use of Google Place API (I have a billing account and API Key).
Although I am able to get the precise location of the user with longitude and latitude, I am facing some problems in dealing with Google Place API. Here are some errors that I am getting in "Firebase Logs":
Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail
and
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/var/tmp/worker/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/var/tmp/worker/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/var/tmp/worker/node_modules/express/lib/response.js:267:15)
at callPlaceAPI.then.catch (/user_code/index.js:480:34)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
I tried changing many different ways of initialising firebase-admin, but the above error remains the same. I did this:
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: "https://test-dia.firebaseio.com"
});
process.env.DEBUG = 'dialogflow:debug';
and even this:
dmin.initializeApp({
credential: admin.credential.cert({
"type": "service_account",
"project_id": "took from Firebase Generated Private Key",
"private_key_id": "took from Firebase Generated Private Key",
"private_key": "took from Firebase Generated Private Key",
"client_email": "took from Firebase Generated Private Key",
"client_id": "took from Firebase Generated Private Key",
"auth_uri": "took from Firebase Generated Private Key",
"token_uri": "took from Firebase Generated Private Key",
"auth_provider_x509_cert_url": "took from Firebase Generated Private Key",
"client_x509_cert_url": "took from Firebase Generated Private Key"
}),
databaseURL: "https://test-dia.firebaseio.com"
});
process.env.DEBUG = 'dialogflow:debug';
My package.json
is:
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "8"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.5.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.6.0"
}
}
Would you help me understand the problem? My index.js contains more than 600 lines of code, still, I am providing link to it for your consideration. Here: https://gist.github.com/shivam-k/49cfd05b36eb52d219f99b20cf285f03
Some important thing before you start reading the code:
- Code of your interest starts down the line 353. Because after that only, I am taking the location and making a call to Google Place API.
- The approach for parsing results that you had mentioned in the last email consists of two parts.
- Making a call from the main function to the function that contains parsed result. Though, I am still not getting any result.
- Making a call from the main function to the function that contains parsed result starts at line 451.
- The function which contains calling Place API and parsing results starts at line 499.
- JSON received ( I took it from the documentation for testing purpose) from the call made to testAPI is below.
Any suggestion on how to make a call to Place API and parse results from would be highly appreciated. Your any advice would be very encouraging to me.
{
"candidates" : [
{
"formatted_address" : "140 George St, The Rocks NSW 2000, Australia",
"name" : "Museum of Contemporary Art Australia",
"opening_hours" : {
"open_now" : false
},
"photos" : [
{
"height" : 3492,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/105784220914426417603/photos\"\u003eKeith Chung\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAA4fqUuhQuWUh11h_QEJJY5c14dM5V6tRjv9662oZdRxQdyvnmzK6b2ENgL9dtUb_bkSnIUTAzEmxdtJfLFQ1H_eqHDJkkiTedPu664OqkUOtxAfoBPCU01FOI1nN9RifJEhA47TjtbpLsT_yU6mfs8VZKGhRef9lr97rh2asJsCF3XBM-TH-InA",
"width" : 4656
}
],
"place_id" : "ChIJ68aBlEKuEmsRHUA9oME5Zh0"
}
],
"status" : "OK"
}