如何在dialogflow v2中调用事件:nodejs

时间:2018-10-12 12:00:48

标签: node.js npm dialogflow api-ai

我正在使用npm使用dialogflow v2。我只想在dialogflow中调用welcome事件。我该如何在nodejs中做到这一点。我很新。 这是我的代码

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

打开聊天页面时,我只想发送欢迎消息。为了做到这一点,我读到我必须打电话给活动。我怎样才能做到这一点?我已经从here

中获取了参考

2 个答案:

答案 0 :(得分:1)

请求正文应如下所示:

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

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

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

让我知道您是否遇到任何困难:)

答案 1 :(得分:0)

我建议使用actions-on-google,因为为dialogflow链接构建nodejs后端要容易得多:actions on google

有关示例项目,请参考Number genie project

希望这会对您有所帮助。