1
votes

I'm having problems because the code I made in the DialogFlow Fulfillment index.js when I took the context parameters he was unable to send talking to the DialogFlow support I was informed that the DialogFlow Fulfillment does not recognize asynchronous functions so when I use the "push" from Firebase to send the parameters he doesn't send anything I believe he expects some parameter from the context but because he doesn't receive it he skips the push function and ends up not executing and doesn't send anything.

DialogFlow Fulfillment index.js code:


    const functions = require('firebase-functions');
    const { WebhookClient } = require('dialogflow-fulfillment');
    const { Card, Suggestion } = require('dialogflow-fulfillment');
    const admin = require('firebase-admin');
    admin.initializeApp({
        credential: admin.credential.applicationDefault(),
        databaseURL: 'https://testechatbot-2020.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 Mensagem(agent) {
            var context = agent.context.get('awainting_nome');
            var nome = context.parameters.nome;
            var mensagem = agent.parameters.mensagem;
            let teste = nome + " " + mensagem;
            try {
                admin.database().ref('Dados/').push({
                    Nome: nome,
                    Mensagem: mensagem
                });
            } catch (err) {
                console.error(err);
                return;
            }
        }
    
        let intentMap = new Map();
        intentMap.set('EntradaMensagem', Mensagem);
        agent.handleRequest(intentMap);
    });

DialogFlow Fulfillment package.json code:


    {
      "name": "dialogflowFirebaseFulfillment",
      "description": "Fluxo com envio de parametros para o Firebase",
      "version": "1.0.0",
      "private": true,
      "license": "Apache Version 2.0",
      "author": "Google Inc.",
      "esversion": 8,
      "engines": {
        "node": ">=10.0.0"
      },
      "scripts": {
        "start": "firebase serve",
        "deploy": "firebase deploy"
      },
      "dependencies": {
        "@google-cloud/firestore": "^0.16.1",
        "firebase-admin": "^8.13.0",
        "actions-on-google": "^2.2.0",
        "firebase-functions": "^3.7.0",
        "dialogflow": "^1.2.0",
        "dialogflow-fulfillment": "^0.6.0",
        "@google-cloud/dialogflow": "^3.0.0",
        "node-fetch": "^2.6.0"
      }  
    }

Image with response from DialogFlow support about asynchronous functions

response from DialogFlow support

1

1 Answers

0
votes

I'm not sure where you heard that the Intent Handler can't support async functions. They most certainly can. If you're using an async function (or a function that returns a Promise - same thing), you either must declare it an async function or return the Promise.

Your handler function should look something more like

    function Mensagem(agent) {
        var context = agent.context.get('awainting_nome');
        var nome = context.parameters.nome;
        var mensagem = agent.parameters.mensagem;
        let teste = nome + " " + mensagem;
        return admin.database().ref('Dados/').push({
                Nome: nome,
                Mensagem: mensagem
            })
            .then( snapshot => {
                agent.add( "pushed" );
            })
            .catch (err => {
                console.error(err);
                agent.add( "Error." );
            })
    }