从意向触发履行网络挂接异步?

问题描述 投票:1回答:1

我有一些意图需要触发实现网络挂接和不关心的响应。网络挂接花费的时间比超时更长的时间来应对这样我想的意图,简单地用“谢谢你聊天”,然后关闭对话,而实际上触发网络挂接回应。

感觉很容易,但我想的东西。此外,我是新来的dialogflow东西。

我可以用任何语言做到这一点,但这里是在Javascript中的例子:

fdk.handle(function (input) {
  // Some code here that takes 20 seconds.

  return {'fulfillmentText': 'i can respond but I will never make it here.'}
});

编辑1 - 尝试异步

当我使用异步功能,POST请求从未发生过。因此,在下面的代码:

fdk.handle(function (input) {
  callFlow(input);
  return { 'fulfillmentText': 'here is the response from the webhook!!' }
});

async function callFlow(input) {
  console.log("input is --> " + input)

  var url = "some_url"

  console.log("Requesting " + url)

  request(url, { json: true, headers: {'Access-Control-Allow-Origin' : '*'} }, (err, res, body) => {
    if (err) { return console.log(err); }
    console.log("body is...")
    console.log(body)
  });
}

我在日志中看到两个的console.log输出,而是从要求什么。和请求似乎并没有发生或者是因为我没有看到它在我的终点。

由于囚犯的小费。好像我需要通过callFlow(返回履行回JSON)和手柄()函数。现在,谷歌主页不会超时,并产生两个HTTP调用和响应。

const fdk = require('@fnproject/fdk');
const request = require('request');

fdk.handle(function (input) {
  return callFlow(input);
});

async function callFlow(input) {
  var searchPhrase = input || "cats"
  var url = "some url"

  return new Promise((resolve, reject) => {
    request.post(url, {
      headers: { 'content-type': 'application/x-www-form-urlencoded' },
      body: searchPhrase
    },
      function (err, resp, body) {
        if (err) { return console.log(err) }
        r = { 'fulfillmentText': `OK I've triggered the flow function with search term ${searchPhrase}` }
        resolve(r)
      }
    );
  });

}
dialogflow google-home fn
1个回答
2
投票

你不能异步触发履行。在对话模式,预期的实现将执行一些确定所述响应的逻辑。

你可以,但是,在返回结果之前不完成履行执行异步操作。

如果您使用的节点(8版本及以上)的足够现代版,您可以通过声明一个函数作为async功能,但不与await关键字调用它做到这一点。 (如果你没有用await调用它,它会等待异步操作完成,然后继续。)

所以,这样的事情应该工作,因为你的例子:

async function doSomethingLong(){
  // This takes 20 seconds
}

fdk.handle(function (input) {
  doSomethingLong();

  return {'fulfillmentText': 'This might respond before doSomethingLong finishes.'}
});

更新1根据您的代码示例。

这似乎很奇怪,你报告该呼叫到request不会出现在所有的工作要做,但也有关于它的一些奇怪的事情,可能会导致它。

首先,request本身不是一个异步函数。它使用一个回调模型和async功能不只是自动等待被调用的回调。所以,你的callFlow()函数调用console.log()了几次,电话request()和回调之前回报叫回来。

你或许应该有类似的request包取代request-promise-native并等待您从电话得到承诺。这使得callFlow()真正的异步(当它结束通话,你可以登录)。

其次,我想指出的是您显示的代码没有做一个POST操作。它默认为GET。如果你或你调用API,期待一个POST,这可能是错误的来源。但是,我没有料想到会被填充err参数,你的代码看起来像它会检查,并记录,这一点。

在整个设置的一个未知的,对我来说,是我不知道fdk如何处理异步功能,和我的文档的粗略浏览没有教育了我。我已经做到了这一点与其他框架,这是没有问题的,但我不知道是否fdk处理超时或执行其他任务,一旦发送应答杀了电话。

© www.soinside.com 2019 - 2024. All rights reserved.