Search code examples
node.jsfirebaseactions-on-googledialogflow-esfulfillment

I can not get information with "DialogflowApp.getUser" method


https://developers.google.com/actions/reference/nodejs/ApiAiApp

I'd like to obtain an access token on Dialogflow by referring to the above official website, but the execution result of DialogflowApp.getUser() will be null.

Account linking is set up and on the client side it is certified with a Google Account. AssistantToAgentDebug.assistantToAgentJson.user on the DEBUG tab of DialogFlow Simulator contains a value.

Also, I can get an access token by referring req.body.originalRequest.data.user.accessToken variable on the request body.

I'd like to obtain user information with DialogflowApp.getUser(), Is there a mistake in the definition below?

*Using the Fullfilment, the logic is described in index.js.

*index.js

'use strict';

const App = require('actions-on-google').DialogflowApp;

exports.testMethod = (req, res) => {

  // result is null
  const app = new App({req, res});
  let user = app.getUser();
  console.log('User is ' + user);

  // Access token can be acquired
  let accessToken = req.body.originalRequest.data.user.accessToken;
  console.log('accessToken is ' + accessToken);

  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ 'speech': 'success!', 'displayText': 'success!' }));
};

*package.json

{
  "name": "testMethod",
  "engines": {
    "node": "~4.2"
  },
  "dependencies": {
    "actions-on-google": "^1.0.0",
    "firebase-admin": "^4.2.1",
    "firebase-functions": "^0.5.7"
  }
}

Solution

  • The problem is that the call to the constructor expect the parameters to be named request and response. So you should write the line as

    const app = new App({request:req, response:res});