1

I'm using dialogflow v2 using npm. I just want call a welcome event in dialogflow. How can I do it in the nodejs. I'm pretty new to it. This is my code

const projectId = "xxxxxx";
  const LANGUAGE_CODE = 'en-US';
  const sessionId = req.body.sessionId;
  var query = req.body.query;

  const sessionClient = new dialogflow.SessionsClient();
  const sessionPath = sessionClient.sessionPath(projectId,sessionId);
  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        text: query,
        languageCode: LANGUAGE_CODE,
      },
    },
  };

  sessionClient.detectIntent(request).then(response => {
    console.log('intent detected');
    const result = response[0].queryResult;
    console.log(`  Query: ${result.queryText}`);
    console.log(`  Response: ${result.fulfillmentText}`);

    if(result.fulfillmentText) {
      console.log(result.fulfillmentText);
      return res.json({reply: result.fulfillmentText})
    }
    // if(result.intent) {
    //   console.log(`  Intent: ${result.intent.displayName}`)
    // }
    else {
      console.log('no intent found');
    }
  }).catch(err => {
    console.log('error '+err);
  })

As I open the chat page I just want to throw a welcome message. In order to do that, i read that i have to call the event. How can I do that? I have taken the reference from here

raj_tx
  • 229
  • 1
  • 5
  • 18

2 Answers2

5

The Request body should be like this:

let eventName='WELCOME';  //name of the event

 let request = {
        session: sessionPath,
        queryInput: {
          event: {
            name: eventName,  
            languageCode: 'en-US'
          },
        },
      };

checkout- https://github.com/googleapis/nodejs-dialogflow/blob/master/samples/detect.js#L96

let me know if you find any difficulties :)

1

I would suggest using actions-on-google as it is much easier to build nodejs backend for dialogflow link : actions on google

And for sample project refer Number genie project

Hope this would help you.

siddhant sankhe
  • 623
  • 6
  • 12
  • where is code in genie demo app to call an event? eg something like `conv.event('EVT')` – dcsan Oct 13 '19 at 05:33
  • In `number-genie` they have used `conv.contexts` context based approach , so the intents are triggered by context set , for event based intent triggering you can trigger events too but u will need `dialogflow-fulfillment` library for event based approach i guess , Hope this helps you. – siddhant sankhe Oct 14 '19 at 07:02
  • `const { WebhookClient } = require('dialogflow-fulfillment'); const agent = new WebhookClient({ request, response }); //with data agent.setFollowupEvent({ "event": { "name": 'StartGame', "data": { 'type': 'test' } } }); //direct trigger agent.setFollowupEvent('StartGame'); ` Hope this works. – siddhant sankhe Oct 14 '19 at 07:02