Search code examples
flutterdialogflow-esdialogflow-es-fulfillment

How to pass Firebase user UID to Dialogflow fulfillment to be able to save data


I plan to save data in "medic_reminder" collection for a specific user with uid. But I dont know how to pass the user uid that currently login through my application.

Here are the index.js for the Dialogflow fulfillment, here I am already specify the user uid:

async function handlerSaveFromDB(agent){
     const medicName = agent.parameters.medicName;
     const quantityTime = agent.parameters.quantiy;
     const dateEnded = agent.parameters.dateEnded;
     const dosage = agent.parameters.dosage;
     const timeFormat = agent.parameters.time;
     const time = new Date(timeFormat);  
     const minutes = time.getMinutes();
     const hour = time.getHours() -16;
     const ref = db.collection('users').doc('wM4uxxxxxxx').collection('medic_reminder').doc('5');
     const data = {
      dateEnded: new Date(dateEnded),
      dosage: dosage,
      hour: hour,
      howManyTimeDay: 0,
      id: '5',
      medName: medicName,
      min: minutes,
      quantityTime: quantityTime,
      status: true
    };
    const res = await ref.set(data);
     response.json({
       fulfillmentText: `Your reminder was saved`
   });
   }

Here is my Dialogflow request:

Future<AIResponse> detectIntent(String query) async {
    String queryParams = '{"resetContexts": ${this.resetContexts} }';
    if (payload.isNotEmpty) {
      queryParams =
          '{"resetContexts": ${this.resetContexts}, "payload": $payload}';
    }
    String body =
        '{"queryInput":{"audioConfig":{"languageCode":"$language"}},"outputAudioConfig":{"audioEncoding":"OUTPUT_AUDIO_ENCODING_LINEAR_16"}, "inputAudio":"$query","queryParams": $queryParams}';
    var response = await authGoogle.post(_getUrl(),
        headers: {
          HttpHeaders.authorizationHeader: "Bearer ${authGoogle.getToken}"
        },
        body: body);
    return AIResponse(body: json.decode(response.body));
  }
}

Please help me with any suggestions on how do I get the logged in user uid. Thanks


Solution

  • First you need to add the uid to the variable named payload. Based on your code, it seems that payload is a global variable, so if it's currently empty, just go ahead and add the user (see this question to know how to get the uid from Firebase):

    String uId = "uxxxx"; // This is an example variable, here you'll have your user from Firebase
    String payload = '{"uId":"$uId"}'; // Add it to a payload string
    

    In your flutter code, you are already prepared to send the payload information in the detectIntent request, so no need to change that part:

    if (payload.isNotEmpty) {
          queryParams =
              '{"resetContexts": ${this.resetContexts}, "payload": $payload}';
        }
    

    Finally, in your fulfillment code, it seems that you're using the dialogflow-fulfillment-nodejs library, so you need to access the payload in the originalRequest property in order to get the uId:

    let uId = agent.originalRequest.payload.uId;
    

    And that's it! Now you can use it on the rest of your fulfillment code.