1

i am struggling a bit with my google assistant action. Right now i am using Dialogflow and Firebase for my webhook. In my code i would like to get data from an API, for example this one: API. I am coding with Node.js by the way. Since Node is asynchronous i do not know how to get the Data. When i try to make an Callback it doesnt work e.g.:

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}`)
    }

For some reason this works when i test it in an other application. With Dialogflow it does not work

I have also tried to use asynch for the function app.intent and tried it with await but this did not work too.

Do you have any idea how i could fix this?

Thank you in advance and best regards

Luca

Luca Kaiser
  • 25
  • 1
  • 7
  • What is the error you're getting, can try logging it? – James Ives Nov 29 '18 at 14:39
  • Possible duplicate of [Cloud Functions for Firebase - getaddrinfo ENOTFOUND](https://stackoverflow.com/questions/42774807/cloud-functions-for-firebase-getaddrinfo-enotfound) – James Ives Nov 29 '18 at 18:31
  • this libraray might help, https://www.npmjs.com/package/dialogflow-helper I wrote this library on the top of dialogflow rest client – Inzamam Malik Feb 17 '20 at 14:08

4 Answers4

2

You need to return Promise like

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

See following for details:

Dialogflow NodeJs Fulfillment V2 - webhook method call ends before completing callback

Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60
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.

James Ives
  • 3,177
  • 3
  • 30
  • 63
  • Have you looked at the original error message you're getting from the first example you posted? If it's related to the address not being found then this is likely the case. – James Ives Nov 29 '18 at 18:07
0

Due to asynchronicity, the function go() is gonna be called after the callback of callapi been executed.

Although you said that you have tried to use async, I suggest the following changes that are more likely to work in your scenario:

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}`)
      }
Aldo
  • 1,199
  • 12
  • 19
0

First follow the procedure given in their Github repository

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

Here you can find a working module already given in the README file. You have to make keyFilename object to store in SessionsClient object creation (go at the end of post to know how to genearate credentials file that is keyFileName). Below the working module.

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();

Here you can obtain the keyFileName file that is the credentials file using google cloud platform using

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

For complete tutorial (Hindi language) watch youtube video:

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

arg0100
  • 21
  • 3
  • this libraray might help, https://www.npmjs.com/package/dialogflow-helper I wrote this library on the top of dialogflow rest client – Inzamam Malik Feb 17 '20 at 14:09