Dialogflow @vitalets/google-translate-api 翻译不工作

Dialogflow @vitalets/google-translate-api Translation not working

我正在使用 firebase,但我的翻译不起作用:( 我没有收到任何回应,但其他功能运行正常

这是我的 index.js

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const translate = require('@vitalets/google-translate-api');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

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 welcome(agent) {
        agent.add(`Welcome to my agent!`);
    }

    function fallback(agent) {
        agent.add(`I didn't understand`);
        agent.add(`I'm sorry, can you try again?`);
    }

    function addTwoNo(agent) {
        const sno = agent.parameters['sno'];
        const fno = agent.parameters['fno'];
        var sum = sno + fno ;
        agent.add(`Sum is `+sum)
    }

    function tratext(agent) {
        const text = agent.parameters['text'];
        const lto = agent.parameters['lang-to'];
        const lfrom = agent.parameters['lang-from'];

        const ltoLen = lto.length > 0;
        const lfromLen = lfrom.length > 0;

        if(ltoLen){
            translate(agent.text, {to: lto}).then(res => {
                agent.add(res.text);
            }).catch(err => {
                console.error(err);
            });
        } else {
            translate(agent.text, {to: 'en'}).then(res => {
                agent.add(res.text);
            }).catch(err => {
                console.error(err);
            });
        }
        translate(agent, {to: 'en'}).then(res => {
            console.log(res.text);
            //=> I speak English
            console.log(res.from.language.iso);
            //=> nl
        }).catch(err => {
            console.error(err);
        });
    }


    // // Uncomment and edit to make your own intent handler
    // // uncomment `intentMap.set('your intent name here', yourFunctionHandler);`
    // // below to get this function to be run when a Dialogflow intent is matched
    // function yourFunctionHandler(agent) {
    //   agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
    //   agent.add(new Card({
    //       title: `Title: this is a card title`,
    //       imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
    //       text: `This is the body text of a card.  You can even use line\n  breaks and emoji! `,
    //       buttonText: 'This is a button',
    //       buttonUrl: 'https://assistant.google.com/'
    //     })
    //   );
    //   agent.add(new Suggestion(`Quick Reply`));
    //   agent.add(new Suggestion(`Suggestion`));
    //   agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
    // }

    // // Uncomment and edit to make your own Google Assistant intent handler
    // // uncomment `intentMap.set('your intent name here', googleAssistantHandler);`
    // // below to get this function to be run when a Dialogflow intent is matched
    // function googleAssistantHandler(agent) {
    //   let conv = agent.conv(); // Get Actions on Google library conv instance
    //   conv.ask('Hello from the Actions on Google client library!') // Use Actions on Google library
    //   agent.add(conv); // Add Actions on Google library responses to your agent's response
    // }
    // // See https://github.com/dialogflow/fulfillment-actions-library-nodejs
    // // for a complete Dialogflow fulfillment library Actions on Google client library v2 integration sample

    // Run the proper function handler based on the matched Dialogflow intent name
    let intentMap = new Map();
    intentMap.set('Default Welcome Intent', welcome);
    intentMap.set('addTwoNo', addTwoNo);
    intentMap.set('Default Fallback Intent', fallback);
    intentMap.set('tratext',tratext)
    // intentMap.set('your intent name here', yourFunctionHandler);
    // intentMap.set('your intent name here', googleAssistantHandler);
    agent.handleRequest(intentMap);
});

这是我的 package.json

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "10"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "^2.2.0",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.5.0",
    "@vitalets/google-translate-api": "^4.0.0"
  }
}

我正在使用 pay 去 firebase

我使用 addTwoNo 函数进行了测试,它可以正常工作,但不能进行翻译

希望能得到一些帮助

看来,在您的 tratext 函数(代理)下,您正在翻译“代理”而不是“agent.text”。您可以尝试如下操作:

translate(agent.text, {to: 'en'}).then(res => {
    console.log(res.text);
    //=> I speak English
    console.log(res.from.language.iso);
    //=> nl
}).catch(err => {
    console.error(err);
});

此外,为了排除翻译 api 身份验证的任何问题,您可以通过翻译虚拟文本来调试翻译 api。我建议您尝试使用此代码片段并验证生成的日志:

translate("Hola mundo", {to: 'en'}).then(res => {
    console.log(res);
}).catch(err => {
    console.error(err);
});

最后,我发现了一个github repository 用户分享了一个使用 NodeJS 下的 dialogflow fulfillment 翻译文本的代码片段,您可以参考。