How can I connect to a MySql database, through webhook in Dialogflow?

0

What happens is that, I'm working with Dialogflow , given that I'm creating a Chatbot for a company, so through webhook, in the Fulfillment options I'm connecting to a Firebase database asi.

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

    // initialise DB connection
    const admin = require('firebase-admin');
    admin.initializeApp({
      credential: admin.credential.applicationDefault(),
      databaseURL: 'https://<Nombre de la base de datos>.firebaseio.com',
    });

    process.env.DEBUG = 'dialogflow:debug';

    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 handleAge(agent) {
        const edad = agent.parameters.edad;
         //agent.add('Muchas gracias...');


        //return admin.database().ref('ageInfo').once("value").then((snapshot) => {
        //  var averageAge = snapshot.child("runningAverage").val();
        //  agent.add('Our recorded average age is ' + averageAge);
        //});

         return admin.database().ref('ageInfo').transaction((ageInfo) => {
          if(ageInfo !== null) {
            let oldAverage = ageInfo.runningAverage;
            let oldTotalCount = ageInfo.totalCount;
            let newAverage = (oldAverage * oldTotalCount + edad) / (oldTotalCount + 1);
            ageInfo.runningAverage = newAverage;
            ageInfo.totalCount+=1;
            //agent.add('Nuestra edad promedio registrada es ' + newAverage);
          }
          return ageInfo;
        }, function(error, isSuccess) {
          console.log('Actualizar el éxito de transacción de edad promedio: ' + isSuccess);
        });


      }

      // Run the proper function handler based on the matched Dialogflow intent name
      let intentMap = new Map();
      intentMap.set('preguntaedad', handleAge);
      agent.handleRequest(intentMap);
    });

My question is if there is any possibility that I can connect to a database, to enter data entered by the user.

    
asked by Andrés Cantillo 25.09.2018 в 06:52
source

1 answer

0

Look, it's possible, I see 2 possibilities, that you do the webhook on the same server that you have the database and not on firebase (it's a bit more tedious I think), or you can create an API or server that accepts requests POST (writing) in which you have the BD and your webhook send the data to the server, in this case firebase will ask you to change to a payment plan because it is only allows requests to api's from Google in the free plan.

    
answered by 09.01.2019 в 10:12