如何在 dialogflow 行编辑器中实现 google 翻译器 api
How to implement the google translator api in the dialogflow line editor
我在 dialogflow 中实现翻译器时遇到问题,我不知道会出什么问题,代码对我不起作用。你能指导我吗?我澄清行编辑器不允许我实现异步功能。
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”,
“查询结果”:{
“queryText”:“你好”,
“参数”: {
“文字”:“你好”
},
"allRequiredParamsPresent": true,
"fulfillmentText": "翻译:未定义",
“fulfillmentMessages”:[
{
“文本”: {
“文本”: [
“翻译:未定义”
]
}
}
],
“输出上下文”:[
{
"姓名": "projects/canvas-primacy-314603/agent/sessions/d5048480-e5fb-c291-19d7-a2085d8d5fe2/contexts/text",
“寿命计数”:5,
“参数”: {
"text.original": "你好",
“文字”:“你好”
}
}
],
“意图”:{
"姓名": "projects/canvas-primacy-314603/agent/intents/b4144ec2-afd1-46bd-9347-7e20ae615f58",
"displayName": "traducir"
},
“intentDetectionConfidence”:1,
“诊断信息”:{
“webhook_latency_ms”:3292
},
"languageCode": "en",
“情感分析结果”:{
“queryTextSentiment”:{
“分数”:0.2,
“幅度”:0.2
}
}
},
“webhookStatus”:{
"message": "Webhook 执行成功"
}
}
代码存在一些问题。代码需要需要所需的库、定义代理、包含意图映射,并且函数必须命名为 dialogflowFirebaseFulfillment 才能使用 Dialogflow 实现库。
您可以查看 Dialogflow 实现库文档和示例以查看所需的样板元素1,然后在它们周围添加您的代码。
在 运行 代码之前,我设置了翻译冒号 (:) 之后的单词的意图。
在您的代码中,要正确获取响应,您应该使用 result.data
来获取响应正文。响应正文的结构如下:
{
"data":{
"translations":[
{
"translatedText":"translated_output_here"
}
]
}
}
因此您可以通过打印此 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);
});
}
测试完成:
我在 dialogflow 中实现翻译器时遇到问题,我不知道会出什么问题,代码对我不起作用。你能指导我吗?我澄清行编辑器不允许我实现异步功能。
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”, “查询结果”:{ “queryText”:“你好”, “参数”: { “文字”:“你好” }, "allRequiredParamsPresent": true, "fulfillmentText": "翻译:未定义", “fulfillmentMessages”:[ { “文本”: { “文本”: [ “翻译:未定义” ] } } ], “输出上下文”:[ { "姓名": "projects/canvas-primacy-314603/agent/sessions/d5048480-e5fb-c291-19d7-a2085d8d5fe2/contexts/text", “寿命计数”:5, “参数”: { "text.original": "你好", “文字”:“你好” } } ], “意图”:{ "姓名": "projects/canvas-primacy-314603/agent/intents/b4144ec2-afd1-46bd-9347-7e20ae615f58", "displayName": "traducir" }, “intentDetectionConfidence”:1, “诊断信息”:{ “webhook_latency_ms”:3292 }, "languageCode": "en", “情感分析结果”:{ “queryTextSentiment”:{ “分数”:0.2, “幅度”:0.2 } } }, “webhookStatus”:{ "message": "Webhook 执行成功" } }
代码存在一些问题。代码需要需要所需的库、定义代理、包含意图映射,并且函数必须命名为 dialogflowFirebaseFulfillment 才能使用 Dialogflow 实现库。
您可以查看 Dialogflow 实现库文档和示例以查看所需的样板元素1,然后在它们周围添加您的代码。
在 运行 代码之前,我设置了翻译冒号 (:) 之后的单词的意图。
在您的代码中,要正确获取响应,您应该使用 result.data
来获取响应正文。响应正文的结构如下:
{
"data":{
"translations":[
{
"translatedText":"translated_output_here"
}
]
}
}
因此您可以通过打印此 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);
});
}
测试完成: