Search code examples
axiosdialogflow-es-fulfillmentgoogle-translation-api

How to implement the google translator api in the dialogflow line editor


I am having problems when implementing a translator in dialogflow, I don't know what will be wrong, the code does not work for me. could you please guide me. I clarify that the line editor does not let me implement an asynchronous function.

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

this is the Diagnostic info that I get in the Raw API response { "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": "hello", "text": "hello" } } ], "intent": { "name": "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 execution successful" } }


Solution

  • Before running the code I set up my intent to translate words after the colon (:). enter image description here

    In your code, to properly get the response you should be using result.data to get the body of the response. The body of the response is structured this way:

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

    Thus you can access the translated data by printing 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);
      }); 
      }
    

    Testing done:

    enter image description here