在 dialogflow sdk v2 上将参数发送到 webhook [英] Send parameters to webhook on dialogflow sdk v2
问题描述
我正在尝试向 dialogflow (api.ai) 发送一些参数,例如用户名、电子邮件等,但我无法弄清楚.问题是我无法使用 Dialogflow v2 Nodejs SDK 获取/设置任何特定数据(例如用户名、电子邮件等).我尝试使用 queryParams.payload (v1: originalRequest) 但它没有以某种方式工作.此外,我尝试使用数据触发 自定义事件,但我无法在响应中获取任何事件数据.有人知道如何为 dialogFlow 上的会话发送一些特定数据吗?
I'm trying to send some parameters to dialogflow (api.ai) such as username, email, etc but I couldn't figure it out. The problem is I cannot get/set any specific data (such as username, email, etc.) with Dialogflow v2 Nodejs SDK. I tried to use queryParams.payload (v1: originalRequest) but It didn't work somehow. Also, I tried to trigger custom event with data but I couldn't get any event data on the response. Does someone know how to send some specific data for session talk on dialogFlow?
有效负载示例
const projectId = 'test-bot-test-1111';
const sessionId = user.uuid;
const languageCode = 'en-GB';
const sessionClient = new dialogFlow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode
}
},
queryParams: {
payload: {
data: {
username: 'bob',
email: 'bob@test.com'
}
}
}
};
let resultReq;
console.log('request :: ', request, '
');
try {
resultReq = await sessionClient.detectIntent(request);
} catch (err) {
// eslint-disable-next-line no-console
return console.error('ERROR:', err);
}
事件示例
const projectId = 'test-bot-test-1111';
const sessionId = user.uuid;
const languageCode = 'en-GB';
const sessionClient = new dialogFlow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
const request = {
session: sessionPath,
queryInput: {
event: {
name: 'custom_event',
languageCode,
parameters: {
name: 'sam',
user_name: 'sam',
a: 'saaaa'
}
}
},
queryParams: {
payload: {
data: user
}
}
};
let resultReq;
console.log('request :: ', request, '
');
try {
resultReq = await sessionClient.detectIntent(request);
} catch (err) {
// eslint-disable-next-line no-console
return console.error('ERROR:', err);
}
推荐答案
Dialogflow 的 v2 API 使用 gRPC 并且有一些怪癖,其中之一是您遇到的.如果您查看 Node.js 库的示例,您可以了解如何解决此问题.您需要实现一个 jsonToStructProto
方法来将您的 JavaScript 对象转换为 proto 结构,或者只需复制 structjson.js
文件in the sample in this gist.下面是一个使用 structjson.js
文件的完整示例:
Dialogflow's v2 API uses gRPC and has a few quirks, one of which you've run into. If you look at the samples for the Node.js library you can see how to workaround this. You'll need to impliment a jsonToStructProto
method to convert your JavaScript object to a proto struct or just copy the structjson.js
file in the sample in this gist. Below is a fully working example using the structjson.js
file:
// Imports the Dialogflow library
const dialogflow = require('dialogflow');
// Import the JSON to gRPC struct converter
const structjson = require('./structjson.js');
// Instantiates a sessison client
const sessionClient = new dialogflow.SessionsClient();
// The path to identify the agent that owns the created intent.
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
event: {
name: eventName,
parameters: structjson.jsonToStructProto({foo: 'bar'}),
languageCode: languageCode,
},
},
};
sessionClient
.detectIntent(request)
.then(responses => {
console.log('Detected intent');
logQueryResult(sessionClient, responses[0].queryResult);
})
.catch(err => {
console.error('ERROR:', err);
});
这篇关于在 dialogflow sdk v2 上将参数发送到 webhook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!