Reputation: 1238
I made some API call as node.js and I checked it works well in my local.
So I tried to put into Dialogflow.
But It doesn't give any variable.
This is the errors.
Error: could not handle the request
I explain my procedure below.
[1]
This works well in my local with node.js.
It gives a value as ['aa','bb','cc',...,'dd']
const request = require('request');
const url = "https://datos.madrid.es/portal/site/egob/menuitem.ac61933d6ee3c31cae77ae7784f1a5a0/?vgnextoid=00149033f2201410VgnVCM100000171f5a0aRCRD&format=json&file=0&filename=201132-0-museos&mgmtid=118f2fdbecc63410VgnVCM1000000b205a0aRCRD&preview=full";
function information(){
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200)
{
}
var bd = body['@graph'];
var model = [];
var i = i;
for (i = 0; i < Object.keys(bd).length; i++)
{
model[i] = bd[i].title;
}
return model;
});
}
[2]
Therefore I made some code on inline editor to check it works well with this kind of value.
I attached a function most below in this code.
When I type 'hello', it says '1 , 2'. That works well.
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');
process.env.DEBUG = 'dialogflow:debug';
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 hello(agent) {
var model2 = information();
agent.add(model2);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
let intentMap = new Map();
intentMap.set('hello', hello);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
function information(){
return ['1','2'];
}
[3]
At last, I tried to do with my API call.
But It doesn't give any value.
It has every package moduel also.
I wonder why it doesn't work on same situation.
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');
const url = "https://datos.madrid.es/portal/site/egob/menuitem.ac61933d6ee3c31cae77ae7784f1a5a0/?vgnextoid=00149033f2201410VgnVCM100000171f5a0aRCRD&format=json&file=0&filename=201132-0-museos&mgmtid=118f2fdbecc63410VgnVCM1000000b205a0aRCRD&preview=full";
process.env.DEBUG = 'dialogflow:debug';
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 hello(agent) {
var model2 = information();
agent.add(model2);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
let intentMap = new Map();
intentMap.set('hello', hello);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
function information(){
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200)
{
}
var bd = body['@graph'];
var model = [];
var i = i;
for (i = 0; i < Object.keys(bd).length; i++)
{
model[i] = bd[i].title;
}
return model;
});
}
Upvotes: 1
Views: 796
Reputation: 1
You can make an app on heroku and send the fullfilment through to it, instead of using inclide code editor for google cloud.
Upvotes: 0
Reputation: 50741
You don't show the error you're getting, but there are likely two problems with what you're trying to do.
The first is that request
returns its information asynchronously, through a callback. The dialogflow-fulfillment library requires that any Intent Handler that does asynchronous operations return a Promise.
While you can wrap this in a Promise, even easier is to use a version of request
that will work directly with Promises, such as request-promise-native. (See, for example, https://stackoverflow.com/a/49751176/1405634)
Your other issue is that if you're using the built-in code editor for Dialogflow, you're working on top of Cloud Functions for Firebase. By default, network calls are only allowed inside Google's network. In order to access external addresses, you need to make sure you're using a paid plan such as the Blaze plan. There is, however, a free tier which is sufficient for development and most testing, and likely even light production.
Upvotes: 2