3

I need to send a unique identifier to my web service through Dialogflow Fulfillment so that I can recognize who is making the request. For that I need to uniquely identify a user on Dialogflow Fulfillment, but I can't find how to get a token or something like that inside the Inline Editor so that I can identify the device that is making the request.

I tried to see what there is inside the agent variable. But I found nothing that I could use to identify the user who is making the request to my web service.

I also tried to get the userStorage, like seen at How to identify unique users with Diagflow, but it returns me the error:

Cannot read property 'userStorage' of undefined
    at verificarBiologia (/user_code/index.js:37:76)
    at WebhookClient.handleRequest (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:273:44)
    at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:52:9)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:47)
    at /var/tmp/worker/worker.js:686:7
    at /var/tmp/worker/worker.js:670:9
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

Probably because the variable user is undefined.

This is my code:

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

  function verificarBiologia(agent) {

    agent.add('Inicio do metodo');

    console.log('Build 059');

    console.log(agent);

    let payload = request.body.originalDetectIntentRequest.payload;
    console.log(payload);


    let userStorage = request.body.originalDetectIntentRequest.payload.user.userStorage || JSON.stringify({});
    let userId;
    console.log("userStorage", userStorage);
    userStorage = JSON.parse(userStorage);
    console.log("userStorage_after_parsing", userStorage);


    agent.add('Final do metodo');
  }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('VerificarBiologia', verificarBiologia);
  agent.handleRequest(intentMap);
});

EDIT

The request body is as follow:

{
   "responseId":"f4ce5ff7-ac5f-4fec-b5bd-4e5007e4c2de",
   "queryResult":{
      "queryText":"Quando tenho prova de biologia?",
      "parameters":{
         "disciplinaBiologia":"biologia"
      },
      "allRequiredParamsPresent":true,
      "fulfillmentText":"Voc� tem uma prova de biologia no dia 30. Tire suas d�vidas com o professor.",
      "fulfillmentMessages":[
         {
            "text":{
               "text":[
                  "Voc� tem uma prova de biologia no dia 30. N�o deixe de fazer os exerc�cios."
               ]
            }
         }
      ],
      "intent":{
         "name":"projects/verificadorprovas/agent/intents/020017a0-e3a9-46f0-9a2e-d93009f5ac42",
         "displayName":"VerificarBiologia"
      },
      "intentDetectionConfidence":1,
      "languageCode":"en"
   },
   "originalDetectIntentRequest":{
      "payload":{

      }
   },
   "session":"projects/verificadorprovas/agent/sessions/3700fddf-3572-4221-fffc-a0dc1bf28330"
}

Can someone help me to do that? What do I have to do to get something that I can use to identify the user?

Thanks in advance

Claor Uninter
  • 141
  • 2
  • 11

2 Answers2

2

If you are planning to go for Google Assistant using Actions on Google, then the best way to identify a user is by using Account Linking. Check out the following link for more around fetching user information for Google Assistant.

Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60
  • Thanks for your answer. The sample code you linked me requires a conv object. Can you show me how I can get access to this conv object? – Claor Uninter Aug 20 '18 at 18:02
  • If you are only concerned with Actions-On-Google then you can start from this https://github.com/actions-on-google/actions-on-google-nodejs otherwise if you also want to support other platforms then just look at the inline editor code of Firebase in the Dialogflow Console. There some code is commented out which you may try for Google Assistant. – Abhinav Tyagi Aug 20 '18 at 18:17
2

Testing things in the Dialogflow simulator with "Try it now" does not simulate the Actions on Google environment. To do that, you need to use the Actions on Google Simulator, which you can get to by clicking on "See how it works in the Google Assistant" a few lines down.

enter image description here

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Thanks a lot. Using the Google Simulator alongside with the knowledge from https://stackoverflow.com/questions/50775342/how-to-identify-unique-users-with-diagflow, I managed to make it work as I needed. – Claor Uninter Aug 21 '18 at 14:21
  • One more quick question.. Is it possible to access userStorage outside from the Google Assistant? For example, an Android app saves a token on the userStorage and then I retrieve this token on Google Assistant? – Claor Uninter Aug 21 '18 at 15:12
  • If you have a separate question, please start a new Stack Overflow question to ask it. – Prisoner Aug 22 '18 at 14:02