Dialogflow actions-on-google:在设置响应之前无法进行Rest api调用

时间:2018-05-25 14:09:40

标签: node.js actions-on-google dialogflow servicenow-rest-api

使用nodejs我正在尝试进行外部Rest API调用。我必须先打电话然后设置意图的响应。

但是当我尝试这样做时,它希望在我从我的API获得响应之前设置响应。 使用V2版的对话框流程代码。

这是样本

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?',
    })));
*/

我在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);

我尝试过使用节点版本8.11.2和6.10版本

1 个答案:

答案 0 :(得分:1)

如果您使用的是Firebase云功能,则需要确保自己位于paid plans之一。如果您使用的话,FCF仅允许与其他Google服务建立网络连接#Spark;#34;计划。你可以升级到" Blaze"具有按使用费用计划,但包含足以满足大多数开发和轻量使用的免费套餐。

但是,您提到使用Node版本8.11.2和6.10。目前,FCF仅支持节点版本6.14。

您也可以考虑使用request-promise-native包而不是request。请参阅https://stackoverflow.com/a/50441222/1405634

上的Stack Overflow问题