DialogFlow Fulfillment 中的代码问题,因为它无法识别 Firebase 异步函数

Code problem in DialogFlow Fulfillment as it does not recognize Firebase asynchronous functions

我遇到问题是因为我在 DialogFlow Fulfillment 中编写的代码 index.js 当我获取上下文参数时他无法发送与 DialogFlow 支持的对话 我被告知 DialogFlow Fulfillment 无法识别异步函数所以当我使用 Firebase 的“推送”来发送参数时他没有发送任何东西我相信他期望来自上下文的一些参数但是因为他没有收到它他跳过推送函数并最终没有执行并且不发送任何东西。

DialogFlow 实现 index.js 代码:


    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 实现 package.json 代码:


    {
      "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"
      }  
    }

带有 DialogFlow 支持的关于异步函数的响应的图片

response from DialogFlow support

我不确定您从哪里听说 Intent Handler 不支持异步函数。他们当然可以。如果您使用的是异步函数(或 return 是 Promise 的函数 - 同样的事情),则必须将其声明为异步函数或 return Promise。

你的处理函数应该看起来更像

    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." );
            })
    }