Reputation: 287
I'm completely new to DialogFlow.. I wanted to create a chat bot that I can ask a question to and it would respond with a value retrieved from my Firebase Firestore database.
I've already created the necessary intent (GetPopulationInCity
) and selected Enable webhook call for this intent
Preferably I would like to use the DialogFlow Fulfillment alongside my other CloudFunction app.
I've used the code in the following example:
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function GetPopulationInCity(agent) {
//Search Firestore for value, if found =>
agent.add(`There are 10,000 people living in XXX`); //should it be ask or something like send or return?
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Get Population', GetPopulationInCity);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
but I have no clue how to create a handler for my intent and return a value. Is there anyone who could help me out?
Upvotes: 1
Views: 2150
Reputation: 50701
First of all, make sure the name of the Intent you're trying to write a handler for matches the name in the intentMap.set()
part. (In your description, you're not clear about what the intent name is in dialogflow vs the name of the function.)
The handler itself needs to do a few things:
Get the value of any parameters that may be set in the Intent.
You can get this from agent.parameters
.
Query the database for the values.
As part of the result handling, call agent.add()
with the results.
These would be done with code similar to how you're doing a Firebase call now. You haven't shown how you're doing this, or what the structure of your database is like, but assuming you're using the Firebase admin library, your webhook handler might look something like this:
function GetPopulationInCity( agent ){
var cityName = agent.parameters.cityName;
return db.collection('cities').doc(cityName).get()
.then( doc => {
var value = doc.data();
agent.add(`The population of ${cityName} is ${value.population}.`);
});
}
Finally, and more as an aside, your code raises the question of using add()
or ask()
.
You're using the Dialogflow fulfillment library, which (by convention) names the parameter sent to the handler "agent". The function to add a message type to the response is agent.add()
.
There is another library, the actions-on-google library, which works similarly and in conjunction with the dialogflow-fulfillment library. The convention of the a-o-g library is to pass a "conv" parameter with the conversation information. The function to add a response to an agent is conv.ask()
or conv.close()
.
To add some confusion, you can get an a-o-g "conversation" object from the dialogflow-fulfillment library by calling agent.conv()
if you are doing work with Actions (as opposed to one of the other agents that Dialogflow works with).
Upvotes: 2
Reputation: 63
I suggest taking a look at this action sample provided by Actions on Google: https://github.com/actions-on-google/dialogflow-updates-nodejs
Follow the steps in the Readme or just look at the functions/index.js file and you will see how the example handles Firestore.
Upvotes: 1