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

188

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.

Share:
188
Muhaimin Jamalludin
Author by

Muhaimin Jamalludin

Updated on December 01, 2022

Comments

  • Muhaimin Jamalludin
    Muhaimin Jamalludin over 1 year

    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

    • Tlaquetzal
      Tlaquetzal over 3 years
      You should have your user id in your application which is communicating with Dialogflow from flutter. In the detectIntent request you can use the payload field in QueryParameters to send the information for the fulfillment. Can you share the code used to communicate to dialogflow from flutter?
    • Muhaimin Jamalludin
      Muhaimin Jamalludin over 3 years
      Thank you @tlaquetzal , I updated the question with the Dialogflow request.
  • Muhaimin Jamalludin
    Muhaimin Jamalludin over 3 years
    Thank you so much for your help :) . It worked.
  • Tlaquetzal
    Tlaquetzal over 3 years
    That's great, I'm happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please consider marking it as accepted