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

3

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, i will try to do it like this!
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.

1 Comment

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

1 Comment

sadly id doesnt even work when i try to put conv.ask(..) in an extra function
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

1 Comment

this libraray might help, npmjs.com/package/dialogflow-helper I wrote this library on the top of dialogflow rest client

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.