Reputation: 129
Using nodejs I am trying to make a external Rest API call. I have to first make a call and then set the response for the intent.
But when I am trying to do that it expects a Response to be set before i get the response from my API. Using the V2 version of dialogflow code.
here is the sample
var sText=" ";
console.log("Token in Welcome: "+conv.user.access.token);
var auth = "Bearer "+ conv.user.access.token;
var snQuery = "api/now/table/sys_user?sysparm_query=sys_id=javascript:gs.getUserID()";
var endpointUrl = baseURL +snQuery;
console.log("Endpoint user info: "+endpointUrl);
request.get({
url : endpointUrl,
headers : {
"Authorization" : auth,
"Accept": "application/json",
"Content-Type": "application/json"
}
},
function(error, response, body){
if(error)
{
sText=" Sorry! I found some Issue in performing this action, Please try again after some time. ";
console.log("Entering Error: "+error);
conv.close(sText);
}
else
{
console.log("Acccess token: "+conv.user.access.token);
conv.user.storage.UserDetails = JSON.parse(body);
console.log(conv.user.storage.UserDetails);
console.log("SYS ID_Welcome: "+conv.user.storage.UserDetails.result[0].sys_id);
return asyncTask()
.then(() =>conv.ask(new SimpleResponse({
speech: 'Howdy! tell you fun facts about almost any number, like 42. What do you have in mind?',
text: 'Howdy! tell you fun facts about almost any number, like 42. What do you have in mind?',
})));
}
})
//It is expecting my sample response here. Which would execute before my API call starts.
/*
return asyncTask()
.then(() =>conv.ask(new SimpleResponse({
speech: 'Howdy! tell you fun facts about almost any number, like 42. What do you have in mind?',
text: 'Howdy! tell you fun facts about almost any number, like 42. What do you have in mind?',
})));
*/
I am calling the above snippet in Default Welcome Intent.
const functions = require('firebase-functions');
process.env.DEBUG = 'actions-on-google:*';
const {dialogflow,SimpleResponse} = require('actions-on-google');
const request = require('request');
const app = dialogflow();
app.intent('Default Welcome Intent',getWelcomeMsg);
function getWelcomeMsg(conv)
{
// the above snippet goes here
}
exports.MyFunctionName = functions.https.onRequest(app);
I have tried with both node version 8.11.2 and 6.10 versions
Upvotes: 1
Views: 475
Reputation: 50701
If you are using Firebase Cloud Functions, you need to make sure you are on one of the paid plans. FCF allows network connections only to other Google services if you are on the "Spark" plan. You can upgrade to the "Blaze" plan which has per-use billing, but includes a free tier which is sufficient for most development and light usage.
However, you mention using Node version 8.11.2 and 6.10. Currently, FCF only supports Node version 6.14.
It may also be worthwhile for you to look into using the request-promise-native
package instead of request
. See the Stack Overflow question at https://stackoverflow.com/a/50441222/1405634
Upvotes: 1