Search code examples
javascriptnode.jsdialogflow-es

Dialogflow fulfillment fetch not showing response


When I call the intent llamada the intent webhook triggers but it doesn't appear what I pass in the 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, () => {
});

Dialogflow not showing the agent.add(json[0].name)

I get this error:

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.

How can I make it to get in Dialogflow the response correctly?


Solution

  • The issue is that you're doing an asynchronous operation with a Promise, but agent.handleRequest() doesn't know to wait for the Promise to complete. So it tries to return a result to Dialogflow immediately, but the result hasn't been set yet.

    In your case, addressing this is straightforward. You just need to return the Promise that is generated by the fetch().then()... chain. Something like this should work:

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