使用 nodejs 我正在尝试进行外部 Rest API 调用。我必须先拨打电话,然后为意图设置响应。
但是当我尝试这样做时,它希望在我从我的 API 获得响应之前设置一个响应。使用 V2 版本的 dialogflow 代码。
这是样本
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?',
})));
*/
我在默认欢迎意图中调用上述代码段。
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);
我已尝试使用节点版本 8.11.2 和 6.10 版本