首页 > 解决方案 > 如何在对话流行编辑器中实现谷歌翻译器api

问题描述

在对话流中实现翻译器时遇到问题,我不知道会出现什么问题,代码对我不起作用。你能指导我吗?我澄清一下,行编辑器不允许我实现异步功能。

const axios = require('axios'); 
const unirest = require('unirest');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

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 translate(agent){
const text = agent.parameters.text;
const key = "yout_key";
const to = 'es';
const from = 'en';
const response = axios.post(`https://www.googleapis.com/language/translate/v2?key=${key}&source=${from}&target=${to}&q=${text}`);
  return response.then((result) => {
  console.log(result.text);
  agent.add("traduccion: "+result.text);
  });
  }
  let intentMap = new Map();
  intentMap.set('traducir', translate);
  agent.handleRequest(intentMap);
});

这是我在原始 API 响应中获得的诊断信息 { "responseId": "7ecceeb9-9764-417a-9899-315dca16b550-b03aa3f9", "queryResult": { "queryText": "hello", "parameters": { "text": "hello" }, "allRequiredParamsPresent": true, "fulfillmentText": "traduccion: undefined", "fulfillmentMessages": [ { "text": { "text": [ "traduccion: undefined" ] } } ] , "outputContexts": [ { "name": "projects/canvas-primacy-314603/agent/sessions/d5048480-e5fb-c291-19d7-a2085d8d5fe2/contexts/text", "lifespanCount": 5, "parameters": { “text.original”:“你好”,“文本”:“你好”}}],“意图”:{“名称”:“projects/canvas-primacy-314603/agent/intents/b4144ec2-afd1-46bd-9347-7e20ae615f58”,“displayName”:“traducir”},“intentDetectionConfidence”:1,“diagnosticInfo”:{“webhook_latency_ms”:3292 } , "languageCode": "en", "sentimentAnalysisResult": { "queryTextSentiment": { "score": 0.2, "magnitude": 0.2 } } }, "webhookStatus": { "message": "Webhook 执行成功" } }{ "queryTextSentiment": { "score": 0.2, "magnitude": 0.2 } } }, "webhookStatus": { "message": "Webhook 执行成功" } }{ "queryTextSentiment": { "score": 0.2, "magnitude": 0.2 } } }, "webhookStatus": { "message": "Webhook 执行成功" } }

标签: axiosdialogflow-es-fulfillmentgoogle-translation-api

解决方案


在运行代码之前,我设置了翻译冒号(:)之后的单词的意图。 在此处输入图像描述

在您的代码中,要正确获取您应该result.data用来获取响应正文的响应。响应的主体结构如下:

{
   "data":{
      "translations":[
         {
            "translatedText":"translated_output_here"
         }
      ]
   }
}

因此,您可以通过打印 this 来访问翻译后的数据result.data.data.translations[0].translatedText

const text = agent.parameters.any; // This is based from my intent I used "any" as entity of words typed after the colon (:)
const key = "your_api_key";
const to = 'es';
const from = 'en';
const response = axios.post(`https://www.googleapis.com/language/translate/v2?key=${key}&source=${from}&target=${to}&q=${text}`);
  return response.then((result) => {
  console.log(result.data.data.translations[0].translatedText);
  output = result.data.data.translations[0].translatedText;
  agent.add("traduccion: "+ output);
  }); 
  }

测试完成:

在此处输入图像描述


推荐阅读