axios - 如何在对话流行编辑器中实现谷歌翻译器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 执行成功" } }
解决方案
在您的代码中,要正确获取您应该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);
});
}
测试完成:
推荐阅读
- c# - 如何通过 Kubernetes c# API 方法“WebSocketNamespacedPodExecAsync”执行带参数的命令?
- javascript - 比较日期数组以在 java-script 中以简单的方式检查日期重叠
- python - 在 Tkinter 条目中隐藏文本光标
- laravel - laravel-websockets 连接到没有 laravel-echo 的通道
- android - Listview 项目文本未显示完整
- c# - 如何在c#中将递归对象与其他递归对象映射
- java - Android 10:windowTranslucentStatus 使状态栏透明
- c# - 用户 ' 登录失败
'。使用托管标识使用 Azure 函数应用和 Azure SQL 服务时,令牌已过期 - android - Android独特的闪屏:如何让它充满屏幕?
- android - 显示 DialogFragment 后重新显示父片段时,Android 回收器视图会调整大小