0

我的谷歌助手操作有点挣扎。现在我正在为我的 webhook 使用 Dialogflow 和 Firebase。在我的代码中,我想从 API 获取数据,例如这个:API。顺便说一句,我正在使用 Node.js 进行编码。由于节点是异步的,我不知道如何获取数据。当我尝试进行回调时,它不起作用,例如:

app.intent(GetData, (conv) => {
  var test= "error";

  apicaller.callApi(answer =>
    {
      test = answer.people[0].name
      go()

    })
    function go ()
    {
    conv.ask(`No matter what people tell you, words and ideas change the world ${test}`)
    }

出于某种原因,当我在其他应用程序中测试它时,它会起作用。使用 Dialogflow 它不起作用

我还尝试对函数 app.intent 使用 asynch 并使用 await 进行尝试,但这也不起作用。

你知道我该如何解决这个问题吗?

提前谢谢你和最好的问候

卢卡

4

4 回答 4

2

你需要像这样返回 Promise

function dialogflowHanlderWithRequest(agent) {
  return new Promise((resolve, reject) => {
    request.get(options, (error, response, body) => {
      JSON.parse(body)
      // processing code
      agent.add(...)
      resolve();
    });
  });
};

有关详细信息,请参见以下内容:

Dialogflow NodeJs Fulfillment V2 - webhook 方法调用在完成回调之前结束

于 2018-11-29T11:17:09.030 回答
1

If this works in another application then I believe you're getting an error because you're attempting to access an external resource while using Firebases free Spark plan, which limits you to Google services only. You will need to upgrade to the pay as you go Blaze plan to perform Outbound networking tasks.

于 2018-11-29T16:35:28.057 回答
0

首先按照他们的 Github 存储库中给出的过程

https://github.com/googleapis/nodejs-dialogflow

在这里,您可以找到 README 文件中已经给出的工作模块。您必须使keyFilename对象存储在 SessionsClient 对象创建中(在帖子末尾了解如何生成作为 keyFileName 的凭据文件)。在工作模块下方。

const express = require("express");

const bodyParser = require("body-parser");

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));

app.use(bodyParser.json());

//////////////////////////////////////////////////////////////////
const dialogflow = require("dialogflow");
const uuid = require("uuid");

/**
 * Send a query to the dialogflow agent, and return the query result.
 * @param {string} projectId The project to be used
 */
async function runSample(projectId = "<your project Id>") {
  // A unique identifier for the given session
  const sessionId = uuid.v4();

  // Create a new session
  const sessionClient = new dialogflow.SessionsClient({
    keyFilename: "<The_path_for_your_credentials_file>"
  });
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

  // The text query request.
  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        // The query to send to the dialogflow agent
        text: "new page",
        // The language used by the client (en-US)
        languageCode: "en-US"
      }
    }
  };

  // Send request and log result
  const responses = await sessionClient.detectIntent(request);
  console.log("Detected intent");
  console.log(responses);
  const result = responses[0].queryResult;
  console.log(`  Query: ${result.queryText}`);
  console.log(`  Response: ${result.fulfillmentText}`);
  if (result.intent) {
    console.log(`  Intent: ${result.intent.displayName}`);
  } else {
    console.log(`  No intent matched.`);
  }
}

runSample();

在这里,您可以使用 google 云平台获取 keyFileName 文件,该文件是凭据文件

https://cloud.google.com/docs/authentication/getting-started

如需完整教程(印地语),请观看 youtube 视频:

https://www.youtube.com/watch?v=aAtISTrb9n4&list=LL8ZkoggMm9re6KMO1IhXfkQ

于 2020-01-31T14:15:14.370 回答
0

由于异步性,该函数go()将在 callapi 的回调执行后被调用。

尽管您说您已尝试使用异步,但我建议进行以下更改,这些更改更有可能在您的场景中起作用:

app.intent(GetData, async (conv) => {
    var test= "error";

    apicaller.callApi(async answer =>
      {
        test = answer.people[0].name
        await go()

      })
      async function go ()
      {
      conv.ask(`No matter what people tell you, words and ideas change the world ${test}`)
      }
于 2018-11-29T11:15:21.200 回答