NodeJS and Dialogflow connection issue

i’m trying to connect my dialogflow agent with nodejs to create a web application. I’ve tried following the repository https://github.com/googleapis/nodejs-dialogflow , but the terminal shows errors.
Would also like to know how to implement a messenger box to this, any help is appreciated.

Code :

const dialogflow = require('@google-cloud/dialogflow');
const uuid = require('uuid');
const config = require('./counselling-3.json'); 

if(!config.private_key) throw new Error('Private key required')
if(!config.client_email)throw new Error('Client email required')
if(!config.project_id) throw new Error('project required')

const credentials = {
    client_email: config.client_email,
    private_key: config.private_key,
};

const sessionClient = new dialogflow.SessionsClient(
    {
        projectId: config.project_id,
        credentials
    }
);
/**
 * Send a query to the dialogflow agent, and return the query result.
 * @param {string} projectId The project to be used
 */
async function runSample(projectId = 'xxxxxxxxx') {
  // A unique identifier for the given session
  const sessionId = uuid.v4();

  // Create a new session
  const sessionPath = sessionClient.sessionPath(config.project_id, sessionId);

  // The text query request.
  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        // The query to send to the dialogflow agent
        text: 'hello',
        // 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');
  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();

Error:

(node:13536) UnhandledPromiseRejectionWarning: TypeError: sessionClient.sessionPath is not a function
    at runSample (E:\Pharmabot\server.js:29:37)
    at Object.<anonymous> (E:\Pharmabot\server.js:57:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13536) 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:13536) [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.

7 thoughts on “NodeJS and Dialogflow connection issue”

  1. That’s because the method is named .projectAgentSessionSessionPath(), not .sessionPath(). This is also shown on the GitHub page you linked to.

      const sessionPath = sessionClient.projectAgentSessionPath(config.project_id, sessionId);
    

    In general you should aim to read the documentation and example code first before using StackOverflow. You should also get in the habit of reading the error, which clearly shows your issue. I have never used this library but was able to quickly spot the problem from looking at the first example from your own link.

    Reply

Leave a Comment