问题描述
我正在使用 dialogflow
NPM模块,我想发送输入/输出上下文
,但是我不确定如何去做。
我知道我可以使用
在 google-assistant
NPM中进行操作我可以设置 contexts
参数
使用以下方法,
I am using dialogflow
NPM module and I want to send input/output context
but I am not sure how to do.I know I can do in google-assistant
NPM withI can set contexts
with parameter
using below method,
const parameters = { // Custom parameters to pass with context
welcome: true,
};
conv.contexts.set('welcome-context', 5, parameters);
推荐答案
为了使用 NPM模块,您必须首先使用 dialogflow.ContextsClient
,然后将其发送给查询。
In order to send a context using the Dialogflow NPM module, you have to first create a context, using dialogflow.ContextsClient
and then send it on the query.
要将参数转换为Dialogflow所需的格式,您将需要使用以下模块:
To convert the parameters to the format required by Dialogflow you will need to use this module: pb-util
const dialogflow = require('dialogflow');
const { struct } = require('pb-util');
const projectId = 'projectId';
const contextsClient = new dialogflow.ContextsClient();
const sessionClient = new dialogflow.SessionsClient();
async function createContext(sessionId, contextId, parameters, lifespanCount = 5) {
const sessionPath = contextsClient.sessionPath(projectId, sessionId);
const contextPath = contextsClient.contextPath(
projectId,
sessionId,
contextId
);
const request = {
parent: sessionPath,
context: {
name: contextPath,
parameters: struct.encode(parameters)
lifespanCount
}
};
const [context] = await contextsClient.createContext(request);
return context;
}
function sendQuery(sessionId, query, context) {
const session = sessionClient.sessionPath(projectId, sessionId);
const request = {
session,
queryInput: {
text: {
text: query
}
},
queryParams: {
contexts: [context] // You can pass multiple contexts if you wish
}
};
return sessionClient.detectIntent(request);
}
(async() => {
const parameters = { // Custom parameters to pass with context
welcome: true
};
const sessionId = 'my-session';
const context = await createContext(sessionId, 'welcome-context', parameters);
const response = await sendQuery(sessionId, 'Hi', context);
console.log(response);
})();
请记住,您发送输入上下文。
Have in mind that you send an input context. The output context is generated in the intent.
如果您无法通过身份验证客户端 SessionClient
& ContextClient
您可以检查以下其他问题:
If you have trouble authenticating the clients SessionClient
& ContextClient
you can check this other question: Dialogflow easy way for authorization
这篇关于设置输入或输出上下文对话流nodejs v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!