Я пытаюсь создать одно диалоговое окно Fullfilment, используя скрипт node.js.Я читаю JSON с Google Firebase URL, я могу проанализировать необходимые данные.Однако я не могу отобразить данные JSON в методе agent.add (). Также я не могу использовать здесь метод java async / await, так как я использую node.js 6, который в настоящее время не поддерживается.Я добавил комментарий в код ниже, чтобы показать, какая строка не выполняется.
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const requestNode = require('request');
const NUMBER_ARGUMENT = 'geo-city';
var importedJSON;
process.env.DEBUG = 'dialogflow:debug';
var arv;
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 getData(geocity) {
var url1 = 'https://xxxx.firebaseio.com/temp.json';
var options = {
url: url1,
headers: {
'Content-Type': 'application/json'
}
};
return new Promise(function(resolve, reject) {
requestNode(options, function(error, requestInternal, body){
if (error) {
reject(error);
} else {
importedJSON = JSON.parse(body);
console.log(importedJSON);
console.log(importedJSON[geocity]);
arv = importedJSON[geocity];
resolve(arv);
}
console.log(body + ':' + arv);
});
});
}
var errHandler = function(error) {
console.log(error);
};
function getthedata(agent) {
let geocity = request.body.queryResult.parameters[NUMBER_ARGUMENT];
var getdata = getData(geocity);
getdata.then(function(result,agent) {
arv = result;
console.log("JSON received data is:");
console.log(arv);
agent.add(`Received value is ` + arv); // this is not getting executed
}, function(error) {
console.log(error);
});
agent.add(`Received value is ` + arv); // this is getting executed
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Property rate', getthedata);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
I am expecting all the below lines to be executed from the above code.
getdata.then(function(result,agent) {
arv = result;
console.log("JSON received data is:");
console.log(arv);
agent.add(`Received value is ` + arv); // this is not getting executed