简体   繁体   中英

How to save the user response in Dialogflow via webhook to google cloud firestore using cloud functions

I had built a bot in dialogflow which consists of a series of intents and i want to save the user responses in firestore under a collection. I wrote the backend code in node.js but facing some issues. I am facing an issue called "App is not a constructor at exports.dialogflowFirebaseFulfillment.functions.https.onRequest"

const functions = require('firebase-functions');
const admin = require('firebase-admin');

const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

const array = require('array');

admin.initializeApp();

var db = admin.firestore();
var users = array();

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

const NAME_ACTION = 'Welcome';


exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    const app = new App({request, response});

    console.log('Request headers: ' + JSON.stringify(request.headers));
    console.log('Request body: ' + JSON.stringify(request.body));


    let action = request.body.result.action; // https://dialogflow.com/docs/actions-and-parameters
    console.log('Actions = '+ JSON.stringify(action));

    let query = request.body.result.resolvedQuery;

    const parameters = request.body.result.parameters; // https://dialogflow.com/docs/actions-and-parameters

    const inputContexts = request.body.result.contexts; // https://dialogflow.com/docs/contexts

    function makeName (app) {
           // early age
           let Name = app.getArgument(Name_Argument);

           if(action === 'Welcome'){
            var data = {
                0: Name,

              };

              var setDoc = db.collection('data').doc('one').set(data);
           }
    }
    let actionMap = new Map();
    actionMap.set(NAME_ACTION, makeName);
    app.handleRequest(actionMap);
    function sendResponse (responseToUser) {
        if (typeof responseToUser === 'string') {
            let responseJson = {};
            responseJson.text = responseToUser; // spoken response
            responseJson.displayText = responseToUser; // displayed response
            response.json(responseJson); // Send response to Dialogflow
        } else {
            let responseJson = {};

            responseJson.text = responseToUser.text || responseToUser.displayText;
            responseJson.displayText = responseToUser.displayText || responseToUser.text;

            responseJson.data = responseToUser.richResponses;

            responseJson.contextOut = responseToUser.outputContexts;

            response.json(responseJson); // Send response to Dialogflow
        }
    }
});

Check my code below,

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

Use const app = new WebhookClient({ request, response }); Instead of const app = new App({request, response});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM