Dialogflow通过NodeJS传递参数 [英] Dialogflow pass parameters through NodeJS
问题描述
如何通过NodeJS请求传递参数?例如,我想传递代码和dialogflow中的名称,并使用包含我传递的参数的响应自动回答,例如 Hi $ name。
我的实际请求:
const请求= {
会话:sessionPath,
queryInput:{
文本:{
文本:查询,
languageCode:languageCode,
},
},
};
编辑4 [index.js],这是在答案之后的新正确代码
const projectId ='your-project-id';
const sessionId ='session-id';
常量查询=您的查询;
const languageCode ='您的语言';
const j = require( ./ structjson.js); //从答案中下载
const dialogflow = require(’dialogflow’);
const sessionClient = new dialogflow.SessionsClient({keyFilename:’./THIS-IS-RIGHT.json’});
//定义会话路径
const sessionPath = sessionClient.sessionPath(projectId,sessionId);
异步函数request(){
const contextClient = new dialogflow.ContextsClient({keyFilename:’./your-path-to-file.json’});
const contextData = {
名称:contextClient.contextPath(projectId,sessionId,您的上下文),
参数:j.jsonToStructProto({name:'John'} ),
lifespanCount:1
}; //名称为
的示例const context = await contextClient.createContext({
parent:sessionPath,
上下文:contextData
});
const请求= {
会话:sessionPath,
queryInput:{
文本:{
文本:query,
languageCode
}
},
queryParams:{
contexts:context //您可能想在此处添加其他上下文
}
};
const结果=等待sessionClient.detectIntent(request);
console.log(result);
}
request();
发送<$ c $时可以传递参数c>事件,而不是文本
。
您将需要转换一个javascript对象进行原型构建。有一个软件包
另一种方法是使用 dialogflow.ContextsClient
& client.createContext()
并将参数添加到上下文中,然后使用 queryInput
请求发送该上下文。 / p>
异步函数request(){
const contextClient = new dialogflow.ContextsClient({keyFilename:'..'}) ;
const sessionClient = new dialogflow.SessionsClient({keyFilename:’..’});
const contextData = {
名称:contextClient.contextPath('[PROJECT]','[SESSION]','[YOUR-CONTEXT]'),
参数:struct .encode({name:'John'}),
lifespanCount:1
};
const context = await contextClient.createContext({
父级:sessionPath,
上下文:contextData
});
const请求= {
会话:sessionPath,
queryInput:{
文本:{
文本:query,
languageCode
}
},
queryParams:{
contexts:context //您可能想在此处添加其他上下文
}
};
const结果=等待sessionClient.detectIntent(request);
console.log(result);
}
现在您将需要创建一个参数,值是:#your-context.name
How can i pass parameters through NodeJS request? I would like to pass, for example, the name from my code and dialogflow automatically answer with a response which contains the parameters i passed, as "Hi $name".
Mine actual request:
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
};
EDIT 4 [index.js], this is new right code After the answer
const projectId = 'your-project-id';
const sessionId = 'session-id';
const query = 'your-query';
const languageCode = 'your-language';
const j = require("./structjson.js"); //download It from the answer
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient({keyFilename: './THIS-IS-RIGHT.json'});
// Define session path
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
async function request() {
const contextClient = new dialogflow.ContextsClient({ keyFilename: './your-path-to-file.json' });
const contextData = {
name: contextClient.contextPath(projectId, sessionId, 'your-context'),
parameters: j.jsonToStructProto({ name: 'John' }),
lifespanCount: 1
};//An example for the name
const context = await contextClient.createContext({
parent: sessionPath,
context: contextData
});
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode
}
},
queryParams: {
contexts: context // You may want to add the other contexts here
}
};
const result = await sessionClient.detectIntent(request);
console.log(result);
}
request();
You can pass parameters when sending an event
instead of text
.
You will need to convert a javascript object to proto struct. There is a package pb-util that will handle the encoding/decoding
const { struct } = require('pb-util');
const request = {
session: sessionPath,
queryInput: {
event: {
name: eventName,
parameters: struct.encode({ name: 'John' }),
languageCode
}
}
};
After that you will need to create a parameter with the following syntax on your intent. #eventName.name
Another way to do it, is creating a context, using dialogflow.ContextsClient
& client.createContext()
and add the parameters to the context, and then send that context with the queryInput
request.
async function request() {
const contextClient = new dialogflow.ContextsClient({ keyFilename: '..' });
const sessionClient = new dialogflow.SessionsClient({ keyFilename: '..' });
const contextData = {
name: contextClient.contextPath('[PROJECT]', '[SESSION]', '[YOUR-CONTEXT]'),
parameters: struct.encode({ name: 'John' }),
lifespanCount: 1
};
const context = await contextClient.createContext({
parent: sessionPath,
context: contextData
});
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode
}
},
queryParams: {
contexts: context // You may want to add the other contexts here
}
};
const result = await sessionClient.detectIntent(request);
console.log(result);
}
And now you will need to create a parameter, which value is: #your-context.name
这篇关于Dialogflow通过NodeJS传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!