【问题标题】:Dialogflow fulfillment fetch not showing responseDialogflow 履行获取未显示响应
【发布时间】:2021-05-04 10:00:55
【问题描述】:

当我调用意图 llamada 时,意图 webhook 会触发,但它不会显示我在 agent.add(json[0].name) 中传递的内容。

const express = require('express')
const app = express()
const {WebhookClient} = require('dialogflow-fulfillment');

const fetch = require('node-fetch')

let url = (ommited)
let settings = {method: "Get"};

app.get('/', function (req, res) {
  res.send('Hello World')
})

app.post('/webhook', express.json() ,function (req, res) {
  const agent = new WebhookClient({ request:req, response:res }); 
 
  function llamada(agent) {
    fetch (url, settings)
    .then(res => res.json())
    .then((json) => {
      agent.add(json[0].name)
    })
    .catch((error) => {
      assert.isNotOk(error,'Promise error'); 
    });
  }

  let intentMap = new Map();
  intentMap.set('llamada', llamada);
  agent.handleRequest(intentMap);
})
 
app.listen(3000, () => {
});

我收到此错误:

ok
(node:9936) UnhandledPromiseRejectionWarning: Error: No responses defined for platform: DIALOGFLOW_CONSOLE
    at V2Agent.sendResponses_ (C:\Users\aasensio\Desktop\Zerca\node_modules\dialogflow-fulfillment\src\v2-agent.js:243:13)
    at WebhookClient.send_ (C:\Users\aasensio\Desktop\Zerca\node_modules\dialogflow-fulfillment\src\dialogflow-fulfillment.js:505:17)
    at C:\Users\aasensio\Desktop\Zerca\node_modules\dialogflow-fulfillment\src\dialogflow-fulfillment.js:316:38
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9936) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with 
.catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9936) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我怎样才能让它在 Dialogflow 中正确获得响应?

【问题讨论】:

    标签: javascript node.js dialogflow-es


    【解决方案1】:

    问题是您正在使用 Promise 执行异步操作,但 agent.handleRequest() 不知道要等待 Promise 完成。因此它会尝试立即将结果返回给 Dialogflow,但结果尚未设置。

    在您的情况下,解决这个问题很简单。您只需要返回由fetch().then()... 链生成的Promise。像这样的东西应该可以工作:

        return fetch (url, settings)
        .then(res => res.json())
        .then((json) => {
          agent.add(json[0].name)
        })
        .catch((error) => {
          assert.isNotOk(error,'Promise error'); 
        });
    

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多