1

使用 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 版本

4

1 回答 1

1

如果您使用的是 Firebase Cloud Functions,则需要确保您使用的是付费计划之一。如果您使用“Spark”计划,FCF 仅允许网络连接到其他 Google 服务。您可以升级到按使用计费的“Blaze”计划,但包括一个足以满足大多数开发和轻度使用的免费层级。

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

您可能也值得考虑使用request-promise-native包而不是request. 请参阅https://stackoverflow.com/a/50441222/1405634上的 Stack Overflow 问题

于 2018-05-28T10:57:50.260 回答